簡體   English   中英

使用數據結構從文件中讀取數據時出現問題

[英]Trouble reading in data from a file using data structures

我有一個包含租車信息和公司的程序。 我正在嘗試閱讀它,並在終端上顯示它。 但是,我只讓一家公司清楚地打印,而其他公司只打印出垃圾。 我也想以5個汽車的庫存來存儲代理商,但是在不讀取我所有信息的情況下,我不完全知道如何存儲它們。 我也只能使用C-Style字符串。

這是我正在閱讀的文件:

Hertz 93619
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1

Alamo 89502
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 112.83 1
2010 Toyota Corolla 50.36 1

Budget 93035
2008 Ford Fiesta 42.48 0
2009 Dodge Charger 55.36 1
2012 Chevy Volt 89.03 0
2007 Subaru Legacy 59.19 0
2010 Nissan Maxima 51.68 1

我需要幫助的代碼部分:

#include <iostream>
#include <fstream>
using namespace std;

struct car
{

        char agency[10];
        int zip;
        int year;
        char make[10];
        char model[10];
        float price;
        int available;

} ;

struct agency
{

    char company[10];
    int zip;
    int inventory[5];
};

void menu();


// Main Function
int main ()
{
    // declare variables
    const int carAmount = 15;
    int agencyAmount = 1;
    int choice;
    agency agencyLib[carAmount];
    car carLib[carAmount];
    char filename[10];
    ifstream carInData;
    bool menu1 = false;

    //prompt user for input file
    cout << " Enter file name: ";
    cin >> filename;

    // Start loop menu
    while(menu1 = true)
    {
        menu();
        carInData.open(filename);
        cin >> choice;

        if (carInData.is_open())
        {
            // read list of names into array

            for (int count = 0; count < agencyAmount; count++) 
            {
                carInData >> agencyLib[count].company >> agencyLib[count].zip;
                for (count = 0; count < carAmount; count++)
                {
                    carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
                }

            }

        }

    switch (choice)
    {
        // Case 1 closes menu
        case 1:
            return 0;
            break;
        // Case 2 displays if car is available if 1, unavailable if 0
        case 2:
        // itterate through car array
            for(int count = 0; count < agencyAmount; count++)
            {
                cout << agencyLib[count].company << " " << agencyLib[count].zip << "\n";
                for(int count = 0; count < carAmount; count++)
                {
                    // Displays if car is available or not 
                    /*      if (carLib[count].available == 1)
                    cout << " Available ";
                    else
                        cout << " Unavailable ";
                    */
                    // Display Cars
                    cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << "  " << "\n";
                }
            }
        }
    }
}

作為一般性的初步評論,我認為即使出於學習目的,這種練習也應使您最好使用std::strings代替c-strings和std::vector來保存越來越多的項目。

您的代碼有什么問題?

第一個問題是,你使用相同的計數器count來填充你的機構陣列和陣列車。 這將使您很快擁有超出數組邊界的計數器並破壞內存。

解決方案:使用2個不同的計數器重做循環結構。

下一個問題是您無法確定代理商的汽車清單的末尾。 這使得讀取多個代理不現實:您將在流讀取中遇到失敗,這將阻止您獲得任何有用的數據。

解決方案:分析讀取時的失敗,以識別從汽車(第一個元素應該是數字)到新代理商(第一個元素是字符串)的路程。

另外,您可能有一些比字符數組所允許的長度更長的字符串,從而導致進一步的內存損壞。

解決方案:限制使用iomanip()讀取的字符數以固定最大寬度。 強烈建議這樣做,除非您選擇std::string

最后一個問題:可變長度數組不是標准的C ++功能,即使某些流行的編譯器支持它也是如此。

解決方案:要么將動態分配與new / delete一起使用,要么為此練習選擇使用恆定的最大大小。

程式碼片段:

修改后,沒有選擇,菜單等,讀數將如下所示:

const int carAmount = 30;    // !!!
const int agencyAmount = 10;  // !!!
agency agencyLib[carAmount];
car carLib[carAmount];
ifstream carInData ("test.dat");
int numCar = 0, numAgency = 0;        // !!! shows the real number of items available
int count1, count2;                   // 

cout << "Start reading" << endl;
for (numAgency = numCar = 0; carInData && numAgency < agencyAmount; numAgency++) {
    if (!(carInData >> setw(sizeof(agencyLib[numAgency].company)) >> agencyLib[numAgency].company >> agencyLib[numAgency].zip))
        break;  // if nothing left, exit loop immediately
    for (; numCar < carAmount; numCar++) {
        carInData >> carLib[numCar].year >> setw(sizeof(carLib[numCar].make )) >>carLib[numCar].make 
                                         >> setw(sizeof(carLib[numCar].model))>>carLib[numCar].model 
                                         >> carLib[numCar].price >> carLib[numCar].available;
        if (carInData.fail()) { // here we expect a year, but get an agency string 
            carInData.clear(); 
            break;
        }
        strcpy(carLib[numCar].agency, agencyLib[numAgency].company);
        carLib[numCar].zip = agencyLib[numAgency].zip;
    }
}

以及隨后的顯示:

cout << "Display agencies: " << endl; 
for (count1 = 0; count1 < numAgency; count1++) {
    cout << agencyLib[count1].company << " " << agencyLib[count1].zip << "\n";
}
cout << "Cars: " << endl;
for (count2 = 0; count2 < numCar; count2++) {
    cout << carLib[count2].agency << " " << carLib[count2].zip << ": "; 
    cout << carLib[count2].year << " " << carLib[count2].make << " " << carLib[count2].model << " " << carLib[count2].price << "  " << "\n";
}

請注意,代理商和汽車之間沒有鏈接(公共字段除外),因此顯示僅顯示兩個不同的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM