More Rand Issues

Hello all, I recently posted a thread concerning issues with using rand in programs.

I am still having some trouble as I now have to change the program again.

The program outputs 4 problems for the user to complete and displays a congratulatory message if the user got the answer correct, as well as giving a message telling the user if they got it wrong and gives them another chance at the problem until they get it right.

Now I need the program to output 20 problems with not only random integers between 10-99, but I also need it to give a random operand between *,/,+,-.

Here is the program now:

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
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>

using namespace std;

int main()
{
    srand(time(NULL));

    double a = rand()%(99-10+1) + 10;
    double b = rand()%(99-10+1) + 10;
    double c = rand()%(99-10+1) + 10;
    double d = rand()%(99-10+1) + 10;
    double e = rand()%(99-10+1) + 10;
    double f = rand()%(99-10+1) + 10;
    double g = rand()%(99-10+1) + 10;
    double h = rand()%(99-10+1) + 10;

    double value_1, value_2, value_3, value_4;

    static const char* answers[] = {"\nNice Job!\n\n" , "\nCongratulations! Your answer is correct.\n\n", "\nYes! You are right!\n\n"};

    cout<< "This program will test your arithmetic skills.\n \n";

    cout<<"  "<<a<<"+"<<b<< " = ";
    cin>>value_1;

        while (value_1 != (a+b))
        {
            cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
            cout<< "  "<<a<<"+"<<b<< " = ";
            cin>> value_1;
        }

        if (value_1 == (a+b))
        {
            cout << answers[rand() % 3];
        }

    cout<<"  "<<c<<"-"<<d<< " = ";
    cin>>value_2;

        while (value_2 != (c-d))
        {
            cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
            cout<< "  "<<c<<"-"<<d<< " = ";
            cin>> value_2;
        }

        if (value_2 == (c-d))
        {
        cout << answers[rand() % 3];
        }

    cout<<"  "<<e<<"*"<<f<< " = ";
    cin>>value_3;

        while (value_3 != (e*f))
        {
            cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
            cout<< "  "<<e<<"*"<<f<< " = ";
            cin>> value_3;
        }

        if (value_3 == (e*f))
        {
            cout << answers[rand() % 3];
        }

    cout<<"  "<<g<<"/"<<h<< " = ";
    cin>>value_4;

        while (value_4 <= (g/h)-.01 || value_4 >= (g/h)+.01)
        {
            cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
            cout<< "  "<<g<<"/"<<h<< " = ";
            cin>> value_4;
        }

        if (value_4 >= (g/h)-.009 && value_4 <= (g/h)+.009)
        {
            cout << answers[rand() % 3];
        }

    return 0;
}


Can you use rand to choose between 4 options that are not integers? Or is there a way I can use rand to choose between integers that are assigned to each individual operand?

Finally, Is there a way I can just use a and b as my variables and have them be assigned different numbers each time they are used instead of using 40 different variables like a,b,c,d,e....
rand()%(99-10+1) + 10; Why this? Why are you having the computer calculate this over and over?

Operators:
You can enumerate them. Basically it's a name that gets assigned a number, you can then check the number and output the correspond operand for that.

Operands:
Break this into functions and I think you'll see the solution is pretty simple
I think the OP did it to remember the range of the values the rands could emit. Any decent optimizing compiler will precompute the value for him. :)

@OP:
1. Yes you can, you'll just have to have some sort of mapping from one to the other. I would, as ResidentBiscuit suggested, use functions.
2. Yes. Just do the assignment each time you need to change them (ex. line 41).

-Albatross

Thanks for your help ResidentBiscuit and Albatross!

I think the program works the way it should now. I have tested the program a few times and I have yet to find any issues.

So here's the code I came up with:

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
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>

using namespace std;


void Problems ();
double value;

int main()
{
    srand(time(NULL));
    cout<< "This program will test your arithmetic skills.\n \n";
    int Problem_Number = 1;
    while (Problem_Number<=20)
    {
    Problem_Number++;
    Problems();
    }


    return 0;
}

void Problems()
{
    static const char* answers[] =
    {"\nNice Job!\n\n" , "\nCongratulations! Your answer is correct.\n\n", "\nYes! You are right!\n\n"};

        double a = rand()%(99-10+1) + 10;
        double b = rand()%(99-10+1) + 10;
        double op = rand()%(4-1+1) + 1;
        double value1,value2;

        if (op==1)
        {
            value1 = a+b;

            cout<<a<<"+"<<b<<" = ";
            cin>>value2;
            while (value1 != value2)
            {
                cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
                cout<< "  "<<a<<"+"<<b<< " = ";
                cin>>value2;
            }
            if (value2 = value1)
            {
                cout << answers[rand() % 3];
            }

        }
        else if (op==2)
        {
            value1 = a-b;

            cout<<a<<"-"<<b<<" = ";
            cin>>value2;
            while (value1 != value2)
            {
                cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
                cout<< "  "<<a<<"-"<<b<< " = ";
                cin>>value2;
            }
            if (value2 == value1)
            {
                cout << answers[rand() % 3];
            }
        }
        else if (op==3)
        {
            value1 = a*b;

            cout<<a<<"*"<<b<<" = ";
            cin>>value2;
            while (value1 != value2)
            {
                cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
                cout<< "  "<<a<<"*"<<b<< " = ";
                cin>>value2;
            }
            if (value2 = value1)
            {
                cout << answers[rand() % 3];
            }
        }
        else
        {
            value1 = a/b;

            cout<<a<<"/"<<b<<" = ";
            cin>>value2;
            while (value2 <= (a/b)-.01 || value2 >= (a/b)+.01)
            {
                cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
                cout<< "  "<<a<<"/"<<b<< " = ";
                cin>>value2;
            }
            if (value2 >= (a/b)-.01 && value2 <= (a/b)+.01)
            {
                cout << answers[rand() % 3];
            }
        }
}


Please don't hesitate to tell me what I could have done better or more efficiently, or if there are problems I did not detect in the program.

Also @ ResidentBiscuit - How could I get my computer to not calculate that over and over again while still generating random variables in that range?
Last edited on
Well, that whole bit in the parentheses could be brought to just one number.
Topic archived. No new replies allowed.