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

C++ cin requiring 2 or more inputs before it accepts a value

我已经尝试了很多东西并不断更改此代码,但我无法让它停止需要两个输入才能接受 'score' 的值。此外,我一直无法找到一种方法来阻止来自 'score' 和 'answer' 的 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;
}