If Else fail

Hi

I have followed a book called C++ through Game Programming. Where I found a program called Hero's Inventory. I wanted to modify that program, but it I have a problem.

In the program you get to choose either if you want to trade your sword for a battle axe or not. But whatever you push, you accept the trade. So if you answer is N, you accept the trade. I just can't understand why..

Thanks for any help

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//Hero's Inventory 1.1
//Demonstrates the use of arrays

#include <iostream>
#include <string>

using namespace std;

int main()
{
	cout << "\tHero's Invetory 1.1\n\n";		

	const int MAX_ITEMS = 10;
	string inventory[MAX_ITEMS];
	int money = 100;

	int numItems = 0;
	inventory[numItems++] = "sword";
	inventory[numItems++] = "armor";
	inventory[numItems++] = "shield";

	cout << "Your money: " << money << "\n\n";
	cout << "Your items:\n";
	for (int i=0; i < numItems; ++i)
	{
		cout << inventory[i] << endl;
	}

	int tradeSwordAxe;
	cout << "\nSomeone ask if you want to trade your sword for an battle axe. (y/n)\n";
	cin >> tradeSwordAxe;


	if (tradeSwordAxe == 'y'||'Y')
	{
		cout << "You accepted.\n";
		inventory[0] = "battle axe";
		cout << "\nInventory:\n";
		
	}
	else if (tradeSwordAxe == 'n'||'N')
	{
		cout << "You declined.\n";
		cout << "\nInventory:\n";
		
	}
	else
	{
		cout << "Please answer either 'Y' or 'N'.\n";
	}
	{
		for (int i = 0; i < numItems; ++i)
		{
			cout << inventory[i] << endl;
		}
	}

	cout << "\nThe item name '" << inventory[0] << "'has ";
	cout << inventory[0].size() << " letters in it.\n";

	cout << "\nYou find a healing potion.\n";
	if (numItems < MAX_ITEMS)
	{
		inventory[numItems++] = "healing potion";
	}
	else
	{
		cout << "Your inventory is full.\n";
	}

	cout << "\nYour items:\n";
	for (int i = 0; i < numItems; ++i)
	{
		cout << inventory[i] << endl;
	}

	return 0;
}
 
	if (tradeSwordAxe == 'y'||'Y')


You can't specify two possible conditions like this. You have to type it as:

 
	if (tradeSwordAxe == 'y'|| tradeSwordAxe == 'Y')


This statement

if (tradeSwordAxe == 'y'||'Y')

is equivalent to

if ( ( tradeSwordAxe == 'y' ) || ( 'Y' != 0 ) )

As 'Y' always is unequal to 0 then the condition is always true. You should change your statement the following way


if ( ( tradeSwordAxe == 'y' ) || ( tradeSwordsAxe == 'Y' ) )
Ok, I have done so, but now I am facing another problem. Whatever input it get, the program say
Please answer either 'Y' or 'N'.
Please show your updated code.
This is the updated codes

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//Hero's Inventory 1.1
//Demonstrates the use of arrays

#include <iostream>
#include <string>

using namespace std;

int main()
{
	cout << "\tHero's Invetory 1.1\n\n";		

	const int MAX_ITEMS = 10;
	string inventory[MAX_ITEMS];
	int money = 100;

	int numItems = 0;
	inventory[numItems++] = "sword";
	inventory[numItems++] = "armor";
	inventory[numItems++] = "shield";

	cout << "Your money: " << money << "\n\n";
	cout << "Your items:\n";
	for (int i=0; i < numItems; ++i)
	{
		cout << inventory[i] << endl;
	}

	int tradeSwordAxe;
	cout << "\nSomeone ask if you want to trade your sword for an battle axe. (y/n)\n";
	cin >> tradeSwordAxe;


	if ((tradeSwordAxe == 'y') || (tradeSwordAxe =='Y'))
	{
		cout << "You accepted.\n";
		inventory[0] = "battle axe";
		cout << "\nInventory:\n";
		
	}
	else if ((tradeSwordAxe == 'n') || (tradeSwordAxe == 'N'))
	{
		cout << "You declined.\n";
		cout << "\nInventory:\n";
		
	}
	else
	{
		cout << "Please answer either 'Y' or 'N'.\n";
	}
	{
		for (int i = 0; i < numItems; ++i)
		{
			cout << inventory[i] << endl;
		}
	}

	cout << "\nThe item name '" << inventory[0] << "'has ";
	cout << inventory[0].size() << " letters in it.\n";

	cout << "\nYou find a healing potion.\n";
	if (numItems < MAX_ITEMS)
	{
		inventory[numItems++] = "healing potion";
	}
	else
	{
		cout << "Your inventory is full.\n";
	}

	cout << "\nYour items:\n";
	for (int i = 0; i < numItems; ++i)
	{
		cout << inventory[i] << endl;
	}

	return 0;
}
1
2
3
        int tradeSwordAxe;
	cout << "\nSomeone ask if you want to trade your sword for an battle axe. (y/n)\n";
	cin >> tradeSwordAxe;


tradeSwordAxe is of type int. When you enter a letter the input operation fails, and your if tests against the junk that was in tradeSwordAxe when you defined it, but didn't initialize it. The chances of that being any value you're expecting is rather small.


Last edited on
The problem is in these statements

1
2
3
int tradeSwordAxe;
cout << "\nSomeone ask if you want to trade your sword for an battle axe. (y/n)\n";
cin >> tradeSwordAxe;


You defined tradeSwordAxe as int. Then you enter input as, for example, Y, input stream cin sees that there is no a number and it stops to read and assigns to tradeSwordAxe value 0.

You should define tradeSwordAxe as type char
Topic archived. No new replies allowed.