Jun 10, 2022 at 9:35am
As a starter then perhaps something like this (using C++ random rather than c rand):
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
|
#include <iostream>
#include <random>
#include <iomanip>
std::mt19937 rng(std::random_device {}());
int main() {
std::uniform_int_distribution<int> nums(-64, 64);
std::uniform_int_distribution<unsigned> oper(0, 3);
unsigned correct {}, asked {};
char again {};
do {
const auto Num1 { nums(rng) };
const auto Num2 { nums(rng) };
if (Num1 == 0 || Num2 == 0)
continue;
int compAnswer {};
char op {};
switch (oper(rng)) {
case 0:
op = '+';
compAnswer = Num1 + Num2;
break;
case 1:
op = '-';
compAnswer = Num1 - Num2;
break;
case 2:
op = '/';
compAnswer = Num1 / Num2;
break;
case 3:
op = '*';
compAnswer = Num1 * Num2;
}
std::cout << Num1 << " " << op << " " << Num2 << '\n';
++asked;
int userAnswer {};
std::cout << "What is your answer (as an integer): ";
std::cin >> userAnswer;
if (userAnswer != compAnswer)
std::cout << "Incorrect!\n";
else {
std::cout << "Correct!\n";
++correct;
}
std::cout << "Again (y/n): ";
std::cin >> again;
} while (again == 'y' || again == 'Y');
std::cout << "Asked " << asked << ", correct " << correct << " (" << std::setprecision(2) << std::fixed << correct * 100.0 / asked << "%)\n";
}
|
Last edited on Jun 10, 2022 at 9:40am
Jun 15, 2022 at 12:59pm
Ah, another "I need better code" leech shows up. They get it and then edit their post to remove their original code.
And now the OP has "left the building."
Last edited on Jun 15, 2022 at 1:01pm