|
From: Danny A. <tam...@ms...> - 2002-04-13 02:41:17
|
I was wondering if someone could help me I typed this code in from a book=
C++ for the absolute beginner (I am one) and I seem to be having some di=
fficulty. Look below
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define MAX(a, b) a<b ? b: a //the common macro
struct Item //used to store a single itme fro=
m the store
{
string name;
int price;
};
class Store //used to handle everything to do=
with the store
{
vector<item> inventory;
vector<item> forSale;
int money;
public:
Store(Item* itemList, int n);
=20
~Store() {}
=20
string BuyItem(int item);
=20
string viewInventory();
=20
string ListItems();
=20
int getMoney()
{
return MAX(money,0);
}
};
Store::Store(Item* itemList, int n)
{
for(int i =3D 0; i < n; i++)
forSale.push_back(itemList[i]);
money =3D 20;
}
string Store::BuyItem(int item)
{
money -=3D forSale[item-1].price;
if (money < 0)
return "\nSorry, you don't have enough money.\n\n";
inventory.push_back(forSale[item-1]);
return "You boght a " + forSale[item-1].name + '\n';
}
string Store::ListItems()
{
string s;
for(int i =3D 0; i < forSale.size(); i++)
{
s +=3D "[";
s +=3D i + 49;
s+=3D"]Buy a ";
s+=3D forSale[i].name;
s+=3D " ($";
s+=3D ")\n";
}
return s;
}
string Store::viewInventory()
{
string s;
for(int i =3D 0; i < inventory.size(); i++)
s +=3D inventory[i].name + '\n';
return s + '\n';
}
int main(void)
{
int input;
Item f[3];
f[0].name =3D "Clown";
f[0].price =3D 2;
f[1].name =3D "Cracker Jack";
f[1].price =3D 6;
f[2].name =3D "Camel";
f[2].price =3D 9;
Store s =3D Store(f,3);
while(true)
{
do {
cout << "Welcome to the store.\n"
<< "You have " << s.getMoney()
<< " dollars.\n"
<< "\nWhat would you like to do?\n"
<< s.ListItems()
<< "[4]View your inventory\n"
<< "[5]Leave\n";
cin >> input;
}while(input<1||input>5);
switch(input)
{
case 4:
cout << s.viewInventory();
break;
case 5:
goto END;
default:
cout << s.BuyItem(input);
}
}
END:
cout << "See Ya!";
return 0;
}
AND HERE ARE THE ERRORS I GOT!!
LINE 17 'item was not declared in this scope
template 1 argument is invalid
template 2 argument is invalid
ANSI C++ forbids declaration 'inventory' with no type
LINE 18 THE SAME THING AS 17
LINE 40 request for member 'push_back' in 'Store::forSale', which is not =
aggregat type
LINE 46 Invalid types 'int[int]' for array subscript
LINE 49 Invalid types 'int[int]' for array subscript
LINE 50 Invalid types 'int[int]' for array subscript
LINE 56 request for member 'size' in 'Store::forSale', not aggregat type
LINE 61 Invalid types 'int[int]' for array subscript
LINE 71 request for member 'size' in 'Store::inventory', not aggregat typ=
e
LINE 72 Invalid types 'int[int]' for array subscript
|