Need some help cleaning up and adding a loop

Hey there, newbie here. I'm supposed to create a program that gives you a menu of problems. You choose a problem, it creates two random numbers and asks you for the answer. This code works but I want help cleaning it up. For example, is there a shorter way of declaring the random numbers? How do I create a loop if they get it wrong? Any advice will be appreciated! Thank you so much. :)

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

int main(int argc, const char * argv[])
{
    int n1, n2, n3, n4, n5, n6, n7, n8, a1, a2, a3, a4, a11, a22, a33, a44, choice;
    std::cout << "What kind of problem would you like?\n";
    std::cout << "Addition, press 1\n";
    std::cout << "Subtraction, press 2\n";
    std::cout << "Multiplication, press 3\n";
    std::cout << "Division, press 4\n";
    std::cin >> choice;
    n1 = rand() % 100 + 1;
    n2 = rand() % 100 + 1;
    n3 = rand() % 100 + 1;
    n4 = rand() % 100 + 1;
    n5 = rand() % 100 + 1;
    n6 = rand() % 100 + 1;
    n7 = rand() % 100 + 1;
    n8 = rand() % 100 + 1;


    if(choice == 1)
    {std::cout << n1 << "+" << n2 << "=" << "\n\n";
    a1 = n1+n2;

    std::cout << "What is the answer?";
    std::cin >> a11;
    if(a1==a11)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }
    else if(choice == 2)
    {
    std::cout << n3 << "-" << n4 << "=" << "\n\n";
    a2 = n3-n4;
    std::cout << "What is the answer?";
    std::cin >> a22;
    if(a2==a22)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }

    else if(choice == 3)
    {
    std::cout << n5 << "x" << n6 << "=" << "\n\n";
    a3 = n5*n6;
    std::cout << "What is the answer?";
    std::cin >> a33;
    if(a3==a33)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }
    if(choice == 4)
    {std::cout << n7 << "/" << n8 << "=" << "\n\n";
    a4 = n7/n8;
    std::cout << "What is the answer?";
    std::cin >> a4;
    if(a4==a44)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }
}
Exactly why do you need 8 random numbers in the first place? Wouldn't 2 work just as well?

As for looping until the user gets the correct answer, I guess something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
bool correct = false;

while (!correct)
{
    std::cin >> answer; //get input
    if (answer == something) //test input
    {
        std::cout << "Congratulations, you got it right!";
        correct = true;
    }
    else
        std::cout << "Sorry, you got it wrong.  Try again: ";
}


Also, you might want to know that doing math with integers mean that the result would be an integer too (ie 5/2 is 2, not 2.5).
I'm not sure why you need a different set of random numbers for each operation - can't you just use the same two random numbers no matter what operation the user selected?
I guess I didn't think it through or I thought there might be an error when using the same random numbers. That will help clean it up. Thank you.

Where would I insert the loop?
You would insert it between your if statements.

1
2
3
4
5
6
if (choice == 1)
    //insert loop here
else if (choice == 2)
   //insert loop here
else if (choice == 3)
  //etc. 
Topic archived. No new replies allowed.