Need help with this code!

Oct 8, 2014 at 2:54pm
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!
Oct 8, 2014 at 3:08pm
1
2
sum = a - b;
sum = abs(a - b);
Will this work for you?
Oct 8, 2014 at 3:30pm
No, unfortunately that didn't work, it still gave me a sum that left me with a negative answer.
Thanks though.
Oct 8, 2014 at 3:38pm
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
Oct 8, 2014 at 3:45pm
Just a question:

Why would you include iostream and stdio? It's literally the same thing.
Oct 8, 2014 at 3:48pm
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.
Oct 8, 2014 at 3:49pm
I have them both added from other things I was trying, most of the libraries included aren't actually being used.
Oct 8, 2014 at 3:58pm
@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 Oct 8, 2014 at 4:29pm
Oct 8, 2014 at 4:30pm
Show your code.
Oct 8, 2014 at 4:36pm
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;
}
Oct 8, 2014 at 4:40pm
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);
Oct 8, 2014 at 4:53pm
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!
Oct 8, 2014 at 5:02pm
Sure:
1
2
3
if (a < b) //Now it is "before"
	std::swap(a, b);
sum = a - b;
Oct 8, 2014 at 5:05pm
Ah, Thanks so much, it works perfectly!
Topic archived. No new replies allowed.