我在使用这个基本的C ++测验程序时遇到了一些小问题。在main函数中,我让用户输入他/她的名字,然后将这个字符串传递给下一个函数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]);
答案 0 :(得分:0)
我改变了如何从用户
获取字符串输入 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;
}
答案 1 :(得分:0)
你的d = {1:[1,333], 2:[5,22], 3:[8,0]}
# Use d[k][1] so that max is based on the second
# item in each list
max_key = max(d, key=lambda k: d[k][1])
print((max_key, d[max_key]))
(1, [1, 333])
没有返回任何内容 - 根据签名需要返回一个char值。如果您的字符串包含换行符,则希望使用std::getline(std::cin, str);而不是valCheck()
。默认情况下,std::cin
会跳过空格。此外,您之前没有使用原型函数调用std::cin
,因此您需要将其移到take_quiz()
之上或至少在上面指定函数签名。
完整的程序应该如下所示(只需添加一个检查main()
是否等于'b')。
quiz_results[0]