Tuesday, July 14, 2009

C++ input question?

so here is the thing I have a program broken up into classes with there appropriate implementation files.





This problem im getting is that the in the main menu I created when the user inputs there choice from the menu ive done it like this:





basically its





cin %26gt;%26gt; choice;





switch(choice)


{


case 1:





break;





}





and so on.





But once they choice is made there is a conflict because the choice is taken in by the cin %26gt;%26gt; operator and the first thing taken in after the choice is made is using the getline because it needs to be able to take spaces. So the question is this....





Is there another way I can do my menu so that it doesnt use cin? I tried doing getline but it doesnt work with switch or if statements for some reason.





Since getline is from C not c++ is there an equevalent that will work for my situation???





thanks for reading!

C++ input question?
Q


But once they choice is made there is a conflict because the choice is taken in by the cin %26gt;%26gt; operator and the first thing taken in after the choice is made is using the getline


End Q


See http://www.daniweb.com/tutorials/tutoria... .





Q


Is there another way I can do my menu so that it doesnt use cin? I tried doing getline but it doesnt work with switch or if statements for some reason.


End Q


Let me guess, choice Is an integer? Because if you take a look at getline (http://www.cplusplus.com/reference/strin... ), you’ll see that it requires a string. There’s a C string version and then there’s a C++ string version. Either one you can convert to an integer for using in a switch statement. But you need to go the conversion route with getline.





Q


Since getline is from C not c++ is there an equevalent that will work for my situation???


End Q


Getline is from C++ not C. What references are you using? Get better ones.
Reply:Before the line


cin%26gt;%26gt;choice,


add the line


fflush(stdin);


There are chances that this may work out.

columbine

No comments:

Post a Comment