Thread: Input

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    Input

    I'm new at C++ code and don't understand how to get a program to take a full line of input from the user. I was looking in the tutorial, but it only explains how to use variables to accept one character or integer, as opposed to taking a full word. Please help.

    EDIT- I apologize. I found it.
    Last edited by Foley; 08-27-2006 at 11:29 AM. Reason: Never mind.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    C++ has a library called <string>. the data type string can hold any number of characters. eg,
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s = "Hello, World!";
        cout << s;
    }
    To get a full line of text from the user, to be stored in a string, use the getline() function.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s;
        cout << "Enter input: \n";
        getline(cin, s);
        cout << "User inputted: " << s << endl;
    }

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    One more question: How can I make an If with a string?

    ie, I want to a certain thing to happen if the user enters a certain string.

    Thanks.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You can make an if with a C++ string the same way as with any other type, eg,
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        cout << "Would you like to hear a joke?" << endl;
        string response;
        getline(cin, response);
        if ( "yes" == response )
            cout << "Two Parrots are sat on a perch.  One says to the other "
                 << "\"Can you smell fish?\"" << endl;
        cout << "Goodbye";
    }
    Just be careful that strings, like char's are case-sensitive. ie, "yes" is not the same as "YES"

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Thank you guys SO much. I'm working on a text based game and this is a great help.

  6. #6
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by Foley
    ie, I want to a certain thing to happen if the user enters a certain string.
    It's easy if you want an exact match. Just compare the string variable with a quoted string.
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    
    int main()
    {
        std::string command;
    
        cout << "Do you want a <greeting> or do you want to <quit>: ";
    
        if (cin >> command)
        {
            if (command == "greeting")
            {
                cout << "Hello!\n";
            }
            else if (command == "quit")
            {
                cout << "Goodbye\n";
            }
            else
            {
                cout << "I don't understand\n";
            }
        }
    
        return 0;
    }
    It's probably better to search for a substring though, that way if you type in some spaces or something, the messages still work.
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::string;
    
    int main()
    {
        string command;
    
        cout << "Do you want a <greeting> or do you want to <quit>: ";
    
        if (getline(std::cin, command))
        {
            if (command.find("greeting") != string::npos)
            {
                cout << "Hello!\n";
            }
            else if (command.find("quit") != string::npos)
            {
                cout << "Goodbye\n";
            }
            else
            {
                cout << "I don't understand\n";
            }
        }
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Okay, one more question. I'm having a little trouble with loops.

    I want to loop an effect that says if something happens, something else happens, ie:

    Code:
        while (z==1){
    
        string command;
    
        getline(cin, command);
    
              if ( "attribute"||"att" == command) {
    
                   cout<<"Your current attributes are:\n";
    
                   cout<<"Strength: "<<s<<"\n";
    
                   cout<<"Dexterity: "<<d<<"\n";
    
                   cout<<"Intelligence: "<<i<<"\n";
    
                   cout<<"Wisdom: "<<w<<"\n";}
                   };
    This is an excert from the game I'm making. I have z set to always equal 1. The problem is that I want it to loop that while z=1, if you type attribute or att you will see the attributes. But it loops that no matter what you type it shows the attributes. I don't understand why it's doing this. Please help.
    Last edited by Foley; 08-27-2006 at 06:59 PM.

  8. #8
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by Foley
    if ( "attribute"||"att" == command) {
    That's the problem, and I think it's because the left side of the || expression is saying "do this if the string isn't a null pointer". I'm sure that a quoted string will never be null, so the if always returns true because || expressions are short circuited. It returns as soon as a true result is found. This is what you want instead, I think.
    Code:
    if (command == "attribute" || command == "att")

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You have two problems...
    First, if ( "attribute"||"att" == command) doesn't work. Without entering into much detail about lvalues and rvalues, how you should create these type of conditions is:

    if ( "attribute" == command || "att" == command)


    The second problem is:
    When z == 1 you enter a loop. That loop first reads your input. If the input is either "attriute" or "att", it will display the current attributes.

    Now... What happens next? When it finishes displaying the attributes, z will still be == 1. So the loop will repeat from the start. It will wait again for an input from you. If you don't input "attribute" or "att", the if statement will return false. So no attributes will be displayed and once again z was unchanged. Which means once again it will go to the beggining of the loop and wait for another input from you.

    Without more info of what z means, I have little advice to give you. But the main thing here is that your loop is being controlled by a variable that is never changing. So you entered an infinite loop. You need to either change z inside the loop or reevaluate your code and ask yourself if you really need that loop. Maybe this will be enough:
    Code:
        string command;
    
        getline(cin, command);
    
              if ( 1 == z && "attribute" == command || "att" == command) {
    
                   cout<<"Your current attributes are:\n";
    
                   cout<<"Strength: "<<s<<"\n";
    
                   cout<<"Dexterity: "<<d<<"\n";
    
                   cout<<"Intelligence: "<<i<<"\n";
    
                   cout<<"Wisdom: "<<w<<"\n";}
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    The problem is that I want it to loop that if that is the command, then the attributes show, and otherwise the program continues as normal. Instead, it loops that if it doesn't get the command, it waits for it. I want the loop to be there, but in the background.
    Last edited by Foley; 08-29-2006 at 12:28 PM.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just be careful with the substring thing... take 'quit' for example... say somebody says
    "why dont' you quit your dayjob?"
    that code will exit them from the program

    or what about
    "quit greeting me like that!"

    is that a response to your greeting, or is it them wanting to quit? or something else?

    this is stretching slightly into AI concepts, and if you keep it to old-school Mud style you shouldn't have a problem with it. Just do a google search for "Mud" and you'll see that nerds are more powerful than wet pools of dirt... so HA!

    menus are very nice too
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Okay, I tried something a little different to see if I could get what I want.

    Code:
    while (z==1) {
        string command;
        getline(cin, command);
                if ( 1 == z && "attribute" == command || "att" == command) {
                  cout<<"Your current attributes are:\n";
                  cout<<"Strength: "<<s<<"\n";
                  cout<<"Dexterity: "<<d<<"\n";
                  cout<<"Intelligence: "<<i<<"\n";
                  cout<<"Wisdom: "<<w<<"\n";}
                else {
    The object was that if you typed attribute or att you got your attributes, and if you put anything else, you get the other text. Everything else was behind "else". And the program did not appriciate it. It gave me an endless stream of everything else in the game instead of the attributes- and when I went up top looked, the attributes had never even been displayed. How in the world did that happen?

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Watch your precedence. I think you might want to change this:
    Code:
    if ( 1 == z && "attribute" == command || "att" == command) {
    ->
    Code:
    if ( 1 == z && ( "attribute" == command || "att" == command ) ) {
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM