I have a program for school. The program is an inventory program. It will start by writing blank records to a file. The user then should have a case drive menu. The user will select an option for what action they desire. Actions are add new record, change record, and display record. I have the basics for the code (no validation, formatting, etc) but my case statements wont work with my code. It errors out with error C2360 initialization of 'inventory2' is skipped by 'case' lable. How can I prevent this? I try to call the cases by function but not sure how to write to a file with a function. Any help would be great. I just want the thing to work at this point. Thank you for your suggestions/help in advance.
#include <iostream>
#include <fstream>
using namespace std;
const int DESC_SIZE = 31;
const int NUM_RECORDS = 5;
struct InventoryItem
{
char desc[DESC_SIZE];
int qty;
double price;
};
int main ()
{
InventoryItem record = {"",0,0.0};
long recNum;
char choice;
fstream inventory("Inventory.dat", ios::out | ios::binary);
//write the blank records
for (int count = 0; count < NUM_RECORDS; count++)
{
cout << "Now writing record " << count << endl;
inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
}
//close file
inventory.close();
cout << "What would you like to do?" << endl;
cout << "Please select an option below: " << endl;
cout << "\t1. Add new record" << endl;
cout << "\t2. Display a record" << endl;
cout << "\t3. Change any record" << endl;
cout << "\t4. Quit Program" << endl;
cin >> choice;
switch (choice)
{
case '1':
fstream inventory2("Inventory.dat", ios::in | ios::binary);
inventory2.read(reinterpret_cast<char *>(&record), sizeof(record));
while (!inventory2.eof())
{
cout << "Description: ";
cout << record.desc << endl;
cout << "Quantity: ";
cout << record.qty << endl;
cout << "Price: ";
cout << record.price << endl << endl;
inventory2.read(reinterpret_cast<char *>(&record),sizeof(record));
}
inventory2.close();
break;
case '2':
fstream inventory4("Inventory.dat", ios::in | ios::binary);
inventory4.read(reinterpret_cast<char *>(&record), sizeof(&record));
cout << "Which record do you want to view? ";
cin >> recNum;
inventory.seekg(recNum *sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record), sizeof(&record));
cout << "Description: ";
cout << record.desc << endl;
cout << "Quanitity: ";
cout << record.qty << endl;
cout << "Price: ";
cout << record.price << endl;
inventory4.close();
break;
case '3':
fstream inventory3("Inventory.dat", ios::in | ios::out | ios::binary);
cout << "Which record number do you want to edit? ";
cin >> recNum;
inventory3.seekg(recNum * sizeof(record), ios::beg);
inventory3.read(reinterpret_cast<char *>(&record), sizeof(record));
cout << "Description: ";
cout << record.desc << endl;
cout << "Quanity: ";
cout << record.price << endl;
cout << "Price: ";
cout << record.price << endl;
cout << "Enter the new data:\n";
cout << "Description: ";
cin.ignore();
cin.getline(record.desc, DESC_SIZE);
cout << "Quanitity: ";
cin >> record.qty;
cout << "Price: ";
cin >> record.price;
inventory3.seekp(recNum * sizeof(record), ios::beg);
inventory3.write(reinterpret_cast<char *>(&record), sizeof(record));
inventory3.close();
break;
case '3':
isDone = true;
break;
default:
cout << "Invalid choice!";
}
return 0;
}
Errors:
Error 1 error C2360: initialization of 'inventory2' is skipped by 'case' label c:\CIS234\final test\final test\please work please.cpp 66 final test
Error 2 error C2360: initialization of 'inventory4' is skipped by 'case' label c:\CIS234\final test\final test\please work please.cpp 87 final test
Error 3 error C2360: initialization of 'inventory2' is skipped by 'case' label c:\CIS234\final test\final test\please work please.cpp 87 final test
Error 4 error C2360: initialization of 'inventory3' is skipped by 'case' label c:\CIS234\final test\final test\please work please.cpp 118 final test
Error 5 error C2360: initialization of 'inventory4' is skipped by 'case' label c:\CIS234\final test\final test\please work please.cpp 118 final test
Error 6 error C2360: initialization of 'inventory2' is skipped by 'case' label c:\CIS234\final test\final test\please work please.cpp 118 final test
Error 7 error C2196: case value '3' already used c:\CIS234\final test\final test\please work please.cpp 118 final test
Error 8 error C2065: 'isDone' : undeclared identifier c:\CIS234\final test\final test\please work please.cpp 119 final test
Error 9 error C2361: initialization of 'inventory3' is skipped by 'default' label c:\CIS234\final test\final test\please work please.cpp 121 final test
Error 10 error C2361: initialization of 'inventory4' is skipped by 'default' label c:\CIS234\final test\final test\please work please.cpp 121 final test
Error 11 error C2361: initialization of 'inventory2' is skipped by 'default' label c:\CIS234\final test\final test\please work please.cpp 121 final test