Quiz Game C++

my code keeps getting an error. can anyone help? Its supposed to be a multiple choice quiz game that pulls the Q&A from external .txt file and give you a score.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

/// Scores depending upon difficulty of questions.

const float DIFF_LEVEL_ONE = 1.23;
const float DIFF_LEVEL_TWO = 1.51;
const float DIFF_LEVEL_THREE = 2.02;

int main()
{
    cout << "\  ______________________________\n";
    cout << "  Project 2: C++ Game Time\n";
    cout << "\  ______________________________\n";

    ///step1:
    ///Declare the necessary variables, for example:
    ofstream outFile;           /// declare output file stream variable (done)
    ifstream inFile;
    string alias;               /// declare alias name (done)
    string date;                /// declare the date (string)
    string A, B, C, D;
    string question;
    int questions;              /// declare the number of questions (integer)
    int difficulty;             /// declare the difficulty level (integer)
    int counter;
    int correct;
    float multiplier;
    char theAnswer;
    char Answer;
    char answer;

    ///step2:
    ///prompt for the user information (Game alias and date).
    cout << "Enter the alias name for the game (example: C++ Game): ";
    cin >> alias;
    cout << "Enter the date (example: Oct/12/2017): ";
    cin >> date;

    ///step3 (done):
    ///use user's Game Name to create a new output file.
    alias = alias.append(".txt");
    outFile.open(alias.c_str(), ios::app);

    ///step4
    ///Ask the user for number of questions REPEATEDLY.
    ///Check if the number of questions is between 1-15.
    do {
    cout << "How many questions? <1 - 15>: ";
    cin >> questions;
    if (questions > 15)
    {
       cout << "You entered an invalid answer." << endl;
    }}
    while (questions > 15);

    ///step5
    ///ask the user to enter the difficulty level.
    /// check the difficulty level.
    /// If the difficulty level is not within the acceptable range, display the error message and exit the program.
    cout << "Enter difficulty level: <1,2,3>: ";
    cin >> difficulty;
    while (1> difficulty || difficulty > 3)
    {
        cout << "Invalid difficulty level! Try again later. " << endl << endl;
    }

    ///step6
    /// open the correct input file
    /// use the files(questions1.txt or questions2.txt or questions3.txt) based on the level.
    switch (difficulty)
    {
    case 1:
        inFile.open("questions1.txt");
            multiplier = DIFF_LEVEL_ONE;
            break;
    case 2:
        inFile.open("questions2.txt");
            multiplier = DIFF_LEVEL_TWO;
            break;
    case 3:
        inFile.open("questions3.txt");
            multiplier = DIFF_LEVEL_THREE;
            break;
    }

    ///step7
    ///you will need to START a LOOP here to repeat steps 7.1, 7.2, 7.3, 7.4, 7.5, and 7.6 for all questions:
    ///for example, if the user entered the number of questions = 5, then the loop will run 5 times.
                    ///step 7.1
                    ///read the question, the correct answer, and the 4 options from the input file.
                    ///step 7.2
                    ///Print the question along with the options on screen.
    do
        {
        counter = 1;
        counter++;
        getline(inFile, question);
        inFile >> theAnswer;
        inFile >> A;
        inFile >> B;
        inFile >> C;
        inFile >> D;
        }
    while (counter <= question)

                    ///step 7.3
                    ///Ask the user for his choice.
    cout << question << endl;
    cout << "A: " << A << endl;
    cout << "B: " << B << endl;
    cout << "C: " << C << endl;
    cout << "D: " << D << endl;
    cin >> Answer;

                    ///step 7.4
                    ///Convert the user choice into upper case letter. Use toupper() to convert a char to uppercase.
    answer = toupper(Answer);

                    ///step 7.5
                    ///If the user choice matches the solution, Then print it as correct and calculate the score.
        if (answer! = 'A' && answer! = 'B' && answer! = 'C' && answer! = 'D')
        {
            cout << "You entered an invalid answer! Try again later. " << endl;
            cin >> Answer;
        }
        if else (answer == theAnswer)
        {
            cout << "CORRECT! " << endl;
            correct++;
        }
                    ///step 7.6
                    ///If the user choice does not match with the solution. Then print the correct solution along with the user's choice.
        else
            cout << "Wrong answer! Better luck next time. " << endl;
            cout << theAnswer << endl;
    }
    ///
    /// END the loop here once ALL questions are answered.
    ///step8
    ///Print the GameName and the score to the screen
    cout << "\  ______________________________\n";
    cout << setw(25) << "GameName: " << left << alias << endl;
    cout << setw(25) << "Score: " << left << correct * multiplier << endl;
    cout << "\  ______________________________\n";

    ///step9
    ///print the Date, Difficulty Level, and the score to the output file.
    outFile << date <<  difficulty <<  correct * multiplier << endl;

    ///step10
    ///Close the input
    ///Close the output file.
    outFile.close();
    inFile.close();
    return 0;
}
Last edited on
closed account (48T7M4Gy)
Please use code tags

