Computer Science Canada

Ending a Loop Accepting Strings

Author:  Zampano [ 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?

Author:  OneOffDriveByPoster [ 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).

Author:  Zampano [ Sun Mar 30, 2008 8:48 pm ]
Post subject:  Re: Ending a Loop Accepting Strings

I'm using GCC.

Author:  Saad [ 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

c++:

#include <iostream>
#include <string>

int main(){
    while (true){
        std::string foo;
        std::cin >> foo;
        if (foo == "exit"){
            break;
        }
    }
    return 0;
}

Author:  md [ 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.

Author:  OneOffDriveByPoster [ 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.

Author:  Zampano [ 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.

Author:  michaelp [ 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?

Author:  Saad [ 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.


Isn't it cin.get() or something? cin.ignore?


c++:
while (std::cin.peek() != '\n'){
    std::cin.ignore();
}


The following should be sufficient to flush the input buffer.


: