【发布时间】:2015-03-10 04:50:34
【问题描述】:
以下代码仅在第一个 while 循环中不能正常工作,它会自动将值赋给 cin,所以我没有机会在下一个循环之前给它一个输入。为什么会这样?
char again = 'y';
while (again=='y') {
int n=1 , chance = 5, score = 0, level = 1;
if (n == 1) {
cout << "Game begin!"<<endl;
while (chance) {
cout << "You have " << chance << " chances left." << endl;
cout<<"level "<<level<<" question!"<<endl;
vector<string> actorset = game(graph,level);
int correct = 0;
string s;
cout << "\nyour answer is:";
std::getline(cin, s);
cout << "Your Answer is " << s <<endl;
vector<string>::iterator vit = actorset.begin();
vector<string>::iterator ven = actorset.end();
cout << "Correct Answers are: "<<endl;
for ( ; vit != ven; vit++) {
if (s.compare(*vit) == 0) {
correct = 1;
}
cout << *vit <<'\t';
}
cout <<'\n';
if (correct == 0) {
chance --;
cout << "Incorrect answer" << endl;
cout << "Your total score is " << score <<"."<<endl;
} else {
score += 10;
level++;
cout <<"Correct answer! You get 10 points!"<<endl;
cout << "Your total score is " << score <<"."<<endl;
}
}
}
cout <<"high score handler"<<endl;
output.open (outfile_name, std::fstream::in | std::fstream::out);
highscore(output,score);
cout << "type y for another try: ";
cin >> again;
}
【问题讨论】: