我已经尝试了很多东西并不断更改此代码,但我无法在接受“得分”值之前停止需要两个输入。此外,我一直无法找到一种方法来阻止cin从“得分”中得分。并且回答' 允许用户在不输入值的情况下按Enter键。我认为这样做的唯一方法就是接受它们作为字符串,但我希望避免这种情况。
#include<iostream>
#include<iomanip>
#include<limits>**strong text**
using namespace std;
//void enterdata(string*, int*);
int main(){
// string names[];
// int testscores[];
string name;
int score;
char answer;
cout<<"This program asks the user to enter a list of names and test scores,"<<endl;
cout<<"then sorts the list into ascending order according to the scores"<<endl;
cout<<"and calculates the average test score."<<endl;
// enterdata(names, testscores);
do{
cout<<"Please enter a name: ";
getline(cin, name);
while(name.empty()){
cout<<"No entry detected. Please enter a name: ";
getline(cin, name);
}
cout<<name<<endl;
cout<<"Please enter a test score: ";
cin>>score;
while(!(cin>>score) || score<0){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Input not valid. Only positive integers will be accpeted."<<endl;
cout<<"Please enter a test score: ";
}
cout<<score<<endl;
cin.sync();
cout<<"Would you like to enter another student name and test score? [y/n] ";
cin>>answer;
while(answer!='y' && answer!='n'){
cout<<"Input not valid. Only y or n will be accepted."<<endl;
cout<<"Would you like to enter another student name and test score? [y/n] ";
cin>>answer;
}
cout<<answer<<endl;
cin.sync();
} while(answer == 'y');
return 0;
}
&#13;
答案 0 :(得分:1)
错误在于:
while(!(cin>>score) || score<0){ //------ Here!
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Input not valid. Only positive integers will be accpeted."<<endl;
cout<<"Please enter a test score: ";
}
当您说cin>>score
时,您正在再次使用输入流,因此程序会提示输入更多内容!将其视为函数调用(提示:运算符重载!)。我认为你写了这个(即!(cin>>score
),因为你想测试流的错误。在这样的小型项目中,我不会出汗(学校运动?)
关于你提出问题的第二部分,关于妨碍进入效果(烦人的换行),我很确定你不能轻易做到这一点。我礼貌地问你是否真的想花时间在这上面(如果是学校练习,其他人都会有同样的问题)
以下是经过清理和更正的代码:
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
using std::cout;
using std::cin;
using std::string;
using std::endl;
//void enterdata(string*, int*);
int main() {
//string names[];
//int testscores[];
string name;
int score;
char answer = 'y';
cout << "This program asks the user to enter a list of names and test scores," << endl;
cout << "then sorts the list into ascending order according to the scores" << endl;
cout << "and calculates the average test score." << endl;
//enterdata(names, testscores);
while(answer == 'y') {
cout << "Please enter a name: ";
std::cin >> name;
while (name.empty()) {
cout << "No entry detected. Please enter a name: ";
std::cin >> name;
}
cout << name << endl;
cout << "Please enter a test score: ";
cin >> score;
while (score<0) {
cin >> score;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Input not valid. Only positive integers will be accpeted." << endl;
cout << "Please enter a test score: ";
}
cout << score << endl;
cin.sync();
cout << "Would you like to enter another student name and test score? [y/n] ";
cin >> answer;
while (answer != 'y' && answer != 'n') {
cout << "Input not valid. Only y or n will be accepted." << endl;
cout << "Would you like to enter another student name and test score? [y/n] ";
cin >> answer;
}
cout << answer << endl;
cin.sync();
}
return 0;
}