Better Programming Skills

Jan 26, 2016 at 10:03pm
Hey guys.
So I'm building a quiz/trivia game that takes a file of Q's and A's and formats them into a function. My program here works completely fine. My question is from my question Function. I was wondering is there a more efficient way to write a function that sets up the questions in a random order?
Below is my code:


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
 void questionFunction(fileStruct arr[], int & score, int limit)
{
    for (int i = 0; i < limit; i++)
    {
        
    
        std::cout << i + 1 << "." << arr[i].question << std::endl;
        int choice;
        int x = rand() % 4 + 1;
        switch(x)
        {
            case 1:
                std::cout << "1:" << arr[i].rightAnswer << std::endl;
                std::cout << "2:" << arr[i].wrongAnswer1 << std:: endl;
                std::cout << "3:" << arr[i].wrongAnswer2 << std:: endl;
                std::cout << "4:" << arr[i].wrongAnswer3 << std:: endl;
                std::cout << "Answer(1/2/3/4):";
                std::cin >> choice;
                switch (choice)
                {
                    case 1:
                        std::cout << "Correct!" << std::endl;
                        score = score + 1;
                        break;
                    case 2:
                        std::cout << "Incorrect" << std::endl;
                        std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                        break;
                        
                    case 3:
                        std::cout << "Incorrect" << std::endl;
                        std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                        break;
                        
                    case 4:
                        std::cout << "Incorrect" << std::endl;
                        std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                        break;
                    
                    default:
                        std::cout << "You did not choose a correct choice!" << std::endl;
                        break;
                }
                break;
                
            case 2:
                std::cout << "1:" << arr[i].wrongAnswer1 << std::endl;
                std::cout << "2:" << arr[i].rightAnswer << std:: endl;
                std::cout << "3:" << arr[i].wrongAnswer2 << std:: endl;
                std::cout << "4:" << arr[i].wrongAnswer3 << std:: endl;
                std::cout << "Answer(1/2/3/4):";
                std::cin >> choice;
                switch (choice)
                {
                case 1:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                case 2:
                    std::cout << "Correct!" << std::endl;
                    score = score + 1;
                    break;
                    
                case 3:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                    
                case 4:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                    
                default:
                    std::cout << "You did not choose a correct choice!" << std::endl;
                    break;
                }
                break;
                
            case 3:
                std::cout << "1:" << arr[i].wrongAnswer2 << std::endl;
                std::cout << "2:" << arr[i].wrongAnswer1 << std:: endl;
                std::cout << "3:" << arr[i].rightAnswer << std:: endl;
                std::cout << "4:" << arr[i].wrongAnswer3 << std:: endl;
                std::cout << "Answer(1/2/3/4):";
                std::cin >> choice;
                switch (choice)
                {
                case 1:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                
                case 2:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                    
                case 3:
                    std::cout << "Correct!" << std::endl;
                    score = score + 1;
                    break;
                    
                case 4:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                    
                default:
                    std::cout << "You did not choose a correct choice!" << std::endl;
                    break;
                }
                break;
                
            case 4:
                std::cout << "1:" << arr[i].wrongAnswer3 << std::endl;
                std::cout << "2:" << arr[i].wrongAnswer1 << std:: endl;
                std::cout << "3:" << arr[i].wrongAnswer2 << std:: endl;
                std::cout << "4:" << arr[i].rightAnswer << std:: endl;
                std::cout << "Answer(1/2/3/4):";
                std::cin >> choice;
                switch (choice)
                {
                case 1:
                    std::cout << "Correct!" << std::endl;
                    score = score + 1;
                    break;
                case 2:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                    
                case 3:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                    
                case 4:
                    std::cout << "Incorrect" << std::endl;
                    std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
                    break;
                    
                default:
                    std::cout << "You did not choose a correct choice!" << std::endl;
                    break;
                }
                break;

        }
    }
    
}

Jan 26, 2016 at 10:18pm
Lots of redundant code here. You could shorten this up a good bit.

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
void questionFunction(fileStruct arr[], int & score, int limit)
{
    for (int i = 0; i < limit; i++)
    {
        std::cout << i + 1 << "." << arr[i].question << std::endl;
        int choice;
        int x = rand() % 4 + 1;
        switch(x)
        {
        case 1:
            std::cout << "1:" << arr[i].rightAnswer << std::endl;
            std::cout << "2:" << arr[i].wrongAnswer1 << std:: endl;
            std::cout << "3:" << arr[i].wrongAnswer2 << std:: endl;
            std::cout << "4:" << arr[i].wrongAnswer3 << std:: endl;
            break;
        case 2:
            std::cout << "1:" << arr[i].wrongAnswer1 << std::endl;
            std::cout << "2:" << arr[i].rightAnswer << std:: endl;
            std::cout << "3:" << arr[i].wrongAnswer2 << std:: endl;
            std::cout << "4:" << arr[i].wrongAnswer3 << std:: endl;
            break;
        case 3:
            std::cout << "1:" << arr[i].wrongAnswer2 << std::endl;
            std::cout << "2:" << arr[i].wrongAnswer1 << std:: endl;
            std::cout << "3:" << arr[i].rightAnswer << std:: endl;
            std::cout << "4:" << arr[i].wrongAnswer3 << std:: endl;
            break;
        case 4:
            std::cout << "1:" << arr[i].wrongAnswer3 << std::endl;
            std::cout << "2:" << arr[i].wrongAnswer1 << std:: endl;
            std::cout << "3:" << arr[i].wrongAnswer2 << std:: endl;
            std::cout << "4:" << arr[i].rightAnswer << std:: endl;
            break;
        }
        std::cout << "Answer(1/2/3/4):";
        std::cin >> choice;
        if(choice == x)
        {
            std::cout << "Correct!" << std::endl;
            score = score + 1;
        }
        else
        {
            std::cout << "Incorrect" << std::endl;
            std::cout << "The correct answer was " << arr[i].rightAnswer << std::endl;
        }
    }
}

As for asking the questions in random order, you could read your questions into some kind of collection (as you seem to be doing now) then use std::shuffle on the collection.
http://www.cplusplus.com/reference/algorithm/shuffle/
Last edited on Jan 26, 2016 at 10:20pm
Jan 26, 2016 at 11:21pm
Awesome thanks man. I actually never heard of the shuffle function. Thanks a lot.
Topic archived. No new replies allowed.