Randomized multiple choice

okay so I have gotten my code sorted out, and now it works. But to be adaptable to 3pi robots, which I will be loading it into eventually, the user has to be given three letter (a, b, c) multiple choices for answer input to the math questions. The three choices have to be randomized everytime the program loops too. Currently it just runs on number input. Any ideas of what sort of statements or functions i should use to do this?


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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>





int menu();
void addition(int *Preward); // function to return truth values
void subtraction(int *Preward);

int main()
{
    int Finished = 0;
    int choice, score = 0;
    int reward = 0;

    while(Finished != 1) // while finished is not true
    {
        printf("\nscore: %d", score); // print the score
        {
            choice = menu(); // calls for user input and enters it to switch
            switch  (choice){ // accesses the functions
            case 0: Finished = 1;                     break;
            case 1: addition(&reward);                break;
            case 2: subtraction(&reward);             break;
            default : printf("Error in input\n\n");   break;
            }
            if(reward == 1){
                score += reward; // score = score + reward
            }
        }
    }
    return 0;
}

int menu() //main menu that gets called and allows fr user input
{
    int MenuChoice;
    printf("\n");
    printf("Enter 1 for addition\n");
    printf("Enter 2 for Subtraction\n");
    printf("Enter 0 to quit\n");
    printf("-----------------\n");
    printf("Choice: ");
    scanf("%d", &MenuChoice); // user input
    return MenuChoice; //return the choice to choice to select a case
}

void addition(int *Preward) //function that is called by the users selection
{
    int answer;
    int random1, random2;
    srand(time(NULL)); //generates fresh numbers each time.
    random1 = 1+(rand()%99); // from 1-99
    random2 = 1+(rand()%99); // from 1-99

    printf("\n%d - %d = ", random1, random2); //code to format the random math problem
    scanf("%d", &answer); // user input of answer
    {
        if (answer == (random1+random2)){
            printf("Congratulations\n\n");
            *Preward = 1;// tellsesult whether to add a pt or not
        }   //if user is correct than the score is incremented
        else{
            printf("Incorrect\n\n");} //if not than the score is not incremented

    }
}

void subtraction(int *Preward) // follows same model as previous function, but instead subtraction.
{
    int answer;
    int random1, random2;

    srand(time(NULL));

    random1 = 1+(rand()%99); // from 1-99
    random2 = 1+(rand()%99); // from 1-99

    printf("\n%d - %d = ", random1, random2);
    scanf("%d", &answer);

    if (answer == (random1-random2)){
        printf("Congratulations\n\n");
        *Preward = 1;// tells result whether to add a pt or not
    }
    else{
        printf("Incorrect\n\n");
    }

}
Topic archived. No new replies allowed.