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();
}

I think i have missed the actualy problem here. Could you explain why you are having trouble with integer division.

I see no reason for it not to work if you use doubles.

Chris

Better write a single function to get operands of a binary operation. Now you have tons of redundand repetitive codes. It's a bad style ;). Remember one of the most valued programming principles: divide and power ;)...

I agree with Freaky_Chris: no problems with integer division in C and C++. It's a well-defined operator. If you want to get fractions, use floating point division (another operator)...

Apropos, think about +, -, * and / characters instead of neutral a, b, c , d letters in user interface...

I just had to change some of my variables to doubles and it worked fine. I did change up the input a little using int instead of char (the kid that tested it liked to hit letters instead of numbers so i had him just use the numpad). I am done with this program as the kid with problems in math is using it and actually getting better but i will remember the advice about not having tons of redundant code for the next project i decide to come up with.

This article has been dead for over six months. Start a new discussion instead.