Using Functions to create an arithmetical quiz

I am stil finding my way into functions in c++. I can't figure out how to sketch out the code for the below exercise. It has also been repeated to given as a practice exercise for us. Your precious help in helping me write this code would be grateful.....
The practice exercise specifications are as following:

The program will be a "quiz-er" for the user. he/she should be presented with a quiz of arithmetic problems. Each "play" of the quiz will be 10 questions. The user will initially be presented with a short menu of options on difficulty level. It could look something like this:

DIFFICULTY LEVEL

1. Easy
2. Moderate
3. Advanced
The difficulty levels determine the number of digits in the operands to be added or subtracted. Easy means only single digit numbers; moderate means double digit numbers; and advanced means 4-digit numbers. After the user picks the level they desire, your program presents problems that look like this:

45 + 9 = _
34 - 88 = _
etc
But for each problem presented, the user is given a chance to answer. If the answer is correct, another problem is presented. If the answer is wrong, the user is to be given one more chance at that problem. Once your program has moved on to the next problem, the correctness/incorrectness of the preceeding problem is tallied. The number of correct and incorrect answer is to be presented at the termination of the quiz along with percentage correct.
So, how are the problems produced? Well, you will use the random number generator provided by the compiler to determine:

1. the values to be added or subtracted
2. whether the problem is addition or subtraction

Remember: use the function srand() in your main to seed the generator and rand() to retrieve random numbers after that. Notes: (1) rand() returns a long int; and (2) pass time(NULL) as an argument to srand() and #include<ctime> to be able to use the system's time clock to seed the generator. I will speak in class about random numbers.


