如何在 C++ 的开关案例菜单中将字符串添加到向量中

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

我的任务是制作一个字符串向量,向其追加字符串,并从中删除字符串。我在追加时遇到问题 strings.I 也设置了它,以便切换案例提供在案例 1 中追加队列的选项。

//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;

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

https://www.onlinegdb.com/online_c++_compiler 上编译您的代码时,它向我显示了这些错误:

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;

如您所见,编译器告诉您您正在跳过 string input 的初始化。同时,您第二次声明 input。 通过删除 switch case 中的 input ,程序可以按预期编译和工作。

编辑:

您不能使用 cin 输入多个单词,因为 cin 提取始终将空格(空格、制表符、换行符...)视为提取值的终止。

所以你必须使用getline。获取 choice.

时相同

完整代码如下:

#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;
}

进入死循环的原因:

cin >> input只能在只输入一个字的情况下起作用。如果您输入多个单词,cin >> input 将捕获第一个单词,然后 cin >> choice 将捕获下一个单词。如果 cin >> choice 的输入 catch 不是 int,则 cin 将失败,这使您在这种情况下陷入无限循环。 这里有说明 http://www.cplusplus.com/doc/tutorial/basic_io/.

switch 语句有点奇怪,因为您不能在 case 子句中声明变量,除非您使用 {} 块为它们创建范围。

   switch (choice)
   {
         case 1:    
         { // start a scope  
            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;
         } // end the scope

但是在您的情况下,您已经在 switch 之外定义了一个 std::string,您打算使用它吗?然后你可以删除 case 子句中的那个。