当我运行这部分代码时,它会立即弹出到控制台中,并且不会让我有机会输入cin.get的数据,就像它会cin一样。 我需要将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);
}
答案 0 :(得分:0)
假设您可以使用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
答案 1 :(得分:0)
我认为你应该修改下面提到的代码:
#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++;
}
}