Working on rand loop and when I run it results don't match

OK, so I am just working on making a trivia game and trying to get the questions to come up based in random number, I don't get any errors, but when I enter the answer it never seems to be right, not sure what I am doing wrong here, I do get a random number I think based off of computer time, but once I get to my questions the answers I have made correct don't work. Any suggestions to get me back on track would be awesome.

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


int questions(int guess);
    int total;
    int counter = 1;
    int score = 0;
    int enter;
    int guess;
    int point = 0;



int main()
{

    //srand is used to find the Random number based on the computer time
    srand(time(NULL));

    cout << "************************************************************************\n";
    cout << "************************************************************************\n";
    cout << "*******************  Millerizi Trivia  *********************************\n";
    cout << "************************************************************************\n";
    cout << "************************************************************************\n";
    cout << "\nHow many questions do you want to answer?\n"
         << "Enter Number Here: ";
    cin >> total;

    cout << "You want to play to answer " << total <<" questions.";

    while (counter <= total)
        {
            //This creates the number randomly between 1 and and the number after %
            cout << "Your Current Score Is: " << score <<"\n";
            guess = rand() % 4 +1;
            questions(guess);
            ++counter;
    }
    cout << "Your Final Score Is: " << score <<"\n";
    return 0;
}


int questions(int guess)
{
    int answer;
    int point;
        switch(guess)
        {
            case 1:
                cout << guess;
                cout<<"\nHow Many Players are on a soccer field for a game?\n";
                cout<<"1. 22\n";
                cout<<"2. 11\n";
                cout<<"3. 10\n";
                cout<<"4. 6\n";
                cout<<"\nPlease Enter 1-4: ";
                cin >> answer;
                    if (answer = 1)
                    {
                        cout<<"\nNice Job, that is Correct\n";
                        point = 1;
                        break;
                    }
                    if (answer = 2)
                    {
                        cout<<"\nSorry, that is incorrect";
                        point = 0;
                        break;
                    }
                    if (answer = 3)
                    {
                        cout<<"\nSorry, that is incorrect";
                        point = 0;
                        break;
                    }
                    if (answer =4)
                    {
                        cout<<"\nSorry, that is incorrect";
                        point = 0;
                        break;
                    }
            case 2:
                cout << guess;
                cout<<"\nHow Many Players are on a basketball court?\n";
                cout<<"\n1. 22";
                cout<<"\n2. 11";
                cout<<"\n3. 10";
                cout<<"\n4. 6";
                cout<<"Please Enter 1-4: ";
                cin >> answer;
                    if (answer = 1)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
                    if (answer = 2)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
                    if (answer = 3)
                    {
                        cout<<"\nNice Job, that is Correct\n";
                        break;
                    }
                    if (answer =4)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
            case 3:
                cout << guess;
                cout<<"\nWhat is the number one sport in the USA?\n";
                cout<<"\n1. Soccer";
                cout<<"\n2. Basketball";
                cout<<"\n3. Football";
                cout<<"\n4. Baseball";
                cout<<"\nPlease Enter 1-4: ";
                cin >> answer;
                    if (answer = 1)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
                    if (answer = 2)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
                    if (answer = 3)
                    {
                        cout<<"\nNice Job, that is Correct\n";
                        break;
                    }
                    if (answer =4)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
            case 4:
                cout << guess;
                cout<<"\nWhat sport has a Wold Cup every 4 years?\n";
                cout<<"\n1. Football";
                cout<<"\n2. Golf";
                cout<<"\n3. NASCAR";
                cout<<"\n4. Soccer";
                cout<<"\nPlease Enter 1-4: ";
                cin >> answer;
                    if (answer = 1)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
                    if (answer = 2)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
                    if (answer = 3)
                    {
                        cout<<"\nSorry, that is incorrect";
                        break;
                    }
                    if (answer =4)
                    {
                        cout<<"\nNice Job, that is Correct\n";
                        break;
                    }                        default:
                    cout<<"\nEnter correct choice";

        }
    //system("pause");
    }




try using switches inside the question to
switch(answer)
case 1:

default:


or useing


if else if ladders

not only if
if
if


this is the answer
|
V
Last edited on
closed account (yR9wb7Xj)
It's your if statement if I'm not mistaking because you're assigning answer to 3 but you're not assigning answer to equal 3. so example if(answer==3), try that for all of them it should work.
DOH, Codex, I think you are right, I use SQL everyday and forgot about that difference. I will try that. Thanks its Abhi, I really do need to figure out the best flow as well, currently just trying to see how I can get stuff working together and then experiment with different options.
Topic archived. No new replies allowed.