Program loops forever at do while loop

[Help]
Hi, I'm new to c++ programming. Really, really new. I am attempting to write a code that allows me to list all the games I have using iterators, and the program seems to work fine when I ask it to input numbers, but the second I add a character, the program loops forever. What am I doing wrong? The code is a mess to read, but I did my best to comment the steps I took. I mean to add more to this code like, adding a menu that lets you add a game, remove a game, or just quit, but I just want to understand why my int variable called elementPosition takes in characters. When I put it on the console all the letters are equal to zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    char StartOver;
    do
    {
        cout << "*********************************************************************" << endl;
        cout << "*********************************************************************" << endl;
        cout << "*********************************************************************" << endl;
        cout << "\n\n\t\t\t\tWelcome.\n\n\t\t            Video game list." << endl;

        vector<string> games; // create a vector variable of type string
        games.push_back("Chrono Trigger");    // add to the string vector list
        games.push_back("Metal Gear");
        games.push_back("Fallout");

        vector<string> :: iterator myGames;   // iterator named my games
        vector<string> :: const_iterator iter;// constant iterator named iter
        unsigned short int i;

        cout << "\n\nYour current list of games: \n" << endl;

        for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
        {
            cout << "\t" << i << ". " << *iter << endl; // list the number of game and the games
        }

        int elementPosition = 0; // So I can choose the element

        do
        {

        cout << "\nType the NUMBER of the game you want to trade: ";
        cin  >> elementPosition;           //user chooses  element
        cin.ignore(); // I think this prevents the compiler from ignoring content left in input buffer and skipping code

        if(elementPosition > games.size() || elementPosition < 1)
            {
                cout << "\n\t**Invalid choice.** \nPick a number from the list of games.\n" << endl;
            }
        }while(elementPosition > games.size() || elementPosition < 1); // Don't go above or below the vector size

        elementPosition -= 1; //This is to make sure a number below 1 is not chosen as an option

        cout << "\nWhat game are you trading " << games[elementPosition] << " for?\n";
        //elementPosition++; // increment element position in order to pick number 1 or higher on the list
        string name;         // The name of the game that will be traded
        getline(cin, name);  // Allows string to have more than one word

        myGames = games.begin() + elementPosition; // first vector element is assigned to myGames + the game I want to trade
        *myGames = name;

        cout << "\n\nThe list has been updated to: \n" << endl;
        for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
        {
            cout << i << ". " << *iter << endl; // list the number of game and the games
        }

        cout << "\nWhat game would you like to add to the list? ";
        getline(cin, name);
        myGames = games.insert(games.end(), name); //Choose where to add new game. I chose at the end

        cout << "\nThe list has been updated to: \n" << endl;

        for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
        {
            cout << i << ". " << *iter << endl; // list the number of game and the games
        }

        cout << "\nType the NUMBER of the game you getting rid of? "; // game to erase from list

        int gameToErase;
        cin >> gameToErase;
        gameToErase -= 1; // compiler starts counting at 0, so 1 is actually the second element
        games.erase((games.begin() + gameToErase));

        cout << "\nThe list  has been updated to: \n" << endl;
        for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
        {
            cout << i << ". " << *iter << endl; // list the number of game and the games
        }

        cout << "Do you wish to Start the list over? (y = yes, and n for no) ";
        cin >> StartOver;
    }while(StartOver == 'y'); // start the list over in yes is pressed

    return 0;
}
Last edited on
Nuh uh bro. You're doing quite well for a first program. Believe it or not, you actually aren't doing anything wrong. Your program just can't handle character values.

If you say

1
2
int stuff;
cin >> stuff;


cin is expecting an integer value. If you don't give it an integer value, the program just isn't capable of handling that sort of input. Cin really only expects integers. You are giving it a char. Cin doesn't know how to handle that. Cin breaks. You get an infinite loop.

For the record, writing code that handles all types of inputs (whether it's char or int or string) can be somewhat challenging task.

I think this will work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  do
        {

        cout << "\nType the NUMBER of the game you want to trade: ";
        cin  >> elementPosition;           //user chooses  element
        cin.ignore(); // I think this prevents the compiler from ignoring content left in input buffer and skipping code
        cin.clear(); // sorta resets the cin
       

        if(elementPosition > games.size() || elementPosition < 1)
            {
                cout << "\n\t**Invalid choice.** \nPick a number from the list of games.\n" << endl;
            }
        }while(elementPosition > games.size() || elementPosition < 1);

Last edited on
Topic archived. No new replies allowed.