如何接受 [ENTER] 键作为无效输入并发送错误消息
How to Accept [ENTER] key as an invalid input and send out error message
这是一个对驾驶执照考试问题的用户输入进行评分的程序。 我在验证用户输入时遇到问题。
我想接受 [ENTER] 键作为无效输入并继续我的验证,而不是只是转到空行而无法处理下一个问题。目的是发送错误消息,并且没有给出输入,并且 [ENTER] 键不是有效输入,并且只接受一次输入有效输入的机会,即 a/a、b/b、c/C 或 d/D。所以这就是为什么我在这里使用 if 语句而不是循环。
我尝试了 if (testTakerAnswers[ans] == (or =( ''( {},但仍然没有解决换行符的问题。
我在这里包含curses.h,希望使用另一篇文章中的getch((语句,但不知何故,我无法设法在我的代码中使用数组而不是常规输入。
我也在寻找其他方法而不是 getch((
所以我应该调整我的布尔函数,还是直接验证main((函数中的输入。
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <curses.h>
using namespace std;
const unsigned SIZE = 20; // Number of qns in the test
char testTakerAnswers[SIZE]; //Array to hold test taker's answers
bool validateInput(char);
class TestGrader
{
private:
char answers[SIZE]; // Holds the correct answers // Answer is array
int getNumWrong (char[]);
void missedQuestions (char[]);
public:
void setKey(string); // Initialize object with standard keys
void grade(char[]); // Grades the answers from tester
};
void TestGrader::setKey(string key){
if (key.length()!=SIZE){
cout << "Error in key data.n";
return;
}
for (unsigned pos = 0; pos < SIZE ; pos ++)
answers [pos] = key [pos];
}
void TestGrader::grade(char test[])
{
int numWrong = getNumWrong(test);
if (numWrong <= 5)
cout << "Congratulations. You passed the exam.n";
else
cout << "You did not pass the exam. n";
cout << "You got " << (SIZE-numWrong) << " questions correct. n";
if (numWrong > 0){
cout << "You missed the following " << numWrong << " questions: n";
missedQuestions(test);
}
}
int TestGrader::getNumWrong(char test[])
{
int counter = 0;
for (int i = 0; i < SIZE; i++){
if (answers[i] != toupper(testTakerAnswers[i])){
counter++;
}
}
return counter;
}
void TestGrader::missedQuestions(char test[])
{
// cout << testTakerAnswers[i]; This is to print taker's answers
int counter = 0;
for (int i = 0; i < SIZE; i++){
if (answers[i] != toupper(testTakerAnswers[i])){
cout << "n" << i + 1 << ". Correct answers: " << answers[i];
counter++;
}
}
}
bool validateInput(char ans){ // Only A, B, C, D valid input
if (toupper(ans)!='A' && toupper(ans)!= 'B' && toupper(ans)!='C' && toupper(ans)!= 'D'){
cout << "n********************WARNING*******************n";
cout << "Invalid input! Enter only a/A, b/B, c/C, or d/Dn";
return false;
}
if (testTakerAnswers[ans] == 'n'){
return false;
}
return true;
}
int main()
{
const int NUM_QUESTIONS = 20;
string name; //Test taker's name
char doAnother; //Control variable for main processing loop
TestGrader DMVexam; //Create a TestGrader object
DMVexam.setKey("BDAACABACDBCDADCCBDA");
do {
cout << "Applicant Name: ";
getline(cin,name);
cout << "Enter answer for " << name << ".n";
cout << "Use only letters a/A, b/B, c/C, and d/D. nn";
for (int i = 0; i < NUM_QUESTIONS; i++){
// Input and validate it
do{
cout << "Q" << i+1 << ": ";
cin >> testTakerAnswers[i];
if (!validateInput(testTakerAnswers[i])){
cout << "You get one more chance to correct.nOtherwise, it count as wrong answer.";
cout << "n*********************************************";
cout << "nRe-enter: ";
cin >> testTakerAnswers[i];
cout << 'n';
break;
}
}while(!validateInput(testTakerAnswers[i]));
}
//Call class function to grade the exam
cout << "Results for " << name << 'n';
DMVexam.grade(testTakerAnswers);
cout << "nGrade another exam (Y/N)? ";
cin >> doAnother;
while (doAnother != 'Y' && doAnother != 'N' && doAnother != 'y' && doAnother != 'n'){
cout << doAnother << " is not a valid option. Try Again y/Y or n/N" << endl;
cin >> doAnother;}
cout << endl;
cin.ignore();
}while(doAnother != 'N' && doAnother != 'n');
return 0;
}
您的问题是cin >> testTakerAnswers[i];
cin 是空格分隔的,这意味着任何空格(包括""(都将被丢弃。所以testTakerAnswers[i]永远不能是''。
我不确定你想做什么,但可以尝试
getline(cin,input_string);
然后
input_string == "A" | input_string == "B" | ...
因此,如果只按回车键,input_string
将变为""
.
相关文章:
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 为 c++ cin 上的输入数无效生成错误消息
- 如何定义此"if block"中其他无效输入的值,以便在c ++中将字符串转换为对象?
- 处理输入流运算符>>中的无效输入
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- C++ QTextDocument::contentChange() 信号提供无效的输入数据
- C++循环:如何让程序在用户输入无效输入时多次重复"error"消息
- C++,程序输出无效输入,但继续通过循环
- 如何使用ISTream对象输入多个字符到对象的数组中?(and 0作为哨兵值.下面的我的代码将无效)
- 当我输入任何内容时,我的代码输出无效
- 如果用户输入无效,则循环
- 如果用户只按Enter或输入无效,则C 如何重新启动循环
- 流中的输入无效C++
- 程序工作,但当输入无效时,它会继续循环 c++
- 如何停止由于输入C++无效而导致的无限循环
- 当用户输入无效整数时,c++菜单卡住了
- dll 中的输入无效<dllname.dll>但我<functionname>没有看到错误