Below is a program that generates random Addition for Subtraction problems depending on the user's choice. The user is prompted to input an answer and then it keeps your score. If you want to quit you just press zero. This is pretty much my first C program ever, I read the first eight chaps of my C/c++ book and along with supplementary online readings, made it. The thing is, it is suppose to be pure C! I was looking for advice on how to salvage this, please give me your wisdom :)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>
usingnamespace std;
int menu();
bool addition(int &reward); // function to return truth values
bool subtraction(int &reward);
int main()
{
bool Result, Finished = false;
int choice, reward, score = 0;
while(!Finished) // while finished is not true
{
cout << "\nscore:" << score; // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = true; break;
case 1: Result = addition(reward); break;
case 2: Result = subtraction(reward); break;
default : cout << "Error in input\n\n"; break;
}
if(Result == true){
score += reward; // score = score + reward
}
}
}
}
int menu() //main menu that gets called and allows for user input
{
int MenuChoice;
cout << "\n";
cout << "Enter 1 for addition\n";
cout << "Enter 2 for Subtraction\n";
cout << "Enter 0 to quit\n";
cout << "-----------------\n";
cout << "Choice: ";
cin >> MenuChoice; // user input
return MenuChoice; //return the choice to choice to select a case
}
bool addition(int &reward) //function that is called by the users selection
{
int answer;
int random1, random2;
reward = 1;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
cout << "\n" << random1 << " + " << random2 << " = "; //code to format the random math problem
cin >> answer; // user input of answer
{
if (answer == (random1+random2)){
cout << "Congratulations\n\n";
returntrue;// tells result whether to add a pt or not
} //if user is correct than the score is incremented
else{
cout << "Incorrect\n\n";} //if not than the score is not incremented
returnfalse;
}
}
bool subtraction(int &reward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
reward = 1;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
cout << "\n" << random1 << " - " << random2 << " = ";
cin >> answer;
if (answer == (random1-random2)){
cout << "Congratulations\n\n";
returntrue; // tells result whether to add a pt or not
}
else{
cout << "Incorrect\n\n";}
returnfalse;
}