cin.get 的问题

Problems with cin.get

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

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

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(如 tag/edit 建议的那样):

如果替换

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

我认为您应该按如下所述修改代码:

#include <iostream>
using namespace std;

#define NUM_ELEMS 3
#define BUF_LEN 10

main()
{
  char s[NUM_ELEMS][BUF_LEN];

  int i = 0;
  while (i < NUM_ELEMS && !cin.fail()) {
    cout << "Enter some text: ";
    cin.getline(s[i], BUF_LEN);
    cout << "You entered '"<< s[i] << "'" << endl;  
    i++;
  }
}