【发布时间】:2020-12-04 21:14:10
【问题描述】:
所以我目前正在用 C++ 编写一个程序。我写了以下函数。它说“//code here”的那一行是我的问题所在。基本上,该程序允许用户创建自己的测验 txt 文件。对于“测验应该有多少问题?”这个问题。用户将输入一个代表问题数量的数字。当必须验证用户输入时,就会出现问题。
谁能帮我创建一个执行以下操作的循环?
- 确保用户只输入数字。
- 确保输入的数字大于 1。
- 如果用户输入了非数字,则会向用户发送错误消息。
- 如果用户输入的数字小于 2,则会向用户发送错误消息。
- 程序会验证用户是否只是按下了回车键。
所有这些都整理好后,程序会将 NumberOfQuestions 设置为等于用户输入并将其转换为 int 后的值。
void WriteOutQuestions(string &QuizName)
{
ofstream WriteOut;
string filename = "";
string userInput = "";
int numberOfQuestions;
char userInputChar = '0';
bool IncludeCommments = false;
cout << "Name your file\n";
getline(cin, filename);
cout << "Now give your new quiz a title\n";
getline(cin, QuizName);
cout << "How many questions should the quiz have?\n";
getline(cin, userInput);
//code here
numberOfQuestions = stoi(userInput);
cout << "The quiz will contain " << numberOfQuestions << " questions." << endl;
cout<< "Would you like to include comments in any of the choices?\n";
cout << "[Y/y for yes N/n for No]\n";
getline(cin, userInput);
if (userInput == "y" && userInput == "Y")
IncludeCommments = true;
else
cout << "Comments disabled by user...\n";
WriteOut.open(filename + ".txt");
if (!WriteOut)
{
cout << "The file was not found...\n";
}
else
{
cout << "File was read!\n";
}
WriteOut << QuizName << endl;
WriteOut << numberOfQuestions << endl;
for (int i = 0; i < numberOfQuestions; ++i)
{
cout << "What is question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << "Q" << i + 1 << " " + userInput << endl;
cout << "What is choice A for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << "A) " + userInput << endl;
if (IncludeCommments == true)
{
cout << "What is the comment for choice A for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << userInput << endl;
}
else
WriteOut << "" << endl;
cout << "What is choice B for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << "B) " + userInput << endl;
if (IncludeCommments == true)
{
cout << "What is the comment for choice B for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << userInput << endl;
}
else
WriteOut << "" << endl;
cout << "What is choice C for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << "C) " + userInput << endl;
if (IncludeCommments == true)
{
cout << "What is the comment for choice C for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << userInput << endl;
}
else
WriteOut << "" << endl;
cout << "What is choice D for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << "D) " + userInput << endl;
if (IncludeCommments == true)
{
cout << "What is the comment for choice D for question number " << i + 1 << "?\n";
getline(cin, userInput);
WriteOut << userInput << endl;
}
else
WriteOut << "" << endl;
cout << "Which choice is the right one? [A, B, C or D]\n";
getline(cin, userInput);
while (userInput != "a" && userInput != "A" && userInput != "b" && userInput != "B" &&
userInput != "c" && userInput != "C" && userInput != "d" && userInput != "D")
{
cout << "Only A-D is accepted\n";
}
userInputChar = userInput[0];
if (userInputChar > 96)
{
userInputChar -= 32;
}
userInput = userInputChar;
cout << "userinput contains " << userInput << endl;
cout << "userinputchar contains " <<userInputChar << endl;
WriteOut << userInput << endl;
}
WriteOut.close();
}
【问题讨论】:
-
允许的最大数量是多少?
标签: c++ loops validation exception getline