String error

This is a discussion on String error within the C++ Programming forums, part of the General Programming Boards category; When ever I use a string variable in my program, if I type a space at the cin statement, the ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    String error

    When ever I use a string variable in my program, if I type a space at the cin statement, the program interprets the space as a break between input and takes each word as a separate input, so the string "a b" is considered two strings, "a" and "b". What is wrong?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Nothing is wrong. That's how the >> operator works.
    Use std::getline() if you want to get a whole line of input.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    What is the syntax for that?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Tried getline ate the name selector part of my program, but the input is counted as just a . and I can't actually input anything there. Syntax used was:

    Code:
    getline( cin, yourname);
    like on the supplied website.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DarkAlex View Post
    Tried getline ate the name selector part of my program, but the input is counted as just a . and I can't actually input anything there. Syntax used was:

    Code:
    getline( cin, yourname);
    like on the supplied website.
    You have to type more than "." to get something else.

    Serious comment: That's the correct statement, therefore your error is somewhere else. Keep looking. If you want us to help look, you'll have to actually post the code.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    This is the code where I tested the new input statement:

    Code:
    int play()
    {
        bool ok=false;
        
        beaten=0;
        hp=10;
        maxhp=10;
        weapon=0;
        gold=5;
        frags=0;
        
        do
        {
                cout<<"What is your charater's name?\n";
                getline( cin, yourname);
                cout<<"Your character's name is "<<yourname<<". Is that okay?\n";
                cin>>choice;
                if (choice=="No" || choice=="no" || choice=="NO" || choice=="n" || choice=="N")
                {
                       ok=false;
                }
                else
                {
                    ok=true;
                }
        }while (ok==false);
        startroom();
        
        if (hp==0)
        {
                  cout<<"Ye dead. Better luck next time!\n";
                  system ("pause");
        }
    }
    yourname is a string, and since this is the only code block I tried the new method in, this has to be the area where the error is occurring.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ah. We seem to have the dreaded "mixing >> and getline" syndrome. It is theoretically possible to write a program that uses both successfully, if you are very careful. Rough guideline: every time you read input using >> and the next input operation will be/may be done with getline, you will need to call cin.ignore() to remove the enter key, which >> does not process and therefore interferes with getline.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Thanks. That fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21