As suggested, use functions to factor out duplicate code and keep your main function neat. This way the program flow is easily settled, e.g.
#include <iostream>
#include <cstdlib>
#include <string>
#include <cassert>
using namespace std;
struct Operation
{
string name;
string symbol;
};
enum
{
eAdd = 0,
eSub,
eMul,
eOperations
};
int computeResult(size_t choice, int x, int y);
size_t askOperation(const Operation op_array[], size_t size);
int askGuess(const Operation & op, int x, int y);
int main()
{
srand(time(0));
Operation op_array[eOperations] = { { "addition", "+" }, { "subtraction", "-"}, {"multiplication", "*"}};
while (true)
{
size_t choice = askOperation( op_array, eOperations );
if ( choice >= eOperations) break;
int a = rand() % 10;
int b = rand() % 10;
if ( a < b ) swap(a,b);
int result = computeResult(choice, a, b);
int guess = askGuess( op_array[choice], a, b);
cout << "your answer is " << ( guess == result ? "correct" : "incorrect") << endl << endl;
}
cout << "see you next time" << endl;
}
int computeResult(size_t choice, int x, int y)
{
int result = -1;
switch( choice)
{
case eAdd:
result = x + y;
break;
case eSub:
result = x - y;
break;
case eMul:
result = x * y;
break;
default:
assert(false);
}
return result;
}
size_t askOperation(const Operation op_array[], size_t size)
{
cout << "choose an operation. " << endl << "enter";
for (size_t n=0; n<size; ++n)
cout << ", " << (n+1) << " for " << op_array[n].name;
cout << " (any other value to exit): " << endl;
int choice;
cin >> choice;
return (choice-1);
}
int askGuess(const Operation & op, int x, int y)
{
cout << "you choose " << op.name << endl;
cout << "what is " << x << " " << op.symbol << " " << y << " ?" << endl;
int result;
cin >> result;
return result;
}
Modern
C++
allowes more concision:
#include <iostream>
#include <utility>
#include <cstdlib>
#include <string>
#include <array>
using namespace std;
using Function = int (int, int);
struct Operation
{
string symbol;
string name;
Function * fun;
};
size_t askOperation(const array<Operation, 3> & oper_array);
int askGuess(const Operation & oper, int x, int y);
int main()
{
srand(time(0));
array<Operation, 3> oper_array =
{{
{ "+", "addition", [](int x, int y) { return x+y;} },
{ "-", "subtraction", [](int x, int y) { return x-y;} },
{ "*", "multiplication", [](int x, int y) { return x*y;}}
}};
while (true)
{
auto choice = askOperation( oper_array );
if ( choice >= oper_array.size()) break;
int a = rand() % 10;
int b = rand() % 10;
if ( a < b ) swap(a,b);
Operation & oper = oper_array[choice];
int result = oper.fun( a, b );
int guess = askGuess( oper, a, b);
cout << "your answer is " << ( guess == result ? "correct" : "incorrect") << endl;
}
cout << "see you next time" << endl;
}
size_t askOperation(const array<Operation, 3> & oper_array)
{
cout << "choose an operation. " << endl << "enter";
for (size_t n=0; n<oper_array.size(); ++n)
cout << ", " << (n+1) << " for " << oper_array[n].name;
cout << " (any other value to exit): " << endl;
int choice;
cin >> choice;
return (choice-1);
}
int askGuess(const Operation & oper, int x, int y)
{
cout << "you choose " << oper.name << endl;
cout << "what is " << x << " " << oper.symbol << " " << y << " ?" << endl;
int result;
cin >> result;
return result;
}
[update]
Or
#include <iostream>
#include <functional>
#include <map>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
using Operation = tuple< string, char , function < int (int, int) > >;
map < int, Operation > m = {
{1, {"addition", '+', plus<int>() }},
{2, {"subtraction", '-', minus<int>() }},
{3, {"multiplication", '*', multiplies<int>() }},
};
while (true)
{
cout << "\nenter choice:\n";
for (const auto & [i, oper] : m)
cout << '\t' << i << " " << get<0>(oper) << '\n';
cout << "any other value to exit\n";
int choice;
cin >> choice;
auto it = m.find(choice);
if ( it == m.end()) break;
int a = rand()%10;
int b = rand()%10;
if ( a < b ) swap(a,b);
const auto & [name, symbol, fun] = it->second;
cout << "you choose " << name << "\n";
cout << "what is " << a << " " << symbol << " " << b << " ?\n";
int guess;
cin >> guess;
cout << "your guess is " << (fun(a,b) == guess ? "correct\n" : "incorrect\n");
}
cout << "see you next time\n";
}
[/update]