How do I stop my loop from skipping getline

This is a discussion on How do I stop my loop from skipping getline within the C++ Programming forums, part of the General Programming Boards category; I'm trying to do an exercise on vectors, and iterators. I tried searching on google but not much luck into ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    How do I stop my loop from skipping getline

    I'm trying to do an exercise on vectors, and iterators. I tried searching on google but not much luck into my specific problem.

    "Write a program using vectors and iterators that allows a user to maintain a list of his or her favorite games. The program should allow the user to list all game titles, add a game title, and remove a game title."

    When I enter in y or Y it starts the loop over, but skips over my getline statement. Is it something still being stored in the input buffer? If so, how do I rectify that, and where is the source of the problem?

    Here's the code so far:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
        vector<string> GAME_TITLE_VECTOR;
        vector<string>::iterator i;
        string GAME_TITLE;
        char AGAIN = 'y';
        
        cout << "\tWelcome to the favorite video game program!" << endl;
        cout << "\tThis program stores your video game into what's called a vector\n\tand shows what you've entered in so far!" << endl;
        
        cout << endl;
        while ((AGAIN == 'y') || (AGAIN == 'Y'))
        {
        
              cout << "\n\nEnter one of your favorite video games!\n";
              getline (cin,GAME_TITLE);
        
              cout << "So far, you've entered:\n";
        
              GAME_TITLE_VECTOR.push_back(GAME_TITLE);
        
              for (i = GAME_TITLE_VECTOR.begin(); i != GAME_TITLE_VECTOR.end(); ++i)
              {
                   cout << *i << endl;
              }
              cout << "\nWould you like to enter another title? ";
              cin >> AGAIN;
        }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,828
    Put a cin.ignore() after the cin >> AGAIN.
    And btw, it's usually considered bad practice to use upper case for variables (it's usually reserved for macros).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Thanks, that fixed the problem. And thank you for the tip as well. I'll make a note of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file with getline and skipping lines..
    By kocmohabt33 in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2011, 11:37 PM
  2. Program skipping while loop
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 03:30 AM
  3. 2 for loops...dunno why its skipping loop 2
    By Axolotl in forum C Programming
    Replies: 3
    Last Post: 04-03-2003, 01:07 PM
  4. How to tell getline to stop at a specific string...
    By GGrrl in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2003, 01:41 PM
  5. Skipping a loop?
    By axon in forum C++ Programming
    Replies: 0
    Last Post: 02-16-2003, 06:52 PM

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