Monday, May 24, 2010

Having a problem with if, else if, and else statements in C++...?

I'm having a problem making a program that calculates angle measures. Whenever a user types an input for the variable "input", it doesn't recognize it even if it is typed in correctly, and goes directly to do else statement, "Invalid command." What's wrong, how do I fix this? Thanks.





#include %26lt;iostream%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string%26gt;


using namespace std;


int main(int argc, char *argv[])


{


cout %26lt;%26lt; "Please enter in Sum Of Angles or Angle Measure" %26lt;%26lt; endl;


string input;


cin %26gt;%26gt; input;


if (input == "Sum Of Angles")


{


cout %26lt;%26lt; "Please enter number of sides of your regular polygon" %26lt;%26lt; endl;


int a;


cin %26gt;%26gt; a;


cout %26lt;%26lt; "Your answer is " %26lt;%26lt; (a - 2) * 180 %26lt;%26lt; endl;


}


else if (input == "Angle Measure")


{


cout %26lt;%26lt; "Please enter number of sides of your regular polygon" %26lt;%26lt; endl;


int b;


cin %26gt;%26gt; b;


cout %26lt;%26lt; "Your answer is " %26lt;%26lt; (b - 2) * 180 / b %26lt;%26lt; endl;


}


else {


cout %26lt;%26lt; "Invalid Command" %26lt;%26lt; endl;


}


system("PAUSE");


return 0;


}

Having a problem with if, else if, and else statements in C++...?
Replace the line (near the top):





cin %26gt;%26gt; input;





with:





getline(cin, input);





And that should do it. When using cin to read a string, it only reads up until the first whitespace character that's typed (space, tab, or newline). getline, here, reads from cin into input up until the newline character you type when you press [ENTER].





So, as it was originally, when you type "Sum Of Angles" it would read the word "Sum" into the input variable, and then hold onto the "Of Angles" for future calls to cin.





So, if I did this:


string a, b ,c;


cin %26gt;%26gt; a;


cin %26gt;%26gt; b;


cin %26gt;%26gt; c;


and typed "Sum Of Angles" all at once and pressed [ENTER], "Sum" would end up in a, and it wouldn't have bothered to wait for me to type anything and press [ENTER] again for b or c. I had typed three things separated by spaces all at once, so it would immediately place "Of" in b and "Angles" in c.





The Psyco's answer is correct if we were talking about c strings here, but you're using a c++ string. C strings are pointers to arrays of characters ( char* ) and c++ strings are objects of a class called string, which you use here.





if ( !strcmp( input , "Sum of Angles" ) )


would only work if input was as c string. So doing that won't even compile. However, you could have done:





if ( !strcmp( input.c_str() , "Sum of Angles" ) )





c_str() is a function of the string class that returns a c++ string object as a c string. The reason if(input=="Sum Of Angles") works is because of "operator overloading". I mean, the "==" operator is redefined for use with c++ strings, so it actually compares the contents of the two strings.
Reply:You can't use ... if ( input == "Sum of angles" )





What that test does is you are comparing the memory address of the variable input to the memory address of "Sum of Angles", which will never be equal.





If you are comparing strings, you need to use STRCMP.





say...





if ( !strcmp( input , "Sum of Angles" ) )


{


...


}





See the command here..





http://www.cplusplus.com/reference/clibr...
Reply:Looks like it is your homework assignment. Consider contacting a freelancer at websites like http://getafreelnacer.com/


No comments:

Post a Comment