基于字符串长度的测验功能中的错误

Error in Quiz Function Based Off String Length

本文关键字:功能 错误 字符串      更新时间:2023-10-16

我在这个基本的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;
    }