我一直遇到这个问题,我试图验证用户输入,但我发现自己在整个程序中使用了大量的 ignores()。由于这是一个学校课程,我只限于iostream 、 cctype和student.h库。问题是我需要确保用户不会尝试将字母字符输入到我认为已涵盖的整数字段中if(!(cin >> val))
,然后我使用cin.ignore(numeric_limits<streamsize>::max(), '\n');
忽略任何额外的内容(例如,如果他们尝试在整数字段中输入小数)。有些东西不能正常工作,因为我要么无法输入名称字段,要么我得到剩余的小数作为名称(如果我输入 123.6,名称将为 0.6)。有人知道比使用质量忽略更好的方法来验证整数吗?
主要的
#include <iostream>
using namespace std;
#include "student.h"
//**************************************************************************
bool validInt(int&);
//**************************************************************************
int main()
{
Student student;
bool validInput;
cout << "You have Chosen to Add a New Student." << endl;
cout << "---------------------------------------" << endl;
// Student ID Validation
do
{
cout << "Enter Student ID (ex. 123): ";
validInput = validInt(student.id);
}while(!validInput);
cout << "Enter Student Name (ex. John Doe): ";
cin.getline(student.name, 50);
cout << "Enter Student City and State (ex. St. Louis, Missouri): ";
cin.getline(student.citystate, 50);
cout << "\n\nStudent ID: " << student.id << endl;
cout << "Student Name: " << student.name << endl;
cout << "Student City / State: " << student.citystate << endl;
return 0;
}
//**************************************************************************
bool validInt(int& val)
{
bool valid = true;
if(!(cin >> val))
{
cout << "\nERROR: Please enter a Positive Whole Number" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
valid = false;
}
return valid;
}
学生页眉
#ifndef STUDENT_H
#define STUDENT_H
//**************************************************************************
struct Student
{
int id;
char name[50];
char citystate[50];
friend ostream& operator<<(ostream& out, const Student& data);
bool operator == (const Student &rhs) const;
bool operator != (const Student &rhs) const;
bool operator < (const Student &rhs) const;
bool operator > (const Student &rhs) const;
bool operator <= (const Student &rhs) const;
bool operator >= (const Student &rhs) const;
};
//**************************************************************************
ostream& operator << (ostream& out, const Student& data)
{
out << data.id << " " << data.name << endl;
return out;
}
//**************************************************************************
bool Student::operator == (const Student &rhs) const
{
return (this->id == rhs.id);
}
//**************************************************************************
bool Student::operator != (const Student &rhs) const
{
return (this->id != rhs.id);
}
//**************************************************************************
bool Student::operator < (const Student &rhs) const
{
return (this->id < rhs.id);
}
//**************************************************************************
bool Student::operator > (const Student &rhs) const
{
return (this->id > rhs.id);
}
//**************************************************************************
bool Student::operator <= (const Student &rhs) const
{
return (this->id <= rhs.id);
}
//**************************************************************************
bool Student::operator >= (const Student &rhs) const
{
return (this->id >= rhs.id);
}
#endif