Migdotcom Migdotcom - 11 months ago 106
C++ Question

How to add a string into a vector in a switch case menu in C++

The task I have is to make a vector of strings, add strings to it, and delete strings from it. I am having trouble with adding strings, based on the book I am reading (Beginning C++ through game programming)

//gaming Queue

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
int choice;
string input;
bool menu = true;
vector<string> favGames;
while (menu){
cout <<"Welcome to the favorite game queue please add your favorite games:\n";
cout <<"1-Add a favorite game.\n";
cout <<"2-List of your favorite games.\n";
cout <<"3-Remove a game.\n";
cin >> choice;
switch (choice)
{
case 1:
cout << "Please add a favorite game to the queue: \n";
string input;
cin >> input;
favGames.push_back(input);// Here is my problem it just jumps to case 2 and shows an error
break;
case 2:
cout << "Here is a list of your favorite games.\n";
break;
case 3:
cout << "No longer like a game which game should we remove?\n";
break;

default:
cout << "You made an illegal choice.\n";
}
}
return 0;
}

Answer Source

When compiling your code on https://www.onlinegdb.com/online_c++_compiler, it shows me those error:

main.cpp:34:18: error: jump to case label [-fpermissive]
         case 2: 
              ^
main.cpp:30:24: note:   crosses initialization of 'std::string input'
             string input;

as you can see the compiler tells you that you're skipping the initialization of string input. At the same time you are declaring input a second time. By removing input inside the switch case, the program compiles and works as intended.

EDIT:

You can't enter more than one word using cin because cin extraction always considers spaces (whitespaces, tabs, new-line...) as terminating the value being extracted.

So you have to use getline. Same for when getting choice.

here is the full code:

#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    int choice;
    string input;
    bool menu = true;
    vector<string> favGames;
    while (menu) {
        cout << "Welcome to the favorite game queue please add your favorite games:\n";
        cout << "1-Add a favorite game.\n";
        cout << "2-List of your favorite games.\n";
        cout << "3-Remove a game.\n";

        string choiceStr;
        getline(cin, choiceStr);
        stringstream(choiceStr) >> choice;

        switch (choice)
        {
        case 1:
            cout << "Please add a favorite game to the queue: \n";
            getline(cin, input);
            favGames.push_back(input);
            break;
        case 2:
            cout << "Here is a list of your favorite games.\n";
            break;
        case 3:
            cout << "No longer like a game which game should we remove?\n";
            break;

        default:
            cout << "You made an illegal choice.\n";
        }
    }
    return 0;
}

The reason why it enters infinite loop:

The cin >> input can only work when you enter only one word. If you enter several words, the cin >> input will catch the first one then the cin >> choice will catch the next one. If the input catch by cin >> choice is not an int, the cin will fail which makes you an infinite loop in this case. It is explained here http://www.cplusplus.com/doc/tutorial/basic_io/.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download