Mysterious error

Hi I am currently learning C++ from a book called "C++ programming for the absolute beginner, 2nd edition" and I came by an example in the book that tries to demonstrate the use of vectors. However when I keyed in the code in the example character for character and tried compiling it, I got an error which I couldn't understand.

The code is:

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <vector>
#include <string>

using std::string;
using std::vector;

#define MAX(a,b) a<b ? b:a 

struct Item
{
	string name;
	int price;
};

class Store
{
	vector<Item> inventory;
	vector<Item> forSale;
	int money;
public:
	Store(Item* itemList, int n);
	~Store(){}
	string BuyItem(int item);
	string viewInventory();
	string ListItems();
	int getMoney(){return MAX(money,0);}
};

Store::Store(Item* itemList, int n)
{
	for(int i=0; i<n;i++)
		forSale.push_back(itemList[i]);
	money=20;
}

string Store::BuyItem(int item)
{
	money-=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 bought a "+forSale[item-1].name+"\n";
}

string Store::ListItems()
{
	string s;
	for(int i=0; i<forSale.size();i++)
	{
		s+="[";
		s+=i+49;
		s+="] Buy a ";
		s+=forSale[i].name;
		s+=" ($";
		s+=forSale[i].price+48;
		s+=")\n";
	}
	return s;
}

string Store::viewInventory()
{
	string s;
	for(int i=0; i<inventory.size();i++)
		s+=inventory[i].name+'\n';
	return s+'\n';
}

int main(void)
{
	using std::cout;
	using std::cin;

	int input;
	Item f[3];
	f[0].name="Clown";
	f[0].price=2;
	f[1].name="Crack Jack";
	f[1].price=6;
	f[3].name="Camel";
	f[3].price=9;
	Store s=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;
			
}


The compiler gives back an error:

1
2
3
4
5
6
7
1
1>  main.cpp
1>c:\users\kenneth\documents\visual studio 2010\projects\testet\testet\main.cpp(49): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\kenneth\documents\visual studio 2010\projects\testet\testet\main.cpp(65): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\kenneth\documents\visual studio 2010\projects\testet\testet\main.cpp(82): warning C4789: destination of memory copy is too small
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>c:\users\kenneth\documents\visual studio 2010\Projects\testet\Debug\testet.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Can anyone please explain or point to me what might be the problem. I don't understand where the error could have occurred as I am supposed to be learning about vectors in the book and not be able to troubleshoot it already.
Thanks.
You should create a console project instead of windows project.
I can't test the code myself now, but the first 2 errors look like they are because you are comparing a signed int with an unsigned int. (int is signed, whereas size() returns an unsigned int because the size of the vector is always positive).

Try changing int i = 0; to unsigned int i = 0;.

The third error is not really what it says it is. It's because there is no f[3]. f[] holds 3 objects, so the last element isf[2] not f[3].

Change
1
2
f[3].name="Camel";
f[3].price=9;

to
1
2
f[2].name="Camel";
f[2].price=9;


I'm pretty sure the last error is caused by the other 3. Fix the code and see if it goes away.
Last edited on
Topic archived. No new replies allowed.