I'm working on this project for my C++ class. When I run the program, all I get is File did not open! Program terminating! Here is my code. Can somebody take a look at it and help me figure out what I'm doing wrong so I can get the entire program to run?
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 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);
}
{
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";
}
I have it in the directory. I noticed that I have ShoppingCart.txt as my inFile and ShoppingCart.txt as my outFile. Would that matter or would that need to be changed?