gpt4 book ai didi

C++ cin 在接受值之前需要 2 个或更多输入

转载 作者:行者123 更新时间:2023-11-28 01:42:44 26 4
gpt4 key购买 nike

我已经尝试了很多东西并不断更改此代码,但我无法让它停止在它接受“分数”值之前需要两个输入。此外,我一直无法找到一种方法来阻止 cin 从“分数”和“答案”允许用户在不输入值的情况下按回车键。我见过的唯一方法是将它们都接受为字符串,但我希望避免这种情况。

#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;
}

最佳答案

错误在这里:

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)是因为你想测试流中的错误。在像这样的小程序中,我不会出汗(学校练习? )

关于你问题的第二部分,关于阻止 enter 产生效果(烦人的换行符),我敢肯定你不能轻易做到这一点。我会礼貌地问你是否真的想花时间在这上面(如果这是学校练习,其他人都会有同样的问题)

这是清理和更正后的代码:

#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;
}

关于C++ cin 在接受值之前需要 2 个或更多输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515830/

26 4 0
文章推荐: javascript - 火狐扩展开发 : How to set an observer only once
文章推荐: html - XPath 选择该父 div 内的