Need help with this code!

I need some help with this code, its a random sum generator that does subtraction. I want it to only generate sums that equal a positive number and not ones that generate a negative number.
Any ideas how id make that work with the code below?
Any help is appreciated.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <ctype.h>
#include <string.h>


using namespace std;

int main()
{
    cout << "Mash your keyboard to exit!" << endl;
	cout << " " << endl;

		int a = 0;
		int b = 0;
		int sum = 0;
		int x = 0;

	while (true) // Keeps going until you get a right answer
	{
		srand((unsigned)time(NULL));

		a = 1 + (rand() % 10);
		b = 1 + (rand() % 10);
		sum = a - b;

        cout << a;
		cout << " - ";
		cout << b << endl;
		cout << "The answer is ";
		cin >> x; // Takes in a type int as x is defined as an int

		if (x != sum)
		{
			cout << "That is not the right answer! " << endl;
			cout << "The correct answer is " << sum << endl;
			cout << " " << endl;
		}
		if (cin.fail()) // Checks if input for a valid type
		{
			cout << "That is not a number! Numbers only. " << endl;
			cout << " " << endl;
			system("pause"); // If you want to keep going remove these to lines. 
			break;
		}
		if (x == sum)
		{
			cout << "That is the correct answer! " << endl;
			cout << " " << endl;
		}
	}
	return 0;
}


Thanks!
1
2
sum = a - b;
sum = abs(a - b);
Will this work for you?
No, unfortunately that didn't work, it still gave me a sum that left me with a negative answer.
Thanks though.
sum cannot be negative. However order of a and b will not change.
To solve that add
1
2
if (a < b)
    std::swap(a, b);
before calculating sum
Just a question:

Why would you include iostream and stdio? It's literally the same thing.
iostream and stdio? It's literally the same thing.
They are completely different.

OP probably just included all headers which he ever were used. I do the same (albeit by including "everything.h") for quick test projects.
I have them both added from other things I was trying, most of the libraries included aren't actually being used.
@MiiNiPaa

I tried that code and it does work very well, with one problem, when a sum changes from' a-b' to 'b-a' it still calculates 'a-b'. So even if I type in the correct answer for what the computer tells me it still says i'm wrong. Any idea how to fix this?
I'm fairly new to this so it could be quite simple.
Thanks.
Last edited on
Show your code.
Her it is:

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <ctype.h>
#include <string.h>


using namespace std;

int main()
{
	cout << "Mash your keyboard to exit!" << endl;
	cout << " " << endl;

	int a = 0;
	int b = 0;
	int sum = 0;
	int x = 0;

	while (true) // Keeps going until you get a right answer
	{
		srand((unsigned)time(NULL));

		a = 1 + (rand() % 10);
		b = 1 + (rand() % 10);
		sum = a - b;

		if (a < b)
			std::swap(a, b);

			cout << a;
			cout << " - ";
			cout << b << endl;
			cout << "The answer is ";
			cin >> x; // Takes in a type int as x is defined as an int

		if (x != sum)
		{
			cout << "That is not the right answer! " << endl;
			cout << "The correct answer is " << sum << endl;
			cout << " " << endl;
		}
		if (cin.fail()) // Checks if input for a valid type
		{
			cout << "That is not a number! Numbers only. " << endl;
			cout << " " << endl;
			system("pause"); // If you want to keep going remove these to lines. 
			break;
		}
		if (x == sum)
		{
			cout << "That is the correct answer! " << endl;
			cout << " " << endl;
		}
	}
	return 0;
}
MiiNiPaa wrote:
To solve that add [...] before calculating sum
1
2
3
sum = a - b;
if (a < b) //It looks more like "after"
	std::swap(a, b);
I'm not quite following you, would you be able to copy what i'm supposed to do into the code and I might understand it better.
Sorry for the inconvenience!
Sure:
1
2
3
if (a < b) //Now it is "before"
	std::swap(a, b);
sum = a - b;
Ah, Thanks so much, it works perfectly!
Registered users can post here. Sign in or register to post.