Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2005, 6:54 AM   #1
Border Fox
Newbie
 
Join Date: Jan 2005
Posts: 5
Rep Power: 0 Border Fox is on a distinguished road
URGENT HELP NEEDED: Whats wrong with my code?

The code below should take a user entered floating point number and use functions SEPARATE (to break the number up into its individual parts) and PRINTOUT ( to display the individual parts ).
despite my best efforts i seem to be up against a brick wall due to my inexperience.

according to my compiler there are errors on:

num.cpp: In function `int main()':
num.cpp:29: error: syntax error before `(' token
num.cpp: In function `void separate(number)':
num.cpp:37: error: syntax error before `;' token
num.cpp:41: error: non-lvalue in assignment
num.cpp:42: error: non-lvalue in assignment

can anyone help me and sort out these errors? better still, could anyone find a better way of solving the above task because i dont think mine is ever guna work!
I think i needa use FLOOR & FABS in the separate funciton but i dont know how to.


_________________________________________________________________________
1 #include <iostream>
2
3 using std::cout;
4 using std::cin;
5 using std::endl;
6
7 #include <cmath>
8
9 struct number {
10 double initial;
11 char sign;
12 int mag;
13 double fract;
14 };
15
16 void separate ( struct number );
17
18 void printOut ( struct number );
19
20 int main ()
21 {
22 number value;
23
24 cout << "Input a real number (one with decimal point): " << endl;
25 cin >> value.initial;
26
27 separate ( value )
28
29 printOut ( value )
30
31 return 0;
32 }
33
34 void separate ( struct number value )
35 {
36 if ( value.initial > 0 )
37 value.sign = +;
38 else
39 value.sign = -;
40
41 floor ( value.initial ) = value.mag;
42 value.initial - value.mag = value.fract;
43
44 }
45
46 void printOut ( struct number value )
47 {
48 cout << "Number to be analysed: " << value.initial << endl;
49 cout << "Sign: " << value.sign << endl;
50 cout << "Whole number: " << value.mag << endl;
51 cout << "Fractional part: " << value.fract << endl;
52 }

__________________________________________________________________________
Border Fox is offline   Reply With Quote
Old Jan 26th, 2005, 7:08 AM   #2
drewlander
Newbie
 
drewlander's Avatar
 
Join Date: Jan 2005
Location: Pittsburgh, PA
Posts: 16
Rep Power: 0 drewlander is on a distinguished road
Send a message via AIM to drewlander
I can see that there are no semicolons in lines 27 and 29;
if + is to be a char then you need single quotes around them (lines 37 and 39 '+' '-');
and for lines 41 and 42 you have things backwards
floor returns a value, therefore you have to assing value.mag to the leftof it like so:
value.mag=floor ( value.initial );
same with line 42
value.fract = value.initial - value.mag;

doing so made it compile fine for me, hope that helps!
__________________
On and on....
drewlander is offline   Reply With Quote
Old Jan 28th, 2005, 7:12 AM   #3
Border Fox
Newbie
 
Join Date: Jan 2005
Posts: 5
Rep Power: 0 Border Fox is on a distinguished road
it's stil not working, iv tried implementing these changes and although it compiles error free it gives strange results.
im thinking the functions mustn't be working properly.
can someone help me to get it to compile and run successfully?
Border Fox is offline   Reply With Quote
Old Jan 28th, 2005, 10:12 AM   #4
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4 Benoit is on a distinguished road
What errors does the complier give you after correcting the ones he pointed out?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Jan 28th, 2005, 10:30 AM   #5
Border Fox
Newbie
 
Join Date: Jan 2005
Posts: 5
Rep Power: 0 Border Fox is on a distinguished road
Quote:
Originally Posted by Benoit
What errors does the complier give you after correcting the ones he pointed out?
[d4033875@linux12 ~]$ gcpp ica3.cpp
[d4033875@linux12 ~]$ ica3
Input a real number (one with decimal point):
34.56
Number to be analysed: 34.56
Sign: �
Whole number: 134515518
Fractional part: 1.06444e-304

iv tried debugging methods:

void separate ( struct number value )
{
if ( value.initial > 0 )
value.sign = '+';
else
value.sign = '-';

value.mag = floor ( value.initial );
value.fract = fabs ( value.initial - value.mag );

cout << "In function separate(): " << endl;
cout << "Number to be analysed: " << value.initial << endl;
cout << "Sign: " << value.sign << endl;
cout << "Whole number: " << value.mag << endl;
cout << "Fractional part: " << value.fract << endl << endl;

}

and also

cout << "Input a real number (one with decimal point): " << endl;
cin >> value.initial;

separate ( value );

cout << "After function separate(): " << endl;
cout << "Number to be analysed: " << value.initial << endl;
cout << "Sign: " << value.sign << endl;
cout << "Whole number: " << value.mag << endl;
cout << "Fractional part: " << value.fract << endl << endl;


printOut ( value );


this enabled me to see that the problem lies in main ( i think )
when i did the debuggin in function separate the program successfully compiled error-free, it printed the correct results...BUT it also printed a second set of incorrect results after the correct set. see below:

[d4033875@linux12 ~]$ gcpp ica3.cpp
[d4033875@linux12 ~]$ ica3
Input a real number (one with decimal point):
23.45
In function separate():
Number to be analysed: 23.45
Sign: +
Whole number: 23
Fractional part: 0.45

Number to be analysed: 23.45
Sign: �
Whole number: 134515794
Fractional part: 1.06444e-304


from this i think that function separate is fine.
but the values arent being passed back to main....OR ELSE the values aren't being passed from main to printOut....OR ELSE there is something wrong with printOut.


question is.,...what? and how do i fix it? HELP!!
Border Fox is offline   Reply With Quote
Old Jan 28th, 2005, 11:09 AM   #6
Monster
Newbie
 
Join Date: Jan 2005
Posts: 20
Rep Power: 0 Monster is on a distinguished road
You are (almost) correct. The values are not passed correctly to the separate function.

That's because you are passing the complete struct. What the program will do is create a copy of the struct and put it on the stack. The separate function will modify the copy of the struct, not the original struct.

To solve this you need to pass the address of the struct. The separate function can use this address to access (and modify) the struct.

Something like this:
void separate(struct number *); 

void separate ( struct number *value )
{
   if ( value->initial > 0 ) 
      value->sign = '+';
   else
      value->sign = '-';

   value->mag = floor(value->initial);
   value->fract = value->initial - value->mag ;
}

int main(void)
{
   ...
   separate ( &value );
   ...
}
Monster is offline   Reply With Quote
Old Jan 28th, 2005, 11:19 AM   #7
Border Fox
Newbie
 
Join Date: Jan 2005
Posts: 5
Rep Power: 0 Border Fox is on a distinguished road
i have posted this problem on 3 separate forums....sorry monster but u have been beaten to the solution.
another guy give me 2 solutions, the one im using is the one you gave which is perfect.
thanx anyway.

i have another minor problem in another program....this one should take two user entered dates (as structures) and determine whether or not they are the same.
my program works but it needs a bit of validating ( even tho i dont think we get marked on validation but it just tidies the program up)

here's the code:

-----------------------------------------------------------------------------------------------------------------------------------

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

struct date {
int day;
char month [ 10 ];
int year;
};

int isequal ( struct date, struct date );

int main ()
{
struct date date1;
struct date date2;

cout << "\n";
cout << "Input the day for date1: " << endl;
cin >> date1.day;
cout << "Input the month for date1 (as a 3-lettered abbreviation eg: jan, feb, mar ): " << endl;
cin >> date1.month;
cout << "Input the year for date1: " << endl;
cin >> date1.year;
cout << "\n";
cout << date1.day << '/' << date1.month << '/' << date1.year << endl;

cout << "\n";
cout << "Input the day for date2: " << endl;
cin >> date2.day;
cout << "Input the month for date2 (as a 3-lettered abbreviation eg: jan, feb, mar ): " << endl;
cin >> date2.month;
cout << "Input the year for date2: " << endl;
cin >> date2.year;
cout << "\n";
cout << date2.day << '/' << date2.month << '/' << date2.year << endl;

cout << "\n";
if ( isequal ( date1, date2 ) == 0 )
cout << "The two dates are exactly the same" << endl;
else
cout << "The two dates are not the same" << endl;
cout << "\n";

return 0;
}

int isequal ( struct date date1, struct date date2 )
{
if ( date1.day == date2.day && date1.year == date2.year &&
!strcmp ( date1.month, date2.month ) )
return 0;
else
return 1;
}

-------------------------------------------------------------------------------------------------------------------------------

here's part of script file:

[d4033875@linux12 ~]$ gcpp ica2.cpp
[d4033875@linux12 ~]$ ica2

Input the day for date1:
12
Input the month for date1 (as a 3-lettered abbreviation eg: jan, feb, mar ):
01
Input the year for date1:
2000

12/01/2000

Input the day for date2:
13
Input the month for date2 (as a 3-lettered abbreviation eg: jan, feb, mar ):
05
Input the year for date2:
1954

13/05/1954

The two dates are not the same



As you can see the program works but there is no limits....people could enter February 31st, or make up months of their own.
the main problem is that if i entered for example 31/12/2000 as date1, and 31/dec/2000 as date 2...they dont match.
or 14/january/2000 and 14/jan/2000 dont match etc etc etc.

i tried countering this by prompting users to enter months with a 3 letter abbreviation of the month name. however this doesnt stop them making up months or using numbers or whatever.
i think when i compare the strings i should compare each element of the array in the string. that way i could compare 'jan' to 'january' and see they are the same....right?

anybody know how to do this ( or some kind of method that would work???)
Border Fox is offline   Reply With Quote
Old Jan 28th, 2005, 11:55 AM   #8
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
Instead of allowing users to input months as text strings, why not enforce a numeric entry for the month? That way you'd only have to test to make sure the number is between 1 and 12 to validate...
If you want to allow users to enter either text or numbers, I'd first check to see which they entered. If they enter a number, see above. If they entered text, convert that to the numeric equivilant (use a switch perhaps?) and then compare.
Sorry no code - trying to get this in while at work...
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~

Hockeyman is offline   Reply With Quote
Old Jan 29th, 2005, 10:29 PM   #9
codetaino
Programmer
 
codetaino's Avatar
 
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 4 codetaino is on a distinguished road
I agree with hockeyman, but if you still want to use the abreviation... why dont you validate it when they enter the info and ask for it again... if they entered it incorrectly?? you can use a loop to do this
__________________
"God bless u all" :)
codetaino is offline   Reply With Quote
Old Jan 30th, 2005, 6:19 AM   #10
Border Fox
Newbie
 
Join Date: Jan 2005
Posts: 5
Rep Power: 0 Border Fox is on a distinguished road
because i have to use the structure as shown, integer day, month as a string, year as an integer.
Border Fox is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:04 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC