Error: expected a statement
Feb 21, 2012 at 4:25pm UTC
Hi
I'm using a book called "C++ through game programming, third edition".
I found a program in that book called "Hero's Inventory". Now I want to modify this program, but I'm facing an error. The part of the program I want to modify, is the trade. In the first version, you traded your sword for a battle axe, without any choise, but noe I want to give the player a choise. But I have an error with the If statement. Here is the code. Can anyone help me find out what's wrong? I have written the errors in the program.
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
//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" ;
for (int i = 0; i < numItems; ++i)
} //Error: expected a statement.
else
{
cout << "You declined.+n" ;
cout << "\nInventory:\n" ;
for (int i = 0; i < numItems; ++i)
} //Error: expected a statement.
{
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;
}
Feb 21, 2012 at 4:28pm UTC
You have empty for loops.
For syntax
1 2 3 4
for (int i=0; i<numItems; i++)
{
// Statements go here
}
Topic archived. No new replies allowed.