Also you’ll need to be more specific about the problem than just saying there is an error.
Line 4: You need #include <string>

Lines 10-12: These lines generate truncation warnings because the right sides are by default double. Use const double.

Lines 16,18,146,148: Illegal escape sequence. \ space is not a valid escape sequence.

Line 31: correct is an uninitialized variable (garbage).

Line 32: If you change lines 10-12 to double, multiplier should be a double also.

Line 47: You don't check that the open of the output file succeeded.

Line 67: If the difficulty is invalid, your program will go into an infinite loop.

Line 102: You don't check that the read succeeded. What if the number of questions entered by the user is greater than the number of entries in the file?

Line 109: You're comparing an int and a string. Did you mean questions?

Line 109: Needs a ; to terminate the do/while.

Line 126: Do not put a space between ! and =.

Line 131: if else is not valid. Id you mean if or else?

Line 139: You're missing an opening {

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
looks like i have a lot of fixing to do. I went ahead and formatted the code. This is my first post so I appreciate all your help.
now the game will run but it gets stuck on the difficulty and with a valid answer it just stops. doesn't prompt to exit or anything. I'm not sure what I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

/// Scores depending upon difficulty of questions.

const double DIFF_LEVEL_ONE = 1.23;
const double DIFF_LEVEL_TWO = 1.51;
const double DIFF_LEVEL_THREE = 2.02;

