0

I'm having some minor trouble with this basic C++ quiz program. In the main function, I have the user enter his/her name and I pass this string to the next function, take_quiz. However, I've noticed that if I include a name with a space in it (like a first and last name), an error occurs. For some reason, the amount of letters in the second word produces the same amount of displays of, "Please enter a valid answer (a, b, c, d)." I thought this was strange because that prompt can only occur when the inline function valCheck is used which is after the first cin of a variable in take_quiz. I need some help identifying the issue and correcting it. Thanks!

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]);
daveliu2
  • 39
  • 8
  • See [this thread](http://stackoverflow.com/questions/5838711/c-cin-input-with-spaces) on how to use `cin` to get a text stream containing spaces. – Carlton Jan 27 '16 at 04:41
  • `valCheck()` doesn't return anything - according to signature it needs to return a `char` value. You want to use `std::getline(std::cin, str);` instead of `cin` if your string contains newlines. – Constantin Jan 27 '16 at 04:42
  • Hey Constantin the std::getline(std::cin, str) function worked great! However I don't think I needed to have valCheck return anything because it's accepting a reference and it is changing input. Correct me if I'm wrong – daveliu2 Jan 27 '16 at 04:50
  • @CZou48 Yes, correct - but then you should change the signature to: `inline void valCheck(char& input)` instead of `inline char valCheck(char& input)`. – Constantin Jan 27 '16 at 04:57

2 Answers2

0

I changed how to get string input from User

    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;
    }
Muhammad Zeeshan
  • 470
  • 7
  • 24
  • Why using a stack allocated char array of 100 bytes, instead of the much nicer & safer [std::string](http://www.cplusplus.com/reference/string/string/) class? – Constantin Jan 27 '16 at 04:53
0

Your valCheck() doesn't return anything - according to signature it needs to return a char value. You want to use std::getline(std::cin, str); instead of std::cin if your string contains newlines. std::cin will by default skip whitespaces. Also you're invoking take_quiz() without a prototype function before, so you need to move it above main() or specify the function signature at least above.

The complete program should look like this (you just need to add a check if quiz_results[0] is equal to '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;
}
Constantin
  • 8,721
  • 13
  • 75
  • 126