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!

Dani AI

Generated

was on the right track: the compiler's "illegal case" message means the case labels are not inside a switch body. In the posted main the switch(choice) is missing its opening brace, so the compiler treats the case lines as stray labels. Add the braces and a default to make the structure explicit, for example:

switch (choice) {
    case 1: addRecord(myFile);    break;
    case 2: displayRecord(myFile); break;
    case 3: modifyRecord(myFile);  break;
    default: /* handle invalid input */ break;
}

A few other critical fixes and improvements that will prevent runtime bugs and data corruption:

  • The file write/read calls are using the address and size of the stream object instead of the inventory struct. Replace those with the inventory variable and its size:

    file.write(reinterpret_cast<char*>(&Inventory), sizeof(Inventory));

    and when reading use a loop that tests the read result:

    while (file.read(reinterpret_cast<char*>(&Inventory), sizeof(Inventory))) {
      /* print Inventory fields */
    }

    Avoid using while(!file.eof()) — test the read directly.

  • Re-prompt or change choice inside the menu loop. Currently choice is read once before the while, so the loop either never runs or becomes infinite. A simple pattern:

    do {
      displayMenu();
      cin >> choice;
      switch(choice) { ... }
    } while (choice >= 1 && choice <= 3);
  • Check file.is_open() after open(), use ios::binary consistently for binary records, and prefer std::string or cin.getline() if item names can contain spaces. Also note that modifyRecord is empty — open with ios::in | ios::out | ios::binary and plan record-seeking by seekg/seekp.

These changes fix the compile error and address the more subtle I/O mistakes that would otherwise corrupt data.

Recommended Answers

All 3 Replies

'{' missing somthing '}'
'{' missing somthing '}'

I dont get it =(

Ohhh.....

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.