Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Ending a Loop Accepting Strings
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zampano




PostPosted: 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
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: 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




PostPosted: Sun Mar 30, 2008 8:48 pm   Post subject: Re: Ending a Loop Accepting Strings

I'm using GCC.
Saad




PostPosted: 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;
}
md




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Saad




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: