Confused. Please help

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?

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

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[size].itemName, name);
strcpy_s(list[size].itemPrice, itemPri);
strcpy_s(list[size].itemTotal, total);
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";
}
It's not finding your ShoppingCart.txt file.

Make sure that file exists and is in the correct directory.
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?
They should be different files
Topic archived. No new replies allowed.