Thread: How do I...

  1. #1
    C++ and openGL Raeliean's Avatar
    Join Date
    Jul 2005
    Posts
    28

    How do I...

    So I'm still just at the very beginning of learning c++.. when making a program that says something like

    Code:
    if (value == 0) {
          break;}
    Thats doable with integers but how do I do it with words so when the user inputs the word "close" it breaks? I don't understand how to use words in it... any help?

    Thanks in advance!
    Be inspired.

  2. #2
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    simple where ever you defin int change it to char ex.

    Code:
    char value
    char wateva
    
    cin>> value; //user input goes here
    if ( value == wateva ) {
    break;
    }
    im a beginner too but is that what you want?

  3. #3
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    forgot the semicolons lol my bad

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Use the string class.
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string input;
        std::cout<<"Enter command: ";
        std::cin>>input;
        if(input =="close")
        {
            return 0;
        }
        std::cout<<"You didn't enter close"<<std::endl;
        return 0;
    }
    Woop?

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    string buffer;
    
    getline (cin, buffer); //or "cin >> buffer;"
    
    if (buffer == "closed")
       break;
    This is just a quick answer, but this is how you would do it: by using a string and testing whether the input is correct. Now you might want to do error checking by changing the buffer to all lower case to insure its not case sensitive.

    Check out the strings tutorials on this site.

    EDIT: Damn prog-bman beat me to it!

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by C+noob
    forgot the semicolons lol my bad
    You know you can edit your posts...

  7. #7
    C++ and openGL Raeliean's Avatar
    Join Date
    Jul 2005
    Posts
    28
    JoshR, just PM that to him, no need to post it. Thanks everyone, all those answers helped a bunch!

    (See, I'm just now learning strings, barely touching on it.)

    Thanks again!
    Be inspired.

  8. #8
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    Quote Originally Posted by prog-bman
    Use the string class.
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string input;
        std::cout<<"Enter command: ";
        std::cin>>input;
        if(input =="close")
        {
            return 0;
        }
        std::cout<<"You didn't enter close"<<std::endl;
        return 0;
    }
    might i add the using namespace std so you dont have to do std:: b4 cout string and cin and yes josh i know lol jus lazy :P

  9. #9
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by C+noob
    simple where ever you defin int change it to char ex.

    Code:
    char value
    char wateva
    
    cin>> value; //user input goes here
    if ( value == wateva ) {
    break;
    }
    im a beginner too but is that what you want?
    I believe he said "word", not character, so he'd either need to change the char into an array, or use a string.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by C+noob
    might i add the using namespace std so you dont have to do std:: b4 cout string and cin and yes josh i know lol jus lazy :P
    You can add the using directive if you want to, but that defeats the entire purpose of the namespace. It's usually not important in beginners' programs, but taking the easy/lazy way out is not a good habit to get into.

Popular pages Recent additions subscribe to a feed