
-----------------------------------
Zampano
Sun Mar 30, 2008 3:35 pm

Ending a Loop Accepting Strings
-----------------------------------
I'm using
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?

-----------------------------------
OneOffDriveByPoster
Sun Mar 30, 2008 4:06 pm

Re: Ending a Loop Accepting Strings
-----------------------------------
What compiler are you using?  I am not having this problem (MSVC Express 2005).

-----------------------------------
Zampano
Sun Mar 30, 2008 8:48 pm

Re: Ending a Loop Accepting Strings
-----------------------------------
I'm using GCC.

-----------------------------------
Saad
Sun Mar 30, 2008 8:59 pm

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


#include 
#include 

int main(){
    while (true){
        std::string foo;
        std::cin >> foo;
        if (foo == "exit"){
            break;
        }
    }
    return 0;
}

-----------------------------------
md
Sun Mar 30, 2008 10:21 pm

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
Sun Mar 30, 2008 11:20 pm

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
Mon Mar 31, 2008 10:50 am

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
Mon Mar 31, 2008 4:58 pm

Re: 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.

Isn't it cin.get() or something? cin.ignore?

-----------------------------------
Saad
Mon Mar 31, 2008 5:14 pm

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.

Isn't it cin.get() or something? cin.ignore?


while (std::cin.peek() != '\n'){
    std::cin.ignore();
}


The following should be sufficient to flush the input buffer.