int main()
{
    cout << "______________________________\n";
    cout << "  Project 2: C++ Game Time\n";
    cout << "______________________________\n";

    ///step1:
    ///Declare the necessary variables, for example:
    ofstream outFile;           /// declare output file stream variable (done)
    ifstream inFile;
    string alias;               /// declare alias name (done)
    string date;                /// declare the date (string)
    string A, B, C, D;
    string question;
    int questions;              /// declare the number of questions (integer)
    int difficulty;             /// declare the difficulty level (integer)
    int counter;
    int correct;
    double multiplier;
    char theAnswer;
    char Answer;
    char answer;

    ///step2:
    ///prompt for the user information (Game alias and date).
    cout << "Enter the alias name for the game (example: C++ Game): ";
    cin >> alias;
    cout << "Enter the date (example: Oct/12/2017): ";
    cin >> date;

    ///step3 (done):
    ///use user's Game Name to create a new output file.
    alias = alias.append(".txt");
    outFile.open(alias.c_str(), ios::app);

    ///step4
    ///Ask the user for number of questions REPEATEDLY.
    ///Check if the number of questions is between 1-15.
    do {
    cout << "How many questions? <1 - 15>: ";
    cin >> questions;
    if (1> questions || questions > 15)
    {
       cout << "You entered an invalid answer." << endl;
    }}
    while (1 <= questions || questions >= 15);

    ///step5
    ///ask the user to enter the difficulty level.
    /// check the difficulty level.
    /// If the difficulty level is not within the acceptable range, display the error message and exit the program.
    cout << "Enter difficulty level: <1,2,3>: ";
    cin >> difficulty;
    if (1> difficulty || difficulty > 3)
    {
        cout << "Invalid difficulty level! Try again later. " << endl << endl;
    }
    ///step6
    /// open the correct input file
    /// use the files(questions1.txt or questions2.txt or questions3.txt) based on the level.
    switch (difficulty)
    {
    case 1:
        inFile.open("questions1.txt");
            multiplier = DIFF_LEVEL_ONE;
            break;
    case 2:
        inFile.open("questions2.txt");
            multiplier = DIFF_LEVEL_TWO;
            break;
    case 3:
        inFile.open("questions3.txt");
            multiplier = DIFF_LEVEL_THREE;
            break;
    }

    ///step7
    ///you will need to START a LOOP here to repeat steps 7.1, 7.2, 7.3, 7.4, 7.5, and 7.6 for all questions:
    ///for example, if the user entered the number of questions = 5, then the loop will run 5 times.
                    ///step 7.1
                    ///read the question, the correct answer, and the 4 options from the input file.
                    ///step 7.2
                    ///Print the question along with the options on screen.
    do
        {
        counter = 1;
        counter++;
        getline(inFile, question);
        inFile >> theAnswer;
        inFile >> A;
        inFile >> B;
        inFile >> C;
        inFile >> D;
        }
    while (counter <= questions);

                    ///step 7.3
                    ///Ask the user for his choice.
    cout << question << endl;
    cout << "A: " << A << endl;
    cout << "B: " << B << endl;
    cout << "C: " << C << endl;
    cout << "D: " << D << endl;
    cin >> Answer;

                    ///step 7.4
                    ///Convert the user choice into upper case letter. Use toupper() to convert a char to uppercase.
    answer = toupper(Answer);

                    ///step 7.5
                    ///If the user choice matches the solution, Then print it as correct and calculate the score.
        if (answer != 'A' && answer != 'B' && answer != 'C' && answer != 'D')
        {
            cout << "You entered an invalid answer! Try again later. " << endl;
            cin >> Answer;
        }
        if (answer == theAnswer)
        {
            cout << "CORRECT! " << endl;
            correct++;
        }
                    ///step 7.6
                    ///If the user choice does not match with the solution. Then print the correct solution along with the user's choice.
        else
            cout << "Wrong answer! Better luck next time. " << endl;
            cout << theAnswer << endl;

    ///
    /// END the loop here once ALL questions are answered.
    ///step8
    ///Print the GameName and the score to the screen
    cout << "\  ______________________________\n";
    cout << setw(25) << "GameName: " << left << alias << endl;
    cout << setw(25) << "Score: " << left << correct * multiplier << endl;
    cout << "\  ______________________________\n";

    ///step9
    ///print the Date, Difficulty Level, and the score to the output file.
    outFile << date <<  difficulty <<  correct * multiplier << endl;

    ///step10
    ///Close the input
    ///Close the output file.
    outFile.close();
    inFile.close();
    return 0;
}
Lines 77,81,85: You don't check if the open of the file succeeded. If the file doesn't open, you proceed as if nothing is wrong.

Lines 97-100: You essentially read the whole file into the same variables. When you exit the loop, you're only going to have the last set of values in memory.

Line 90: You're not looping correctly. Your loop should run from line 97 to line 142.

You have not corrected a number of the problems I pointed out previously.




Im not sure how to check if the open of file was successful, but i fixed the loop and changed the do while to for and the game now runs with the questions, but after the first question the questions get messed up. Thank you again for all your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

/// Scores depending upon difficulty of questions.

const double DIFF_LEVEL_ONE = 1.23;
const double DIFF_LEVEL_TWO = 1.51;
const double DIFF_LEVEL_THREE = 2.02;

