【问题标题】:C++ cin requiring 2 or more inputs before it accepts a valueC++ cin 在接受一个值之前需要 2 个或更多输入
【发布时间】:2017-10-01 19:01:33
【问题描述】:

我已经尝试了很多方法并不断更改此代码,但我无法让它停止在它接受“分数”值之前需要两个输入。此外,我一直无法找到一种方法来阻止 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;
}

【问题讨论】:

  • 我不知道。它似乎与那个问题不同。它没有跳过输入。如果不接受值,程序将不会进入下一步。只要我输入一个正数并回车,我第二次输入一个数字并回车时,cin 接受一个值并继续程序的下一步。
  • 您在这两行中读取了两次整数 - cin>>score; while(!(cin>>score) || score
  • 好的,@Harmeet,那么这不是运行错误检查的适当语句吗?声明 !(cin>>score) 似乎每次我输入数字以外的内容时都会为我捕捉到错误。比如说,我按字母 f 获取考试成绩,它会立即抛出无效语句并告诉我重新输入考试成绩。当我尝试输入整数时,我似乎只有双重输入问题。我应该输入其他声明/条件吗?

标签: c++ char do-while cin


【解决方案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&gt;&gt;score 时,您再次使用输入流,因此程序会提示输入更多输入! 将其视为函数调用(提示:运算符重载!)。我想你写这个(即!(cin&gt;&gt;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;
}

【讨论】:

  • 是的,这是一个学校项目。我一直在尝试在完成其余部分之前获得主要的 do-while 循环。一旦我将它与程序的其余部分一起放入,我可能不得不更改一些声明。该项目是关于指针的,它要求我动态分配一个包含名称和测试分数的数组/数组,然后使用指针,根据测试分数按升序对数组/数组进行排序,同时保持名称与其各自的测试分数相关联。然后我必须计算姓名和分数的排序列表并计算平均测试分数。
  • 无论如何,作为项目的一部分,我们必须对输入进行失败检查并要求重新输入有效数据,而不是让程序因 cin 错误而终止。
  • 另外,我感谢您的帮助,但您给我的代码也不起作用。在 Eclipse 上,它只要求我输入一个整数值,而不是打印我的任何 couts,而在 C++ Shell 上,当我在输入测试分数时给出无效响应时,它会无休止地重新打印名称的第一个值和测试分数 0 .
  • @BigB63 我怀疑除了无效输入之外,您还需要处理任何其他错误,例如包含数字的名称或包含字母的数字。但是如果你真的需要,你可以通过使用 'cin' 的 3 个成员函数和异常来使其防错。这将处理输入流可能引入的所有错误。要查看所有可能的错误,请参阅cplusplus.com(大表的“状态”行)中的成员函数和异常概述。
  • @BigB63 我刚刚将代码复制到 VS2017,即使出现 4 级警告,它也能正常编译和运行。你确定你复制的正确吗?
猜你喜欢
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
  • 2019-08-11
  • 2014-03-10
  • 2017-01-31
  • 1970-01-01
  • 2020-03-21
相关资源
最近更新 更多