# Problems with a C/C++ Trivia Game



## Big-K (Nov 22, 2003)

I'm relatively new to C++. I only started, and only through online tutorials, a couple weeks ago. For a presentation for an English project of mine on Walt Disney, I decided to create a home-programmed trivia game, just to be out of the norm. Sofar I've been able to get it alright. I managed to get a complete, working version in a command line style(although if I could I would make it using buttons in a real window). The problem is that I have it where it has you enter numbers to correspond to letters. I havn't been able to figure out how to get it to recognize letters as letters. For example, a true and false question. Currently, if you type 1 and press enter you get correct, if you type 2 and press enter you get incorrect with an explanation of why. What I want is to do that, but instead of 1 and 2, using T and F. If you press T then you get correct. If you press any other key, particularly F, then you get the incorrect message. What I'm getting is anything you type gets you the incorrect message. Some ideas on what I'm doing wrong?

This is my current code, with only the intro and one question. In the complete version there are more questions and a conclusion.



> #include <iostream>
> #include <cstring>
> 
> using namespace std;
> ...


----------



## Regicide (Aug 8, 2003)

The problem is that you are getting two input values and storing them in the same variable. To store input into a variable, you can either use cin >> or cin.getline(), not both. What is happening is you are overwritting there answer with the later call to cin.getline(). If you use cin.getline() the last argument, delimiter, does not need to be specified if it is a newline character: '\n'. Also, if you are going to initalize a character array of just one, you can just use char. For example, 'char n' is the same as 'char n[1]'. Just a few suggestions.


----------



## Big-K (Nov 22, 2003)

If I take out the [1], I seem to get the error that it lacks a cast.

When I remove either cin>> second or cin.getline, it makes the program shut down after pressing enter at all. If I remove cin.getline and replace it with a cin.ignore();, I get the same problem I have now.


----------



## Regicide (Aug 8, 2003)

Ok, what you can do then is declare decond as a array of 5 or so (it doesn't matter as long as it is greater than 2). Then use cin.getline(second, sizeof(second)). Then, what I would do is have an if statement that only checked the first character. Thi si show I would do it:

```
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
  int answer;
  char second[5];

  cout << "Hello! Welcome to the Walt Disney Trivia Game!\nYou will be presented with 12 questions to answer.\nFor true/false questions, type 1 for true and 2 for false.\nFor multiple choice questions, type the number of your answer\n\n\n----------------------\n";
  cout << "True/False-'Jungle Book' was the last film Disney oversaw.\n";

  cin.getline(second, 5);
  if (second[0] == 'T')
  {
       cout<<"Correct\n-------\n";
  }
  else
  {
      cout<<"Incorrect! After Jungle Book, Disney died of acute circulatory collapse.\n-------\n";
  }
  cin.get();
}
```
This way they can enter 'True' and still be right.


----------



## Kramer55 (Jan 18, 2005)

What Regicide posted should work for you.

On your previous code, you had

```
if ( strcmp ( second, "T" ) == 0 )
```
For further reference, it should have been (second, 'T') since entering T is a char which uses single quotes and strings use double quotes.


----------



## Big-K (Nov 22, 2003)

Ah, thank you very much. That helps. I had no idea about the quote types.


----------

