I have made a simple console based flash card program for the children in my house (and for practice as I just started teaching myself the language). It uses srand to get 2 numbers and has the user input the question. I have included addition, subtraction and multiplication but i would also like to include division. Knowing the problems with integer division in c++, I find myself stuck. Should I just make a bunch of problems and have the program pick them randomly or can I do it like did the rest. I was actually suprised that the program ran the first time i compiled it as I am a complete novice to the language.
Here is the code for the english version (I am the only native english speaker in my house so I wrote it in spanish).
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int answer = 0, first = 0, second = 0,
maxn = 1, turn = 1;
double percent = 100.0, ques = 1, score = 0;
char c = 0, d = 0;
// c is what kind of problem and d is difficulty.
void start();
void add();
void subtract();
void end();
void mult();
int main(int nargs, char* args)
{
cout << "This program generates 'flashcards' to test your math." <<
endl << "It is also to test my c++ programming.\n";
start();
}
void start(void)
{
score = 0;
cout << "Please choose your game:\n(a) Addition\n(b) Subtraction\n" <<
"(c) Multiplication\n(x) Exit\n";
cin >> c;
switch (c)
{
case 'a' : add();
break;
case 'b' : subtract();
break;
case 'c' : mult();
break;
case 'x' : exit(0);
break ;
default : start();
break;
}
}
void add(void)
{
cout << "\nAddition Test.\n";
cout << "Please choose your difficulty:\n(a) easy\n(b) hard\n";
cin >> d;
switch (d)
{case 'a' : maxn = 10;
break;
case 'b' : maxn = 100;
break;
default : start();
break;
}
cout << "How many questions?\n";
cin >> ques;
for (turn = 0;turn < ques;turn++)
{
first = rand() % maxn;
second = rand() % maxn;
cout << first << " + " << second << " = \n\n";
cin >> answer;
if (answer != first + second)
{
cout << "Wrong! " << first << " + " << second << " = " << first + second
<< "\n\n";
}
else
{
score++;
cout << "Correct! " << first << " + " << second << " = " << first + second
<< "\n\n";
}
}
end();
}
void subtract(void)
{
cout << "\nSubtraction Test.\n";
cout << "Please choose your difficulty:\n(a) easy\n(b) hard\n";
cin >> d;
switch (d)
{case 'a' : maxn = 10;
break;
case 'b' : maxn = 100;
break;
default : start();
break;
}
cout << "How many questions?\n";
cin >> ques;
for (turn = 0;turn < ques;turn++)
{
first = rand() % maxn;
second = rand() % maxn;
while (second > first)
{
second = rand() % maxn;
}
cout << first << " - " << second << " = \n\n";
cin >> answer;
if (answer != first - second)
{
cout << "Wrong! " << first << " - " << second << " = " << first - second
<< "\n\n";
}
else
{
score++;
cout << "Correct! " << first << " - " << second << " = " << first - second
<< "\n\n";
}
}
end();
}
void mult(void)
{
cout << "\nMultipilcation Test.\n";
cout << "Please choose your difficulty:\n(a) easy\n(b) hard\n";
cin >> d;
switch (d)
{case 'a' : maxn = 10;
break;
case 'b' : maxn = 20;
break;
default : start();
break;
}
cout << "How many questions?\n";
cin >> ques;
for (turn = 0;turn < ques;turn++)
{
first = rand() % maxn;
second = rand() % maxn;
cout << first << " * " << second << " = \n\n";
cin >> answer;
if (answer != first * second)
{
cout << "Wrong! " << first << " * " << second << " = " << first * second
<< "\n\n";
}
else
{
score++;
cout << "Correct! " << first << " * " << second << " = " << first * second
<< "\n\n";
}
}
end();
}
void end(void)
{
cout << "You correctly answered " << score << " of " << ques << " questions.\n";
percent = 100 * (score / ques);
cout << "That is " << percent << "%\n\n";
start();
}