Thread: How do I stop my loop from skipping getline

  1. #1
    Registered User
    Join Date
    Aug 2011

    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:

    #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";
              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;
        return EXIT_SUCCESS;

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    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
    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-29-2011, 12:37 AM
  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, 02: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, 02:41 PM
  5. Skipping a loop?
    By axon in forum C++ Programming
    Replies: 0
    Last Post: 02-16-2003, 07:52 PM