Posted: Sun Mar 30, 2008 3:35 pm Post subject: Ending a Loop Accepting Strings
I'm using
code:
while (cin >> x)
When "x" was an integer it didn't cause much trouble because I could always enter letters to terminate the while but how should I do the same when "x" is a string. It accepts all characters. I've tried Ctrl+Z but the error message: "....exe encountered an error and needs to close. Please send an error report to Microsoft . . ." always comes up. Is there a safe way to terminate the while?
Sponsor Sponsor
OneOffDriveByPoster
Posted: Sun Mar 30, 2008 4:06 pm Post subject: Re: Ending a Loop Accepting Strings
What compiler are you using? I am not having this problem (MSVC Express 2005).
Zampano
Posted: Sun Mar 30, 2008 8:48 pm Post subject: Re: Ending a Loop Accepting Strings
I'm using GCC.
Saad
Posted: Sun Mar 30, 2008 8:59 pm Post subject: Re: Ending a Loop Accepting Strings
Sorry, I'm a little confused with your question.
What do you mean by terminate when "x" is a string.
If you want it to terminate based on a certain word then all you need to use is break
Posted: Sun Mar 30, 2008 10:21 pm Post subject: RE:Ending a Loop Accepting Strings
I think what he means is that he's trying to read an integer and if instead the user enters a string it crashes.
You need to clear the input buffer - exactly how I forget. I believe I have posted it before if you care to search.
OneOffDriveByPoster
Posted: Sun Mar 30, 2008 11:20 pm Post subject: Re: Ending a Loop Accepting Strings
I am pretty sure that the OP is saying that when he is reading integers he can use the behaviour cause by entering some non-number to exit the program.
What he wants is a way to cause EOF.
Zampano
Posted: Mon Mar 31, 2008 10:50 am Post subject: Re: Ending a Loop Accepting Strings
I'm not reading integers. The problems was with finding an exit when reading srtings. The problem I didn't know about break before, I think it will do well.
michaelp
Posted: Mon Mar 31, 2008 4:58 pm Post subject: Re: RE:Ending a Loop Accepting Strings
md @ Sun Mar 30, 2008 10:21 pm wrote:
I think what he means is that he's trying to read an integer and if instead the user enters a string it crashes.
You need to clear the input buffer - exactly how I forget. I believe I have posted it before if you care to search.
Isn't it cin.get() or something? cin.ignore?
Sponsor Sponsor
Saad
Posted: Mon Mar 31, 2008 5:14 pm Post subject: Re: Ending a Loop Accepting Strings
michaelp wrote:
md @ Sun Mar 30, 2008 10:21 pm wrote:
I think what he means is that he's trying to read an integer and if instead the user enters a string it crashes.
You need to clear the input buffer - exactly how I forget. I believe I have posted it before if you care to search.