将数据从文本文件读取到结构数组中 [英] Reading data from a text file into an array of structs

查看:68
本文介绍了将数据从文本文件读取到结构数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前已经看过这个问题,但是找不到能帮助我解决问题的答案.这里发生了一些事情.这将显示一个菜单,该菜单将允许用户执行以下操作:

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. 查看费用
  2. 更新费用,或
  3. 退出以返回上一级菜单.

此信息保存到应输入和输出到的数据文件中.尝试查看文件中的数据时,第一个菜单选项有问题.假设已经有一个包含以下信息的数据文件 C:\ danceexpenses.txt :

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();
}

推荐答案

如果您的第一个动作是输入1,则代码应输入:

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;

这将打开舞蹈费用文件,但不会从中读取.然后,它遍历尚未初始化的费用列表,并关闭输入文件.还有一个未使用的变量 output .

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;

这篇关于将数据从文本文件读取到结构数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