当我的这部分代码运行时,它会立即弹出到控制台中,而且我没有机会像 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/