![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Jan 2002
Location: Brisbane, QLD
Posts: 46
|
Basically i need to use
std::getline(std::cin, reason) reason is a std::string and well i would have expected this to work perfectly except for the fact that the program does not wait for user input and just goes on to the next prompt in the program ... this is quite frustrating yet it works as expected if used from main() ... but where i am using it from is deep inside a class. Thanks
|
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
|
Have you tried cin.readline(reason) ?
I think you should also look at your class structure, its generally not good practice for something deep inside a class to be getting user input, it reduces the reusability of the code. Perhaps you should get the user input somewhere else and pass it as a parameter to the class that needs it.
__________________
"That's the way good software gets designed. So if you pull out a piece it won't run" - Steve Ballmer |
|
|
|
|
|
#3 |
|
Member
Join Date: Jun 2001
Location: Westside, Ippaswitch
Posts: 913
|
You probably need to clear your input buffer cin.clear()? before attempting to read user input.
|
|
|
|
|
|
#4 |
|
Member
Join Date: Jan 2002
Location: Brisbane, QLD
Posts: 46
|
Thanks
BUT i have tried as a function (not in class, local to the file) and that produces the same output, also getline(cin, reason); and cin.getline(buff, buff_size); (buff is a char[buff_size]) both cause the same problem in the class and also in a function ... yet if i put the same code in main it works fine
|
|
|
|
|
|
#5 |
|
Member
Join Date: Jun 2001
Posts: 1,181
|
You need to flush the buffer for your class.
cin.ignore(); or consequently munch through each character till you get to the newline using cin.get() but cin.ignore() should fix it. |
|
|
|
|
|
#6 |
|
Member
Join Date: Jun 2001
Location: Melbourne
Posts: 259
|
*looks at people above him funny*
you should be using Code:
std::string a; std::getline(std::cin, a); Code:
std::cin.ignore(std::numeric_limits<std::streamsize>::infinity(), '\n'); Code:
while(cin.get() != '\n'); // or cin.ignore(10000, '\n'); feel free to post your code so we can look and debug/help
|
|
|
|
|
|
#7 |
|
Member
Join Date: Jan 2002
Location: Brisbane, QLD
Posts: 46
|
thanks everyone .. but the problem is solved .. and btw i didnt was to ignore the input .. i wanted to get it .. but thanks anyway
also as for the one 2 posts above, get also fails the only working one was read() .. but all fixed now std::cin.sync() fixed it all ![]() thanks all |
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|