Here is a listing of the functions you are required to have in your program. You may include others if you see fit.
• a displayMenu function that has no parameters and returns a char
• a randomInt function that has 2 integer parameters (a minimum and a maximum) that returns a random integer between arguments min and max.
• a generateOperands function that returns nothing but has three parameters. One parameter represents the difficulty level (char). The other two are reference parameters representing the operands of the arithmetic problem that the next function will display.
• a displayProblem function that returns a long int (the answer that the user inputs) and has two parameters (the operands supplied by the preceding function. Note: In either this function or the preceding function, you will decide (randomly) whether the problem is an addition or subtraction problem.
• a isCorrect function that returns a bool and has three parameters. The parameters are the operands of the problem and the answer given by the user. The return value is determined in this function by whether or not the user's answer is correct or not for the given problem.
• a displayMessage function that returns nothing but outputs "correct" or "incorrect" dependent on the value returned by the preceding function.
• a displayFinalResults function that returns nothing and has two parameters: the number correct and the number wrong.


Once the user has finished the quiz, prompt them to see if they'd like to do it again. When you submit, play the quiz once and opt out of a second go round.
Okay. You need a function to return random numbers, which I take it they already told you. Check through the references for rand() and srand(). They'll help a lot.

Also check time.h.

Try generating the two random numbers using %/*(10^number of digits you want)*/, and a third random number using %2 and then using an if-else clause. Then, create some function to check whether the value the user inputed is right, and if so repeat the part that generates the random numbers if the user requests. I'll let you figure out the rest. Good luck.

-Albatross

EDIT: Why the double post? Accident?

Last edited on
Mostly people use functions to get things easyer, but in this case...
No mather I did a little something and here it is, maybe it can help you start

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <vector>

using namespace std;

//the Menu
string Menu(){

    string chose;

    cout << "Hello this is a c++ quizer pleas enter :\nHelp-for a description\nDiff- for chosing a difficulty level " << endl;
    cin  >> chose;

    if(chose!="Help" && chose!="Diff")
    while(chose!="Help" && chose!="Diff"){
            cout << "\n\nYou did not enter a valid comand. Enter Help or Diff" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
            cin  >> chose;
    }

    if(chose=="Help"){
    cout << "\n\nYou will be presented with a quiz of arithmetic problems. Each ''play'' of the quiz will be 10 questions.\n"
         << "You can chose betwean 3 difficulty levels \n"
         << "Easy-single digit numbers\nModearte-double digit numbers\nAdvanced- 4-digit numbers" << endl;

    cout << "\n\nHelp-for a description\nDiff- for chosing a difficulty level " << endl;
    getline(cin, chose);
    }


    if(chose=="Diff"){
        cout << "Enter a difficult level : Easy, Moderate or Advanced " << endl;
        cin  >> chose;
        if(chose!="Easy" && chose!="Moderate" && chose!="Advanced")
        while(chose!="Easy" && chose!="Moderate" && chose!="Advanced"){
            cout << "\nYou did not enter a valid comand. Enter : Easy, Moderate or Advanced" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
            cin  >> chose;
        }
    }

    return chose;
}

//a function that returns true or false depending on have some par of numbers appeared alredy
bool beenhere(int num1,int num2,vector<int> v1, vector<int>v2){

    for(int i=0; i<v1.size(); i++){
        if(v1[i]==num1 && v2[i]==num2){
        return true;
        }
    }

    return false;
}
//function wich generates numbers betwen min and max
int Getnumb(int min,int max){

    int numb(-1);

    while(numb<min || numb>max)
    numb=rand();

    return numb;
}

//this function gives a random operator "-" or "+"
char Getoperator(){

    char operato=1;

    while(operato!=43 && operato!=45)
    operato=rand();

    return operato;
}
//finds the correct answer
int c_answer(int num1, int num2, char operato){

    int c_answer(0);;

    if(operato==43)
    c_answer=num1+num2;
    else if(operato==45)
    c_answer=num1-(num2);
    //cout << "\n\nTacan odgovor je : \n\n" << c_answer;
    //return c_answer;
}
//is returning true or false depending on the given answer is right or wrong
bool CorrectAnswer(int c_answerr, int answer){


    if(c_answerr==answer)
    return true;

    return false;
}

//a function wich displays the "question"
int Display(int num1,int num2, char operato, int c_answerr ){

    int answer;

    for(int i=1; i<=2; i++){
        if(i==2)
        cout << "Incorrect answer try again" << endl;

        cout << num1 << operato << num2 << "= ";
        cin  >> answer;


        if(CorrectAnswer(c_answerr,answer)==true)
        break;
    }

    return answer;
}

//gives the info about the answer being right or wrong
void Display1(bool trueOrfalse, int &correct,int &incorrect){

        if(trueOrfalse==true){
            cout << "Your answer is correct" << endl;
            correct++;
        }
        else if(trueOrfalse==false){
            cout << "That answer is incorrect" << endl;
            incorrect++;
        }
}
//display gives the player info aboput how many answers he had right
void DisplayEnd(int correct,int incorrect){
    cout << "\nYou had " << correct << " correct answers and " << incorrect << " incorrect answers\n"
         << "\nYou had " << correct*100/(correct+incorrect) << " % of correct answers\n" << endl;
}

int main(){
    string diff("a");
    do{
    if(diff=="Exit")
    break;
    diff=Menu();
    int num1,num2;
    int min(0), max(9999);

    if (diff=="Moderate"){
    min=0; max=99;
    }

     else if(diff=="Easy"){
     min=0;
     max=9;
     }

    vector<int> v1, v2;
    int correct(0), incorrect(0);
    for(int i=1; i<=10; i++){
        num1=Getnumb(min,max); num2=Getnumb(min,max);

        if(beenhere(num1,num2,v1,v2)==true && i>1)
        while(beenhere(num1,num2,v1,v2)==true){
                 num1=Getnumb(min,max); num2=Getnumb(min,max);
            };


        v1.push_back(num1);
        v2.push_back(num2);

        char operato(Getoperator());
        int c_answerr(c_answer(num1,num2,operato));

        int answer(Display(num1,num2,operato,c_answerr) );

        bool trueOrfalse(false);

        if(c_answerr==answer)
        trueOrfalse=true;

        Display1(trueOrfalse,correct,incorrect);
}
    DisplayEnd(correct,incorrect);
    cout << "\nThis is the end, if You want to play again type Again\n for exit type Exit\n ";
    cin  >> diff;
}while(diff!="Exit");

    return 0;
}

It could be done much easyer but i tryed to follow the instructions...
Cheers
Last edited on
@justABeginner: Don't just post code. It doesn't really help the OP learn anything.
*glass breaks, cat yelps*

There's no need for a vector<> here!!! And all the functions didn't have to have even one argument... oh God... there was no need for all that, it's overwritten and has incredible overhead, it could have been so darn simple and still followed all the instructions... really, there was no need to show off...

Sorry if this will come across as harsh, but it's for your own good.

This forum is not just a group of insane geeks, as I like to joke about. Those who are considered elite here are teachers. None of us here are homework service, at least most of us who are thinking hope not to be. Those of us who have been labeled "good teachers" give partial solutions or just a few scraps if it's really needed. And we NEVER post the whole code in answer to a question someone posts in any forum other than Lounge or Jobs (no sane person starts threads with questions in Articles unless the questions are part of an article they wrote in the same post).

I may be wrong, but considering all the redundant features of this code, it looks like you were trying to show off. This person asked for help, wanted a solution they could understand (at least I hope), and they probably don't know what a template is. I may be wrong about that, but just so you know, the place to show off what you can do is in your own thread that you created to share a program with the world. Not in a place where someone might have trouble understanding what the heck you're doing...

Welcome to the forums.

-Albatross
i was not realy trying to show of, just in some posts i found that people want codes to give their homework. If I can help them and help myself at the same time, why not?
I like doing this, not showing of but typing codes.
And I may not be a good teacher, (one reason will be my bad english writing).

Just if it is so bad , i apologise, and I will not write here anymore.
Thx ;)

That's fine if you want to help yourself by solving others' problems, but we ask that you don't
post complete solutions because, as Albatross said, it does not help the original poster other
than to get the a higher grade than their skill level would otherwise indicate.
Topic archived. No new replies allowed.