![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 5
Rep Power: 0
![]() |
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 } __________________________________________________________________________ |
|
|
|
|
|
#2 |
|
Newbie
|
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.... |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2005
Posts: 5
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 550
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Jan 2005
Posts: 5
Rep Power: 0
![]() |
Quote:
[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!! |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jan 2005
Posts: 20
Rep Power: 0
![]() |
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 );
...
} |
|
|
|
|
|
#7 |
|
Programmer
|
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. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 4
![]() |
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" :) |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jan 2005
Posts: 5
Rep Power: 0
![]() |
because i have to use the structure as shown, integer day, month as a string, year as an integer.
|
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 4
![]() |
Quote:
--codetaino
__________________
"God bless u all" :) |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|