简体   繁体   中英

Reading data from a text file into an array of structs

I've seen this question asked before but I can't find an answer that helps me solve my problem. There's a few things happening here. This is displaying a menu that will allow the user to:

  1. View expenses,
  2. Update expenses, or
  3. Quit to return to a previous menu.

This information is saved to a data file that should be input and output to. I have a problem with the first menu option when trying to view the data in the file. Assume there is already a data file C:\\danceexpenses.txt containing this information:

DJ
100
Site
100
Food
100
Favors
100
Security
100
Ticket printing
100
Etc.
100
etc.
100

The program will not display the information. It only gives a blank line for each. Is there something I need to add to configure the output?

void charges()
{
    //Structures to hold data
    struct Expenses
    {
        string name; 
        double cost;
    };

    int chargesChoice;
    const int CHARGES_MENU_VIEW = 1, CHARGES_MENU_UPDATE = 2, CHARGES_MENU_QUIT = 3;
    const int NUM_EXPENSES = 8; //number of expenses
    Expenses danceExpenses[NUM_EXPENSES]; //array of structures
    int index; //loop counter

    cout << "\nWhat would you like to do?\n\n"
        << "1. View charges\n"
        << "2. Update charges\n"
        << "3. Return to main menu\n"
        << "Enter your choice: ";
    cin >> chargesChoice;

    while (chargesChoice < CHARGES_MENU_VIEW || chargesChoice > CHARGES_MENU_QUIT)
    {
        cout << "\nPlease enter a valid menu choice: ";
        cin >> chargesChoice;       
    }

    switch (chargesChoice)
    {
        case CHARGES_MENU_VIEW: //Display the expenses data
            myFile.open("c:\\danceexpenses.txt", ios::in); //open file
            if (!myFile)
            {
                cout << "Error opening file. Program aborting.\n";
                return;
            }
            char output[100];
            cout << "\nHere are the expected expenses for the dance:\n";
            for (index = 0; index < NUM_EXPENSES; index++)
                cout << danceExpenses[index].name << endl << endl;
            myFile.close(); //close file
            break;
        case CHARGES_MENU_UPDATE:
            //get expense data
            myFile.open("c:\\danceexpenses.txt", ios::out); //open file
            if (!myFile)
            {
                cout << "Error opening file. Program aborting.\n";
                return;
            }
            for (index = 0; index < NUM_EXPENSES; index++)
            {
                cout << "Enter the name of the expense: ";
                cin.ignore();
                getline(cin, danceExpenses[index].name);
                cout << "Enter the expected cost for this expense: ";
                cin >> danceExpenses[index].cost;
            }

            //Write data to file
            for (index = 0; index < NUM_EXPENSES; index++)
            {
                myFile << danceExpenses[index].name << endl;
                myFile << danceExpenses[index].cost << endl;
            }
            myFile.close(); //close file
            break;
        case CHARGES_MENU_QUIT:
            showMenu();
            break;
    }
    charges();
}

If your first action is to enter 1, the code should enter:

    case CHARGES_MENU_VIEW: //Display the expenses data
        myFile.open("c:\\danceexpenses.txt", ios::in); //open file
        if (!myFile)
        {
            cout << "Error opening file. Program aborting.\n";
            return;
        }
        char output[100];
        cout << "\nHere are the expected expenses for the dance:\n";
        for (index = 0; index < NUM_EXPENSES; index++)
            cout << danceExpenses[index].name << endl << endl;
        myFile.close(); //close file
        break;

This opens the dance expenses file, but doesn't read from it. It then iterates through a list of expenses that have not been initialized, and closes the input file. There's also an unused variable output .

You need to read the data into memory before printing it out.

int num = 0;
while (myFile >> danceexpenses[num].name >> danceexpenses[num].cost)
    num++;

for (index = 0; index < num; index++)
    cout << danceExpenses[index].name << endl << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM