help

Well, to begin with you could
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
As formatted:

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
#include <iostream>

using namespace std;

int main() {
	int Num1, Num2, compAnswer, userAnswer;
	char op;

	// Initialise random seed
	srand(time(NULL));

	// Generate random numbers
	Num1 = rand() % 63 + 1;
	Num2 = rand() % 63 + 1;

	// Generate random operator
	int opNum = rand() % 4;
	if (opNum == 0) {
		op = '+';
		compAnswer = Num1 + Num2;
	} else if (opNum == 1) {
		op = '-';
		compAnswer = Num1 - Num2;
	} else if (opNum == 2) {
		op = '/';
		compAnswer = Num1 / Num2;
	} else {
		op = '*';
		compAnswer = Num1 * Num2;
	}

	// Output equation
	cout << Num1 << " " << op << " " << Num2 << endl;

	do { // Loop
		// Input answer
		cout << "What is your answer: ";
		cin >> userAnswer;

		// Check if answer is wrong
		if (userAnswer != compAnswer) {
			cout << "Incorrect!" << endl;
		}
	} while (userAnswer != compAnswer); // Keeps looping while answer is wrong

	cout << "Correct!" << endl;
}

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
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
Topic archived. No new replies allowed.