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

1. In general, you shouldn't/can't initialize any variables inside of a switch statement. This has caused me problems before as well. Try initializing your fstreams before of the switch statement.

2. The bool isDone is never initialized. You need to do this outside of the switch also

3. I would put all of the code from each branch of the switch statement into its own function. This will make it much easier to read your code. Also, if you do that, you won't have to initialize the fstreams before the switch (iirc). It never hurts to put all large functional sections ( big pieces of the code that do an independent action ) into functions. This will improve the appearance and readability of your code.

Try these suggestions out, and see if it helps.

UPDATE::::::
I was able to get the cases to work. I forgot the {}. Next question, how to run these from functions instead of code on each case?

I am having trouble getting into a function. I am not the best at the file operations part and not sure how to get a function to do this part. My book for scool only shows snippets of code and doesnt show an exampe of calling functions for the examples.

void addRecord(){
    fstream inventory("Inventory.dat", ios::in | ios::binary);
    //do stuff with the invetory stream
    inventory.close();
}

int main ()
{
    //do stuff
    switch (choice)
    {
    case '1':
        addRecord();
        break;
    //Rest of swtich
    }
    //Rest of main
    return 0;
}

That should get you started. You would just have a different function for each part of the switch.

Thank you very much. You are a life saver. I have been looking at this for 2 days straight re reading book etc with no luck. Thank you again.

This article has been dead for over six months. Start a new discussion instead.