Help with variable?

I am actually not sure where to put this, but I have kind of a problem.
So if you see the code that I have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

/*This will be a
random test for my own use*/

int main()
{
	int steal;
	int golds = 50;
	int enemyGold = 2500;
	int resultIng = golds + steal;
	cout << "You have " << golds << " gold, while enemy have " << enemyGold << " gold" << endl;
	cout << "How much do you want to steal?: " << endl;
	cin >> steal;
	cout << "You stole " << steal << " gold" << endl;
	cout << "You now have " << resultIng << " gold" << endl;
	cout << "Enemy have " << enemyGold - resultIng << endl;
	
	cin.get();
	cin.get();
	return 0;
}



This is just a console command where I want to make the user "steal" money from the enemy
As you start with 50 Gold, and the enemy has 2500, I want you to be able to steal from him, after user input it shows you results how much you have now, how much you stole, and how much the enemy have
This is only a test for myself and not going anywhere, but I can't seem to fix the mistake in my code

Anyone care to help?
I'd start with this line:

int resultIng = golds + steal;

The steal variable isn't initialised, so it contains a complete junk value. You're adding that to the result, making this more or less random.

Also, when you're stealing you want to make sure the value actually comes off the enemy's stash. This line isn't doing that:
cout << "Enemy have " << enemyGold - resultIng << endl;

It's simply printing a temporary variable, created from the subtraction there.

I don't think you need the result variable at all. You could do this without it. Something like:
1
2
3
4
5
6
7
8
9
10
int gold = 50,
    enemy_gold = 2500,
    steal_amount = 0;

/* Code to get steal amount here */

gold += steal_amount;
enemy_gold -= steal_amount;

/* Print how much you have here */


Hope this helps.
Last edited on
Thank you so much!
This really helped me, and it worked :)

You also made me realize I could make different variables easier with a comma,
here as in

1
2
3
int gold = 50,
     enemy_gold = 2500,
     steal_amount = 0;


instead of

1
2
3
int gold = 50;
int enemy_gold = 2500;
int steal_amount = 0;


Thank you!
You're welcome. :-)
Topic archived. No new replies allowed.