int main()
{
    cout << "______________________________\n";
    cout << "  Project 2: C++ Game Time\n";
    cout << "______________________________\n";

    ///step1:
    ///Declare the necessary variables, for example:
    ofstream outFile;           /// declare output file stream variable (done)
    ifstream inFile;
    string alias;               /// declare alias name (done)
    string date;                /// declare the date (string)
    string A, B, C, D;
    string question;
    int questions;              /// declare the number of questions (integer)
    int difficulty;             /// declare the difficulty level (integer)
    int counter;
    int correct;
    double multiplier;
    char theAnswer;
    char Answer;
    char answer;

    ///step2:
    ///prompt for the user information (Game alias and date).
    cout << "Enter the name for the game (example: C++ Game): ";
    cin >> alias;
    cout << "Enter the date (example: Oct/12/2017): ";
    cin >> date;

    ///step3 (done):
    ///use user's Game Name to create a new output file.
    alias = alias.append(".txt");
    outFile.open(alias.c_str(), ios::app);

    ///step4
    ///Ask the user for number of questions REPEATEDLY.
    ///Check if the number of questions is between 1-15.
    do {
    cout << "How many questions? <1 - 15>: ";
    cin >> questions;
    if (1> questions || questions > 15)
    {
       cout << "You entered an invalid answer." << endl;
    }
    else
        break;
    }
    while (1 <= questions || questions >= 15);

    ///step5
    ///ask the user to enter the difficulty level.
    /// check the difficulty level.
    /// If the difficulty level is not within the acceptable range, display the error message and exit the program.
    cout << "Enter difficulty level: <1,2,3>: ";
    cin >> difficulty;
    if (1> difficulty || difficulty > 3)
    {
        cout << "Invalid difficulty level! Try again later. " << endl << endl;
    }
    ///step6
    /// open the correct input file
    /// use the files(questions1.txt or questions2.txt or questions3.txt) based on the level.
    switch (difficulty)
    {
    case 1:
        inFile.open("questions1.txt");
            multiplier = DIFF_LEVEL_ONE;
            break;
    case 2:
        inFile.open("questions2.txt");
            multiplier = DIFF_LEVEL_TWO;
            break;
    case 3:
        inFile.open("questions3.txt");
            multiplier = DIFF_LEVEL_THREE;
            break;
    default:
        return 1;
    }

    ///step7
    ///you will need to START a LOOP here to repeat steps 7.1, 7.2, 7.3, 7.4, 7.5, and 7.6 for all questions:
    ///for example, if the user entered the number of questions = 5, then the loop will run 5 times.
                    ///step 7.1
                    ///read the question, the correct answer, and the 4 options from the input file.
    for (counter=1; counter<=questions; counter++){
        getline(inFile, question);
        inFile >> theAnswer;
        inFile >> A;
        inFile >> B;
        inFile >> C;
        inFile >> D;

                    ///step 7.2
                    ///Print the question along with the options on screen.
                    ///step 7.3
                    ///Ask the user for his choice.
    cout << question << endl;
    cout << "A: " << A << endl;
    cout << "B: " << B << endl;
    cout << "C: " << C << endl;
    cout << "D: " << D << endl;
    cin >> Answer;

                    ///step 7.4
                    ///Convert the user choice into upper case letter. Use toupper() to convert a char to uppercase.
    answer = toupper(Answer);

                    ///step 7.5
                    ///If the user choice matches the solution, Then print it as correct and calculate the score.
        if (answer != 'A' && answer != 'B' && answer != 'C' && answer != 'D')
        {
            cout << "You entered an invalid answer! Try again later. " << endl;
            cin >> Answer;
        }
        if (answer == theAnswer)
        {
            cout << "CORRECT! " << endl;
            correct++;
        }
                    ///step 7.6
                    ///If the user choice does not match with the solution. Then print the correct solution along with the user's choice.
        else
            cout << "Wrong answer! Better luck next time. " << endl;
            cout << theAnswer << endl;
}
    ///
    /// END the loop here once ALL questions are answered.
    ///step8
    ///Print the GameName and the score to the screen
    cout << "______________________________\n";
    cout << "GameName: " << alias << endl;
    cout << "Score: " << correct * multiplier << endl;
    cout << "______________________________\n";

    ///step9
    ///print the Date, Difficulty Level, and the score to the output file.
    outFile << date <<  difficulty <<  correct * multiplier << endl;

    ///step10
    ///Close the input
    ///Close the output file.
    outFile.close();
    inFile.close();
    return 0;
}


Im not sure how to check if the open of file was successful
1
2
3
4
  if (! stream.is_open())
  {  cout << "File failed to open" << endl;
      return 1; 
  }

http://www.cplusplus.com/reference/fstream/ifstream/is_open/

Topic archived. No new replies allowed.