Thread: Looping trouble - Spitting out too many lines at once

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    5

    Looping trouble - Spitting out too many lines at once

    I've started learning C++ about one week ago and have now "finally" run into a problem I can't figure out on my own.

    The code bellow is supposed to let you enter data for one character (D&D/NWN-style), print it to a file and then allow the user to add another character without having to restart the console.

    It runs pretty much fine the first time through, but the second time it prints the character name and gender lines to the screen without pausing, thereby only letting you enter gender.
    Same for class and level.

    I tried adding cout << endl; which didn't do anything at all and cin.get() in several places.
    The letter works for int variables but sends strings (which Name and Class are) into an infinite loop of some sort.

    I've used the exact same loop in another program where it did, and still does, work fine.
    I spent the last couple of hours trying to fix this, looked at similar code from other people, even rewrote the things several tims just in case.
    Now I'm hoping that maybe someone here can open my eyes.

    In case it matters: I'm using Win XP Pro SP2, Dev C++ 4.9.9.2 and it's a simple console application.


    Well, here's the code:
    (Ignore the comments, I suck at commenting)

    Code:
    while ( answer == 'y')
        {
              // Starting user Input
              cout << "What is your character's name?\n)";
              getline (cin, Name);
              
              cout << "Which gender is " <<Name << " (m/f)?\n";
              cin >> Gender;
              
              cout << "How many classes does " << Name << " have?\n";
              cin >> classes;
              
        
              //Counting and saving classes and levels
              for (i=0; i<classes; i++)
              {
                  cout << "Enter class number " << i+1 << ": ";
                  cin >> Class [i];
                  cout << "Enter number of levels in this class: " ;
                  cin >> Level [i];
               }
               
     
     
              CharOrg << "Name: " << Name << "\nGender: " << Gender << "\nClass: \n"; 
              for (i=0; i<classes; i++)
              {
                 Class [i];
                 CharOrg << Class [i] << " (" << Level [i] << "), " ; 
              }
       
              cout << endl;         
              cout << " Do you wish to enter another character (y/n)? ";
              cin >> answer;
             
         } // End while

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This is a common problem resulting from the use of operator>> and getline on the same stream. Basically, when you ask "Do you wish to enter another character (y/n)?" the user types 'y' and hits <enter>. The code reads in the 'y' but leaves the newline from the <enter>. The loop continues and use ask "What is your character's name?" again. This time, the newline from the previous answer is still in the stream. Since getline stops at the first newline, it reads that and does not get any input from the user.

    The simple solution is to add a cin.ignore() after a call to cin >>. It is generally safe to do this after each call to cin >>, since it has no affect if another cin >> call comes next. Do not add the cin.ignore() after calls to getline, since getline automatically ignores that trailing newline.

    BTW, excellently asked question.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    5
    Ow, and I even knew about the >> thing, didn't even think to try cin.ignore()

    Thanks, Daved, it's working fine now.
    No wonder I couldn't find a solution though, since I was looking for the wrong problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Simple answer for intersecting lines?
    By LLINE in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2008, 02:10 AM
  3. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  4. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 08:56 PM
  5. Printing 20 lines at a time
    By csmatheng in forum C Programming
    Replies: 5
    Last Post: 04-30-2002, 04:11 PM