1

私の C++ の宿題には、次のような順序で建物情報を含む .txt ドキュメントがあります。

建物名 築年 緯度座標 経度座標

元。

Parking Deck
1993
34.2252
37.5563
Admin Building
1985
34.2356
37.5734

これを、作成した構造体の配列に読み込む必要があります。

struct list 
{
    char name[50];
    int yearBuilt;
    double latCoord;
    double lonCoord;
} building;

これで、作成した型リストの配列にデータを読み込むための for ループを作成しました。

list buildingnumber[SIZE]; //array for buildings

しかし、「k」個の最も古い建物を印刷しようとすると、配列にデータが表示されません

これが私の現在のコードです:

#include <iostream>
#include <fstream>
#include <istream>
#include <cstdlib>
#include <string>

using namespace std;
const int SIZE = 5000;

//struct for building type
struct list 
{
    char name[50];
    int yearBuilt;
    double latCoord;
    double lonCoord;
}building;

list buildingnumber[SIZE]; //array for buildings

void InsertionSort(list buildingnumber[], int buildingsloaded)
{
    int key = 0, i = 0;
    for(int j = 1; j < buildingsloaded; j++)
    {
        key=buildingnumber[j].yearBuilt;
        i=j-1;
        while(buildingnumber[i].yearBuilt > key && i >= 0)
        {
            buildingnumber[i+1] = buildingnumber[i];
            i--;
        }
        buildingnumber[i+1].yearBuilt = key;
    }
}

int main()
{
    char filePath[50];
    ifstream openFile;
    cout << "Enter the path of the building file: ";
    cin.getline(filePath, 50);
    openFile.open(filePath);

    //verify if file is opened + report buildings loaded
    int buildingsloaded = 0;
    if(!openFile.fail())
    {
        while(openFile >> building.name >> building.yearBuilt >> building.latCoord >> building.lonCoord)
        {
            buildingsloaded++;
        }
        cout << buildingsloaded << " buildings have been loaded." << endl;
    }

    // get how many buildings user wants
    int k = 0;
    cout << "How many buildings are you interested in?: ";
    cin >> k;

    //create array
    // loadKbuildings(building, buildingsloaded);
    for(int i = 0; i < k; i++)
    {
        openFile >> buildingnumber[i].name >> buildingnumber[i].yearBuilt >> buildingnumber[i].latCoord >> buildingnumber[i].lonCoord;  
    }

    // insertion sort
    InsertionSort(buildingnumber, buildingsloaded);

    // display earliest k buildings
    cout << "The " << k << " oldest buildings are: " << endl;
    int i = 0;
    while ( i < k )
    {
        cout << buildingnumber[i].name << endl;
        cout << "Year Built: " << buildingnumber[i].yearBuilt << endl;
        cout << "Coordinates: (" << buildingnumber[i].latCoord << "," << buildingnumber[i].lonCoord << ")" << endl;
        cout << endl;
        i++;
    }
}
4

1 に答える 1

2

問題はここにあります:

if(!openFile.fail())
{
    while(openFile >> building.name >> building.yearBuilt >> building.latCoord >> building.lonCoord)
    {
        buildingsloaded++;
    }
    cout << buildingsloaded << " buildings have been loaded." << endl;
}

すでにすべてのデータを読み取っています (建物の数をカウントするため)。次にデータを取得しようとすると、データは失われています。

この問題は、最初のスキャンで建物を配列に格納することで解決できます。

int c = 0;
if(!openFile.fail())
{
    // *
    while(openFile >> buildingnumber[c].name >> buildingnumber[c].yearBuilt >> buildingnumber[c].latCoord >> buildingnumber[c].lonCoord)
    {
        buildingsloaded++;
        c++;
    }
    cout << buildingsloaded << " buildings have been loaded." << endl;
}

WhozCraig のコメントによると、星の下の行は建物の名前を 1 語で表すだけです。代わりに使用cin.getline()して、ループ条件を変更する必要があります。

明らかに、以下のデータ読み取りセクションも取り出す必要があります。

//create array
// loadKbuildings(building, buildingsloaded);
for(int i = 0; i < k; i++)
{
    openFile >> buildingnumber[i].name >> buildingnumber[i].yearBuilt >> buildingnumber[i].latCoord >> buildingnumber[i].lonCoord;  
}
于 2012-11-11T02:48:25.563 に答える