Cannot figure out, why this isn't running entirely.

It starts the program and it prints half of what it is supposed to and then stops and says windows cannot run it. 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
// Hero's Inventory
// Demonstrates arrays

#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int MAX_ITEMS = 10;
	string inventory[MAX_ITEMS];

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

	cout << "Your items: \n";
	for (int i = 0; i < numItems; ++i)
	{
		cout << inventory[i] << endl;
	}
	cout << "\nYou trade your sword for a battle axe.";
	inventory[0] = "battle axe";
	cout << "\nYour items:\n";
	for (int i = 0; numItems; ++i)
	{
		cout << inventory[i] << endl;
	}

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

	cout << "\nYou find healing potion.";
	if (numItems < MAX_ITEMS)
	{
		inventory[numItems++] = "healing potion";
	}
	else
	{
		cout << "You have to many items and cannot carry another.";
	}

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

	return 0;
}
int numItems = 0;
inventory[numItems++] = "sword";
inventory[numItems++] = "armor";
inventory[numItems++] = "shield";
***YOu are starting your array from 1 instead of 0.

///////////////////////////////////////////////////////////////////

cout << inventory[0].size() << " letters in it.\n";
This has a mistake figured out by commenting.
well I dont actually know what your answer is saying, I believe your right, I do, Im sure you wouldn't have answered otherwise. But I just rewrote the entire reference and ran it and it worked so I am going to go through and just look to see what I typed wrong, thank you though.
There is nothing wrong with the code biplav17 pasted. While ugly (cramming multiple things onto one line), it works just fine.

The problem is on line 27:

for (int i = 0; numItems; ++i)

Take a look at that loop condition. Since numItems is nonzero, it will loop forever.


If you are using a debugger, you actually could have been able to find this pretty quick.
That's what I was thinking was the problem (an infinite loop that is) cause I didn't know why else, it would screw up in the middle of running when it had already made some output but that's the thing I was looking through and since it ran, when it quit it just seemed to think that it was a successful run and printed off the time it took to run and didn't think there was anything wrong, I didn't give it the most thorough run over when looking for the program cause I knew it was simple I just said, "screw it, let them find it for me; Ill just re-type it (its from a book I am using to teach myself programming, and C++) and that way hopefully I type it write this next time and I can keep reading and learn what he was trying to teach me and come back to figure out what the stupid problem was after I have learned what I was supposed to from the lesson.
That was a pretty long run-on sentence that I couldn't really follow. But... uh... sounds good. =P
And now that I look at the code in the book with where you identified the problem, I see:
 
for (int i = 0; i < numItems; ++i)

I forgot the "i <"
Thank you :D
Topic archived. No new replies allowed.