I'm currently getting a fatal error LNK1120: 1 resolved externals. Can somebody help me figure out what is going on?


#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

const int MAXCHAR = 101;
const int CAP = 100;

struct Cart
{
char itemName[MAXCHAR];
char itemPrice[MAXCHAR];
char itemTotal[MAXCHAR];
};

void menu();
char getCommand();
void exeCommand(Cart [], int &, char);
void addItem(Cart [], int &);
void showList(Cart [], int &);
void findCart(Cart [], int &);
void loadData(Cart [], int &);
void addItem(ifstream &, Cart [], int &);
void saveData(Cart [], int &);

int main(void)
{
Cart shopping[CAP];
int size = 0;
char option;
loadData(shopping, size);
do
{
menu();
option = getCommand();
exeCommand(shopping, size, option);
}while(tolower(option) != 'q');
}

void menu()
{
cout << "Welcome to Smart Shopping Cart! :\n"
<< "(a) to add an item\n"
<< "(s) to show the items list\n"
<< "(f) to find an item by item name\n"
<< "(q) to quit\n";
cout << "Please enter an option: ";
}

char getCommand()
{
char ans;
cin.get(ans);
switch (tolower(ans))
{
case 'a':
case 's':
case 'f':
case 'q':
cin.ignore(100, '\n');
return ans;
default:
cout << "Illegal input.";
cin.clear();
cin.ignore(100, '\n');
system("cls");
menu();
return getCommand();
}

}

void exeCommand(Cart list[], int &size, char ans)
{
switch(tolower(ans))
{
case 'a':
addItem(list, size);
saveData(list, size);
break;
case 's':
showList(list, size);
break;
case 'f':
findCart(list, size);
break;
case 'q':
cout << "Thanks for using Smart Shopping Cart. Goodbye!" << endl;
return;
default:
return;
}
}

void loadData(Cart list[], int &size)
{
ifstream inFile;
inFile.open("ShoppingCart.txt");
if(!inFile)
{
cout << "File did not open! Program terminating!" << endl;
exit(1);
}
while(!inFile.eof())
{
addItem(inFile, list, size);
}
}

void addItem(Cart list [], int &size)
{
char name[MAXCHAR];
char itemPri[MAXCHAR];
char total[MAXCHAR];
char junk;
cout << "Enter item Name (less than 101 characters.): ";
cin.get(name, MAXCHAR, '\n');
cin.get(junk);
cout << "Enter item price (less than 101 characters.): ";
cin.get(itemPri, MAXCHAR, '\n');
cin.get(junk);
cout << "Enter item price (less than 101 characters.): ";
cin.get(total, MAXCHAR, '\n');
cin.get(junk);
strcpy_s(list.itemName, name);
strcpy_s(list.itemPrice, itemPri);
strcpy_s(list.itemTotal, total);
size++;
}

void addItem(ifstream &inFile, Cart list[], int &size)
{
inFile.get(list.itemName, MAXCHAR, ';');
inFile.ignore(50, ';');
inFile.get(list.itemPrice, MAXCHAR, ';');
inFile.ignore(50, ';');
inFile.get(list.itemTotal, MAXCHAR, ';');
inFile.ignore(50, '\n');
size++;
}

void showList(Cart list[], int &size)
{
int i;
for(i = 0; i < size; i++)
{
cout << left << list.itemName << ";"
<< left << list.itemPrice << ";"
<< left << list.itemTotal << endl;
}
}

void saveData(Cart list[], int &size)
{
int i = 0;
ofstream outFile;
outFile.open("ShoppingCart.txt");
if(!outFile)
{
cout << "File did not open! Program terminating!" << endl;
exit(1);
}

for(i = 0; i < size; i++)
{
outFile << list.itemName << ';'
<< list.itemPrice << ';'
<< list.itemTotal << endl;
}
}

void findCart(Cart list[], int &size)

{
int i = 0;
char junk;
char shopping[MAXCHAR];
cout << "This will allow you to search for an item in a shopping cart." << endl;
cout << "Enter Item Name (less than 101 characters.): ";
cin.get(shopping, MAXCHAR, '\n');
cin.get(junk);
if (strcmp(list.itemName, shopping) == 0)
cout << "found it";
}

This compiled fine for me other than the fact I had to include cstdlib (for system()) and cstring (for strcmp() and strcpy()) and I had to change strcpy_s() to strcpy().

This compiled fine for me other than the fact I had to include cstdlib (for system()) and cstring (for strcmp() and strcpy()) and I had to change strcpy_s() to strcpy().

I tried doing that, but for some reason, it's not compiling for me. Can I see what you did just to make sure I did it the same way?

I commented the lines that I changed but I'll give the line numbers too.

Added lines 4, 5.
Modified lines 128, 129, 130.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib> //added
#include <cstring> //added

using namespace std;

const int MAXCHAR = 101;
const int CAP = 100;

struct Cart
{
	char itemName[MAXCHAR];
	char itemPrice[MAXCHAR];
	char itemTotal[MAXCHAR];
};

void menu();
char getCommand();
void exeCommand(Cart [], int &, char);
void addItem(Cart [], int &);
void showList(Cart [], int &);
void findCart(Cart [], int &);
void loadData(Cart [], int &);
void addItem(ifstream &, Cart [], int &);
void saveData(Cart [], int &);

int main(void)
{
	Cart shopping[CAP];
	int size = 0;
	char option;
	loadData(shopping, size);
	do
	{
		menu();
		option = getCommand();
		exeCommand(shopping, size, option);
	}while(tolower(option) != 'q');
}

