基于字符串长度的测验功能中的错误
Error in Quiz Function Based Off String Length
我在这个基本的C++测验程序中遇到了一些小问题。在主函数中,我让用户输入他/她的名字,然后将此字符串传递给下一个函数,take_quiz。但是,我注意到,如果我包含一个带有空格的名称(如名字和姓氏),就会发生错误。出于某种原因,第二个单词中的字母数量会产生相同数量的显示,"请输入有效的答案(a,b,c,d)"。我认为这很奇怪,因为只有在使用内联函数 valCheck 时才会出现该提示,该函数是在 take_quiz 中变量的第一个 cin 之后。我需要一些帮助来识别问题并纠正它。谢谢!
inline char valCheck(char& input)
{
tolower(input);
while(input < 97 || input > 100)
{
cout << "Please enter a valid answer (a, b, c, d):" << endl;
cin >> input;
}
}
int main(int argc, char *argv[])
{
string name;
cout << "This program will quiz your knowledge of C++. Please enter your name:" << endl;
cin >> name;
cout << "Hello " << name << "! IT'S QUIZ TIME!!!" << endl;
take_quiz(name);
system("PAUSE");
return EXIT_SUCCESS;
}
void take_quiz(string name2)
{
char quiz_results[10];
system("PAUSE");
cout << "nThe quiz will now begin.nThis quiz covers topics such as data types, arrays, pointers, etc." << endl
<< "To answer the multiple choice questions,nsimply input a, b, c, or d according to the given options." << endl
<< "The test will continue regardless if you enter a question wrong or right." << endl
<< "Good luck " << name2 << "!" << endl;
system("PAUSE");
cout << "n1. What preprocessor command must one include to use the cout and cin function?" << endl
<< "na. #include <iomanip>" << endl
<< "b. #include <iostream>" << endl
<< "c. #include <cmath>" << endl
<< "d. using namespace std;" << endl;
cin >> quiz_results[0];
valCheck(quiz_results[0]);
您的valCheck()
不返回任何内容 - 根据签名,它需要返回一个 char 值。你想使用 std::getline(std::cin, str);而不是std::cin
如果您的字符串包含换行符。 默认情况下,std::cin
将跳过空格。此外,您之前在没有原型函数的情况下调用take_quiz()
,因此您需要将其移动到main()
上方或至少在上方指定函数签名。
完整的程序应该看起来像这样(你只需要添加一个检查quiz_results[0]
是否等于'b')。
#include <iostream>
#include <string>
using namespace std;
inline char valCheck(char& input){
tolower(input);
while (input < 97 || input > 100){
cout << "Please enter a valid answer (a, b, c, d):" << endl;
cin >> input;
}
return input;
}
void take_quiz(string name2)
{
char quiz_results[10];
system("PAUSE");
cout << "nThe quiz will now begin.nThis quiz covers topics such as data types, arrays, pointers, etc." << endl
<< "To answer the multiple choice questions,nsimply input a, b, c, or d according to the given options." << endl
<< "The test will continue regardless if you enter a question wrong or right." << endl
<< "Good luck " << name2 << "!" << endl;
system("PAUSE");
cout << "n1. What preprocessor command must one include to use the cout and cin function?" << endl
<< "na. #include <iomanip>" << endl
<< "b. #include <iostream>" << endl
<< "c. #include <cmath>" << endl
<< "d. using namespace std;" << endl;
cin >> quiz_results[0];
valCheck(quiz_results[0]);
}
int main(int argc, char *argv[]){
string name;
cout << "This program will quiz your knowledge of C++. Please enter your name:" << endl;
std::getline(std::cin, name);
cout << "Hello " << name << "! IT'S QUIZ TIME!!!" << endl;
take_quiz(name);
system("PAUSE");
return EXIT_SUCCESS;
}
我更改了从用户获取字符串输入的方式
int main(int argc, char *argv[])
{
char name[100];
cout << "This program will quiz your knowledge of C++. Please enter your name:" << endl;
cin.getline(name,sizeof(name));
//cin >> name;
cout << "Hello " << name << "! IT'S QUIZ TIME!!!" << endl;
take_quiz(name);
system("PAUSE");
return EXIT_SUCCESS;
}
相关文章:
- Malloc后的自由功能错误
- 功能错误中的表达语法帮助!C/C
- 将对象传递到类,没有接收匹配功能错误
- 无效打印功能错误
- 绑定std ::功能错误
- 我可以启用有关我自己的变异功能错误使用的指定符的警告
- 功能错误的使用无效
- C 朋友模板功能错误
- 单链接列表推送功能错误
- ConstexPR静态模板功能:G 错误是Clang上的警告
- 一个奇怪的打印功能错误
- 信号功能错误
- 查找最低值.功能.错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)
- 超载功能错误消息
- 与操作员'<<'不匹配 有人可以帮助解决无效功能错误吗?[C++]
- 没有操作员发现升压read_xml功能错误
- 工厂设计模式中的纯虚拟功能错误
- 如何跟踪LLVM验证功能错误"Expected no forward declarations!"?
- OOP项目功能错误
- c++ GTK控制台日志功能错误