Code:
#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 random1, random2;
int order;
int one, two, three;
int rando1, rando2;
char a, b, c;
char userselect;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
rando1 = 2+(rand()%198); // 2 to 198
rando2 = 2+(rand()%198); // 2 to 198
order = 1+(rand()%3);
switch(order){
case 1: printf("\n%d + %d = ", random1, random2);
one = (random1+random2);
two = (rando1);
three = (rando2);
printf("\n select from the following\n a) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect);
if (userselect == a){
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is a\n\n");
}break;
case 2: printf("\n%d + %d = ", random1, random2);
one = (rando1);
two = (random1+random2);
three = (rando2);
printf("\n select from the following letters\n a) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect);
if (userselect == b){
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is b\n\n");
}break;
case 3: printf("\n%d + %d = ", random1, random2);
one = (rando1);
two = (rando2);
three = (random1+random2);
printf("\n select from the following letters\n a) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect);
if (userselect == c){
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is c\n\n");
}break;
default: printf("error\n"); break;
}
}
void subtraction(int *Preward) // follows same model as previous function, but instead subtraction.
{
int random1, random2;
int order;
int one, two, three;
int rando1, rando2;
char a, b, c;
char userselect;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
rando1 = 2+(rand()%198); // 2 to 198
rando2 = 2+(rand()%198); // 2 to 198
order = 1+(rand()%3);
switch(order){
case 1: printf("\n%d - %d = ", random1, random2);
one = (random1-random2);
two = (rando1);
three = (rando2);
printf("\n select from the following\n a) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect);
if (userselect == a){
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is a\n\n");
}break;
case 2: printf("\n%d - %d = ", random1, random2);
one = (rando1);
two = (random1-random2);
three = (rando2);
printf("\n select from the following letters\n a) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect);
if (userselect == b){
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is b\n\n");
}break;
case 3: printf("\n%d - %d = ", random1, random2);
one = (rando1);
two = (rando2);
three = (random1-random2);
printf("\n select from the following letters \na) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect);
if (userselect == c){
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is c\n\n");
}break;
default: printf("error\n"); break;
}
}