好的,这段代码使我丧命。

我的目标是从文件中读取数据,数据之间用逗号隔开,然后将数据加载到应该是“剧院座位”列表的结构数组中。剧院座位具有某些特征,例如“位置”,“价格”和“状态”。价格是不言而喻的。位置处理“座位”的行和座位号。状态与是否出售有关。之后,我必须解释从数据文件中提取的数据以进行显示,如果用户输入特定的选择,则可以轻松地对其进行操作。但这不是我要解决的问题。
我的问题是,从数据文件加载数据结构的最佳方法是什么?

让我向您展示一些我正在读取的数据文件。

1, 1, 50, 0
1, 2, 50, 0
1, 3, 50, 0
1, 4, 50, 0

为了解释数据文件,第一个数字是“行”,第二个数字是该行中的座位号,第三个数字是价格,最后一个数字(“0”)代表未售出的座位。如果购买了座位,则最终数量为1。

现在,这是我的代码。
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
using namespace std;

enum seatDimensions{ROWS = 10, SEATS_PER = 16};

//Structures
struct Location
{
    int     row;
    int     seatNumber;
};
struct Seat
{
    Location    seat_location[160];
    double      ticketPrice;
    int         status;
    int         patronID;
};

//GLOBALS
const int MAX = 16;

int main()
{
//arrays for our data
Seat        seatList[160];
//INDEX
int index = 1;
//filestream
fstream dataIn;

dataIn.open("huntington_data.dat",ios::in);
if(dataIn.fail())   //same as if(dataIn.fail())
{
    cout << "Unable to access the data file." << endl;
    return 999;
}

string temp;
getline(dataIn,temp,',');
seatList[index].seat_location[index].row = atoi(temp.c_str());
getline(dataIn,temp,',');
seatList[index].seat_location[index].seatNumber = atoi(temp.c_str());
getline(dataIn,temp,',');
seatList[index].ticketPrice = atof(temp.c_str());
getline(dataIn,temp,'\n');
seatList[index].status = atoi(temp.c_str());

    while(!dataIn.eof() && index < MAX)
    {
        index++;
        getline(dataIn,temp,',');
        seatList[index].seat_location[index].row = atoi(temp.c_str());
        getline(dataIn,temp,',');
        seatList[index].seat_location[index].seatNumber = atoi(temp.c_str());
        getline(dataIn,temp,',');
        seatList[index].ticketPrice = atof(temp.c_str());
        getline(dataIn,temp,'\n');
        seatList[index].status = atoi(temp.c_str());
    }
getch ();
return 0;
}

现在,从这里开始,我必须显示座位是否为假。
显示屏应该看起来像这样,因为尚未占用任何座位。
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 // 16 across
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
// 10 deep

我知道我没有正确输入数据,因为无论如何尝试显示,都似乎无法获得此显示。如果您能告诉我我要去哪里错了,请告诉我。任何建议将是惊人的。
另外,如果您有任何疑问要问我。考虑到这个问题,我试图尽可能具体一些,但是我知道它仍然很模糊。
请帮忙。

编辑:感谢那些回答了我的问题的人。我最终在数据结构上走了一条截然不同的路线,但从答案中获得了很多好处。

最佳答案

您遇到的问题是,每个座位在一个数组中都有一个位置,然后又有一个数组:

你需要:

Location    seat_location[160];

变成:
int seat_row;
int seal_num;

然后:
seatList[index].seat_location[index].row = atoi(temp.c_str());

变成:
seatList[index].seat_row = index / COLS ;
seatList[index].seat_num = index % COLS ;

您可能还需要考虑将数据实际排列成与座位尺寸相同的2D数组。

顺便说一句,从我的C背景出发,我建议阅读整行并使用sscanf,例如:
char temp[255];  // Use an appropriate maximum line length +3 for \r\n\0

fgets(dataIn, &temp);
sscanf(temp, "%d, %d, %d, %d", &.....

您也可以考虑使用正则表达式来实现。

10-08 13:41
查看更多