This is an assignment for my c++ class im taking.
#include <iostream>
#include <fstream>
using namespace std;
const int NAMESIZE = 50, DATE = 25;
struct inven
{
char name[NAMESIZE];
int qnty;
double wholeCost;
double retailCost;
char date[DATE];
};
void displayMenu();
void addRecord(fstream &);
void displayRecord(fstream &);
void modifyRecord(fstream &);
int main()
{
fstream myFile;
int choice = 0;
displayMenu();
cin >> choice;
while(choice >= 1 && choice <=3)
{
switch (choice)
case 1:
addRecord(myFile);
break;
case 2:
displayRecord(myFile);
break;
case 3:
modifyRecord(myFile);
break;
}
cin.ignore();
cin.get();
return 0;
}
void displayMenu()
{
cout << "1: Add new records";
cout << "\n2: Display a record";
cout << "\n3: Change a program\n\n";
}
void addRecord(fstream &file)
{
char answr = 'Y';
inven Inventory;
file.open("Inventory.txt", ios::app | ios::binary);
do
{
cout << "Item name: ";
cin >> Inventory.name;
cout << "\nQuantity: ";
cin >> Inventory.qnty;
cout << "\nWholesale Cost: ";
cin >> Inventory.wholeCost;
cout << "\nRetail Cost: ";
cin >> Inventory.retailCost;
cout << "\nDate added to inventory: ";
cin >> Inventory.date;
file.write(reinterpret_cast<char *>(&file), sizeof(file));
cout << "\n\nDo you want to add another record:\n<y/n>";
cin >> answr;
}while(answr == 'y' || answr == 'Y');
file.close();
}
void displayRecord(fstream &file)
{
inven Inventory;
file.open("Inventory.txt", ios::in | ios::binary);
file.read(reinterpret_cast<char *>(&file), sizeof(file));
while(!file.eof())
{
cout << Inventory.name << "\n" << Inventory.qnty << "\n" << Inventory.wholeCost
<< "\n" << Inventory.retailCost << "\n" << Inventory.date << "\n";
file.read(reinterpret_cast<char *>(&file), sizeof(file));
}
file.close();
}
void modifyRecord(fstream &file)
{
}
Sorry, copy pasting the code ruined the format alittle, but the main gist is there.
I cant figure out why, but when I try to compile it is telling me that case 2 and case 3 are illegal cases. Please help!