Thread: getline, while, string problems

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    Exclamation getline, while, string problems

    k so i made a simple password program for my computer programing class
    (if you really want to see it ask and ill put it up, it works fine)
    for ec i can make a program with multiple users but i keep getting this dumb problem

    whenever i have something like this
    Code:
    do
              {
                    cout << "Please enter your name first dude\n";
                    getline (cin,user1);
                    cout << "hhm sounds good can you repeat that please\n";
                    getline (cin,user_check);
                    if (user1!=user_check)
                    {
                          cout << "hhmm nope try again please\n";
                    }
              } while (user1!=user_check);
    pretty simple stuff it works fine but when i do this

    Code:
    if (number_users==3)
        {
              do
              {
                    cout << "Please enter your name first dude\n";
                    getline (cin,user1);
                    cout << "hhm sounds good can you repeat that please\n";
                    getline (cin,user_check);
                    if (user1!=user_check)
                    {
                          cout << "hhmm nope try again please\n";
                    }
              } while (user1!=user_check);
        }
    (my indentation got messed up when i pasted)
    and number_users is not a constant but a user input number it runs through and shows
    Code:
    Please enter your name firs dude
    hhm sounds good can you repeat that please
    the first time through, it doesnt let me input anything,
    but when number_users is a constat that =3 its fine?
    so whats going on here?
    becosue this is agrevating. so what am i missing what dont i know that is causing this.

    help would be much appreciated. yup show this noob how this works please.
    i can do the rest by myself but this is the part that just wont come out right

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Perhaps a character is left in the input stream from when the user inputted "3".
    Try sticking an std::cin.ignore() before the first prompt.

    *edit* before the first prompt in your posted code *edit*
    Last edited by CodeMonkey; 09-22-2007 at 10:50 PM. Reason: grammar
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    hhmm still need help can someone explain this like you would to a noob?
    becouse well i am

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Well, try what I said and see what it does. Then let us know.
    Code:
    if (number_users==3)
        {
              do
              {
                    cout << "Please enter your name first dude\n";
                    cin.ignore();  //http://www.cplusplus.com/reference/iostream/istream/ignore.html
                    getline (cin,user1);
                    cout << "hhm sounds good can you repeat that please\n";
                    getline (cin,user_check);
                    if (user1!=user_check)
                    {
                          cout << "hhmm nope try again please\n";
                    }
              } while (user1!=user_check);
        }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't put the ignore there because then it will cause a problem after the loop has run once.

    cin.ignore() should be added after calls to cin >>. You can add one after every call to cin >> to be safe, or you can just do it after calls to cin >> that come directly before a call to getline. So you want something like this:
    Code:
        cin >> number_users;
        cin.ignore();
    
        if (number_users==3)
        {
              do
              {
                    cout << "Please enter your name first dude\n";
                    getline (cin,user1);
                    cout << "hhm sounds good can you repeat that please\n";
                    getline (cin,user_check);
                    if (user1!=user_check)
                    {
                          cout << "hhmm nope try again please\n";
                    }
              } while (user1!=user_check);
        }
    The reason this happens is that when the user types 3 and hits enter a '3' and a '\n' character are added to the input buffer (the '\n' is a newline from hitting enter). When using cin >> the '\n' is not read. When you then use getline it sees that '\n' and immediately stops reading, since its job is to read until it sees a '\n' character. Adding the ignore() after the cin >> call ignores that newline.

    The reason you don't do it inside the loop is because unlike cin >>, getline does read and ignore the newline automatically. So the first time through the loop things will be fine, but the second time it will call cin.ignore() and there will be no newline to ignore. Instead it will end up ignoring the first letter of the second dude's first name. The same will happen the third time around.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Whoops.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    Yayyyy it worked thank you.
    now i wonder......sorry for all the bother but
    I now there is a way but how do i make it so that when the user imputs their password only *** are shown?? and is there any possible way for me to do and understand it at my current computer programing lv.
    here is my old password program
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string password1, password2, password_check;
        string user, user_check;
        int count, imposter;
        count=4; imposter=20;
        cout << "Hey there little dude whats your name\n";
        getline (cin,user);
        do
        {
                cout << "Please repeat that\n";
                getline (cin,user_check);
                if (user_check!=user)
                {
                      cout << "Whoops try again pleasie\n";
                      getline (cin,user);
                }
        }while (user!=user_check);
        do
        {
               cout << "oky doky Mr. " <<user<< " enter your password\n";
               getline (cin,password1);
               cout << "hhmmm can you repeat that please\n";
               getline (cin,password_check);
               if (password_check!=password1)
               {
                     cout << "whoopsy try again please\n";
                     cout << "\n";
               }
        }while (password1!=password_check);
        
        while (count!=0)
        {
               cout << "Yay\n";
               cout << "Please enter your password to get in\n";
               getline (cin,password2);
               if (password2==password_check)
               {
                     break;
               }
               else
               {
                     cout << "hhmm nope wrong try again\n";
                     count--;
               }
        }
        
        if (password2==password_check)
        {
              cout << "welcome aboard\n";
        }
        if (count==0)
        {
              while (imposter!=0)
              {
                    cout << "Imposter\n\a";
                    imposter--;
              }
              cout << "Go Away\n";
        }
        
        cin.get(); cin.get();
        
        return 0;
    }
    if you wanna check go ahead it works
    ya this part is my homework im just wondering if i could get the asteriks to work as a little extra thing. plus it will be cool to know how to manipulate what the user sees.
    Last edited by Makachu; 09-23-2007 at 03:39 PM. Reason: i wonder

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I now there is a way but how do i make it so that when the user imputs their password only *** are shown??

    There is no real way in standard C++. There are special functions on different platforms that help you do this (like maybe getch()), but you have to be careful when mixing the platform specific functions and standard stuff like cin.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. problems with string manipulation
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 04:45 PM