void menu()
{
	cout << "Welcome to Smart Shopping Cart! :\n"
	<< "(a) to add an item\n"
	<< "(s) to show the items list\n"
	<< "(f) to find an item by item name\n"
	<< "(q) to quit\n";
	cout << "Please enter an option: ";
}

char getCommand()
{
	char ans;
	cin.get(ans);
	switch (tolower(ans))
	{
		case 'a':
		case 's':
		case 'f':
		case 'q':
			cin.ignore(100, '\n');
			return ans;
		default:
			cout << "Illegal input.";
			cin.clear();
			cin.ignore(100, '\n');
			system("cls");
			menu();
			return getCommand();
	}

}

void exeCommand(Cart list[], int &size, char ans)
{
	switch(tolower(ans))
	{
		case 'a':
			addItem(list, size);
			saveData(list, size);
			break;
		case 's':
			showList(list, size);
			break;
		case 'f':
			findCart(list, size);
			break;
		case 'q':
			cout << "Thanks for using Smart Shopping Cart. Goodbye!" << endl;
			return;
		default:
			return;
	}
}

void loadData(Cart list[], int &size)
{
	ifstream inFile;
	inFile.open("ShoppingCart.txt");
	if(!inFile)
	{
		cout << "File did not open! Program terminating!" << endl;
		exit(1);
	}
	while(!inFile.eof())
	{
	addItem(inFile, list, size);
	}
}

void addItem(Cart list [], int &size) //modified this function as marked
{
	char name[MAXCHAR];
	char itemPri[MAXCHAR];
	char total[MAXCHAR];
	char junk;
	cout << "Enter item Name (less than 101 characters.): ";
	cin.get(name, MAXCHAR, '\n');
	cin.get(junk);
	cout << "Enter item price (less than 101 characters.): ";
	cin.get(itemPri, MAXCHAR, '\n');
	cin.get(junk);
	cout << "Enter item price (less than 101 characters.): ";
	cin.get(total, MAXCHAR, '\n');
	cin.get(junk);
	strcpy(list[size].itemName, name); //changed from strcpy_s
	strcpy(list[size].itemPrice, itemPri); //changed from strcpy_s
	strcpy(list[size].itemTotal, total); //changed from strcpy_s
	size++;
}

void addItem(ifstream &inFile, Cart list[], int &size)
{
	inFile.get(list[size].itemName, MAXCHAR, ';');
	inFile.ignore(50, ';');
	inFile.get(list[size].itemPrice, MAXCHAR, ';');
	inFile.ignore(50, ';');
	inFile.get(list[size].itemTotal, MAXCHAR, ';');
	inFile.ignore(50, '\n');
	size++;
}

void showList(Cart list[], int &size)
{
	int i;
	for(i = 0; i < size; i++)
	{
		cout << left << list[i].itemName << ";"
		<< left << list[i].itemPrice << ";"
		<< left << list[i].itemTotal << endl;
	}
}

void saveData(Cart list[], int &size)
{
	int i = 0;
	ofstream outFile;
	outFile.open("ShoppingCart.txt");
	if(!outFile)
	{
		cout << "File did not open! Program terminating!" << endl;
		exit(1);
	}

	for(i = 0; i < size; i++)
	{
		outFile << list[i].itemName << ';'
		<< list[i].itemPrice << ';'
		<< list[i].itemTotal << endl;
	}
}

void findCart(Cart list[], int &size)
{
	int i = 0;
	char junk;
	char shopping[MAXCHAR];
	cout << "This will allow you to search for an item in a shopping cart." << endl;
	cout << "Enter Item Name (less than 101 characters.): ";
	cin.get(shopping, MAXCHAR, '\n');
	cin.get(junk);
	if (strcmp(list[i].itemName, shopping) == 0)
		cout << "found it";
}

Yup, that's what I did. Here is what it is showing at the bottom of the screen:

------ Build started: Project: ShoppingCart, Configuration: Debug Win32 ------
Build started 10/9/2011 7:13:56 PM.
InitializeBuildStatus:
Touching "Debug\ShoppingCart.unsuccessfulbuild".
ClCompile:
ShoppingList.cpp
c:\users\brian williams\desktop\shoppingcart\shoppingcart\shoppinglist.cpp(128): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
c:\users\brian williams\desktop\shoppingcart\shoppingcart\shoppinglist.cpp(129): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
c:\users\brian williams\desktop\shoppingcart\shoppingcart\shoppinglist.cpp(130): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
ManifestResourceCompile:
All outputs are up-to-date.
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Users\Brian Williams\Desktop\ShoppingCart\Debug\ShoppingCart.exe : fatal error LNK1120: 1 unresolved externals

Build FAILED.

Time Elapsed 00:00:03.59
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I know the warnings aren't anything to worry about because the program will still run with a warning.

It looks like you are using visual studio C++, correct? If so you have to be very careful what project type you use and your main() function entry point name. For a Win32 Console Application you have to use main() or _tmain(). If you make a Win32 Project then you have to use WinMain() or _tWinMain() but you cannot use main() or _tmain().

If you want to keep main() then you gotta make sure that you have the correct project type.

Also after compiling in VS C++ I got the same warnings for using strcpy() so sorry I said to change it but I would revert to strcpy_s() (for some reason code::blocks does not have that function or something even tho I included cstring and string.h and it throws no warnings).

Yeah. My professor wants us to use Visual Studio 2010. I went back to double check everything just to make sure that I have the right file that I want to use. Sure enough, I do. I'll ask my professor tomorrow morning about it. Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.