Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 18th August 2002, 11:32 PM   #1
tvjames Thread Starter
Member
 
Join Date: Jan 2002
Location: Brisbane, QLD
Posts: 46
Default Damn C++ iostream

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
tvjames is offline   Reply With Quote

Join OCAU to remove this ad!
Old 19th August 2002, 1:07 AM   #2
Geo
Member
 
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
Default

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
Geo is offline   Reply With Quote
Old 19th August 2002, 12:45 PM   #3
Boney
Member
 
Boney's Avatar
 
Join Date: Jun 2001
Location: Westside, Ippaswitch
Posts: 913
Default

You probably need to clear your input buffer cin.clear()? before attempting to read user input.
__________________
Well helloooo Mr. Fancy Pants!
PCDB Main PC Internet Gateway / File Server
Boney is offline   Reply With Quote
Old 19th August 2002, 2:12 PM   #4
tvjames Thread Starter
Member
 
Join Date: Jan 2002
Location: Brisbane, QLD
Posts: 46
Default

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
tvjames is offline   Reply With Quote
Old 19th August 2002, 2:46 PM   #5
almghty
Member
 
Join Date: Jun 2001
Posts: 1,181
Default

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.
almghty is offline   Reply With Quote
Old 19th August 2002, 7:15 PM   #6
ocean
Member
 
ocean's Avatar
 
Join Date: Jun 2001
Location: Melbourne
Posts: 259
Default

*looks at people above him funny*

you should be using

Code:
std::string a;
std::getline(std::cin, a);
to ignore all characters upto and including the next '\n' on the input stream do

Code:
std::cin.ignore(std::numeric_limits<std::streamsize>::infinity(), '\n');
or, more readably, but not object oriented

Code:
while(cin.get() != '\n');
// or
cin.ignore(10000, '\n');
note: the above examples will *wait* for a \n if none exists on the stream.

feel free to post your code so we can look and debug/help
ocean is offline   Reply With Quote
Old 20th August 2002, 4:32 PM   #7
tvjames Thread Starter
Member
 
Join Date: Jan 2002
Location: Brisbane, QLD
Posts: 46
Default

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
tvjames is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

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


All times are GMT +10. The time now is 8:47 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!