c++ - cin.get 的问题

标签 c++ string cin

当我的这部分代码运行时,它会立即弹出到控制台中,而且我没有机会像 cin 那样为 cin.get 输入数据。 我需要将 cin.get 更改为什么,以便它在每次 cout 后停止并让我输入数据。数据需要能够包含空格、字母和数字,如果有帮助,我只需要能够有 1 个空格。 感谢大家的帮助

编辑:我将所有 cin.get 更改为 cin,并将所有 char qx[10] 更改为字符串 qx。如果有人知道如何防止输入空格,那么它也可以解决我的问题。

int max = 10;
        int randomnumber;

        srand(time(0));
        randomnumber = (rand() % max) + 1;
        cout << randomnumber;

        if (randomnumber == 1)
        {
            char q1[10];
            char q2[10];
            char q3[10];
            char q4[10];
            char q5[10];
            char q6[10];
            char q7[10];
            char q8[10];
            char q9[10];
            char q10[10];

            system("CLS");
            cout << "Quiz Version : A" << endl;
            cout << "sdsdfg:";
            cin.get(q1, 10);
            cout << endl <<"sdfg:";
            cin.get(q2, 10);
            cout << endl << "asdf:";
            cin.get(q3, 10);
            cout << endl << "sdsdfg:";
            cin.get(q4, 10);
            cout << endl << "sdfgsdfg:";
            cin.get(q5, 10);
            cout << endl << "dfgdf:";
            cin.get(q6, 10);
            cout << endl << "zxcvzxc:";
            cin.get(q7, 10);
            cout << endl << "erteetrre:";
            cin.get(q8, 10);
            cout << endl << "dsfgasdgf:";
            cin.get(q9, 10);
            cout << endl << "xvcbyht:";
            cin.get(q10, 10);
        }
        else if (randomnumber == 2)
        {
            char q1[10];
            char q2[10];
            char q3[10];
            char q4[10];
            char q5[10];
            char q6[10];
            char q7[10];
            char q8[10];
            char q9[10];
            char q10[10];

            system("CLS");
            cout << "Quiz Version : B" << endl;
            cout << "sdsdfg:";
            cin.get(q1, 10);
            cout << endl << "sdfg:";
            cin.get(q2, 10);
            cout << endl << "asdf:";
            cin.get(q3, 10);
            cout << endl << "sdsdfg:";
            cin.get(q4, 10);
            cout << endl << "sdfgsdfg:";
            cin.get(q5, 10);
            cout << endl << "dfgdf:";
            cin.get(q6, 10);
            cout << endl << "zxcvzxc:";
            cin.get(q7, 10);
            cout << endl << "erteetrre:";
            cin.get(q8, 10);
            cout << endl << "dsfgasdgf:";
            cin.get(q9, 10);
            cout << endl << "xvcbyht:";
            cin.get(q10, 10);
        }

最佳答案

假设您可以使用 std::string(如标签/编辑建议的那样):

如果你更换

char q1[10];
//...
char q10[10];

string q1;
//...
string q10;

然后您可以使用 std::getline 可靠地获取用户输入:

cout << "Quiz Version : A" << endl;
cout << "sdsdfg:";
getline(cin, q1); //Gets user input from cin and places it in q1

关于c++ - cin.get 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34112287/

相关文章:

c++ - boost::range::join 一个自定义调用中的多个范围

java - 我如何以最佳方式使用 StringBuilder 的 toString 方法

string - 使用 IFS 在 bash 中将字符串拆分为单个字符

java - 解析并捕获字符串末尾的数字

c++ - std::cin while 循环给出了一个奇怪的结果

c++ - Boggle - 实现递归

c++ - 在 Linux 上使用 C++ 访问 Sony Playstation SixAxis Controller

c++ - 如何创建一个窗口,当在窗口客户区按住鼠标指针时我们可以移动窗口?

c++ - C++中数组的大小是否灵活?

c++ - 如何用空格 cin 整个句子