Converting Basic Program to C from C++/C

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 :)



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

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>

using namespace 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";
            return true;// 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

return false;

}
}



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";
            return true; // tells result whether to add a pt or not
        }
        else{
            cout << "Incorrect\n\n";}

return false;

}


Last edited on
Topic archived. No new replies allowed.