assigning variables within if statement and used outside

Feb 13, 2016 at 11:51pm
Hello,

How can I assign a variable within a if statement and allow it to be used outside of it? If that is not possible, what the best way to make the question order random.

Any feedback is appreciated, Thanks!

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
    srand(time(0));

    for(int i=0; i<2; i++) {
        string question;
        string a;
        string b;
        string c;
        string d;
        string correct;
        int points;

        int random=rand()%(3+1)+1;

        if(random==1) {
            question = "How many continents are there?";
            a= "5";
            b= "7";
            c="3";
            d="9";
            correct= "7";
            points=50;
        }

        if(random==2) {
            question = "What is the distance from the sun to the earth? (IN MILLION MILES)";
            a= "150";
            b= "32";
            c= "92";
            d= "63";
            correct= "92";
            points= 100;
        }

        if(random==3) {
            question = "What is the width of San Francisco? (IN KM!)";
            a= "13";
            b= "16";
            c= "12";
            d= "19";
            correct= "19";
            points= 200;
        }
    printQuestion(question, a, b, c, d);
    askQuestion (question,  a, b, c, d,  correct,  points, totalPoints, correctAnswers);
 }
Feb 14, 2016 at 12:01am
How can I assign a variable within a if statement and allow it to be used outside of it?

Not possible.

what the best way to make the question order random

Aren't you already doing that? Depending on the random number, you get a question?

I would use else if though.
1
2
3
if(random == 1)
else if(random == 2)
else if(random == 3)
Last edited on Feb 14, 2016 at 12:02am
Feb 14, 2016 at 12:11am
I am such a fool. I had assigned the int random to numbers that didn't have if statement.

Thanks anyway!
Topic archived. No new replies allowed.