我是C ++的初学者,我目前正在尝试将头围在数组上。我有一个作业,需要从csv文件中读取数据,将其存储到结构化数组中,然后要求用户输入城市名称。基于该城市名称,我需要输出该城市的信息。输入城市名称时出现问题,返回的信息不正确。我似乎无法正确解决。任何对此的指导和对我要去哪里的解释将不胜感激。
csv文件上的小信息块:
State,City,FAA,IATA,ICAO,Airport,Role,Enplanements
ALABAMA,Birmingham,BHM,BHM,KBHM,Birmingham–Shuttlesworth International Airport,S,1335215
ALABAMA,Dothan,DHN,DHN,KDHN,Dothan Regional Airport,N,48423
ALABAMA,Huntsville,HSV,HSV,KHSV,Huntsville International Airport (Carl T. Jones Field),S,505541
ALABAMA,Mobile,MOB,MOB,KMOB,Mobile Regional Airport,N,287661
ALABAMA,Montgomery,MGM,MGM,KMGM,Montgomery Regional Airport (Dannelly Field),N,157958
这是我到目前为止写的:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
struct airplane_Data
{
string state;
string city;
string faa;
string iata;
string icao;
string airport_Name;
string role;
string enplanements;
};
const int C = 8; // Columns: Name of sections
const int MAX = 350; //Rows: 348 Airports
int main()
{
bool reset;
int menu;
string input;
ifstream read_Data;
airplane_Data airplane[C][MAX];
do
{
reset = true;
read_Data.open("Airports.csv");
if ("Airports.csv")
{
cout << "File \"Airports.csv\" opened successfully. ";
cout << endl;
system("pause"); system("cls");
}
if (!"Airports.csv")
{
cout << "Error Openning \"Airports.csv\"";
cout << endl;
system("pause"); exit(0);
}
cout << "---------------Main Menu---------------" << endl << endl;
cout << "1.Search for airport by city name." << endl;
cout << "2.Exit Program" << endl << endl;
cout << "Please select a choice for the menu above: ";
cin >> menu;
cin.ignore();
//Store data into a Structured Array
for (int a = 0; a < C; a++)
{
for (int i = 0; i < MAX; i++)
{
getline(read_Data, airplane[a][i].state, ',');
getline(read_Data, airplane[a][i].city, ',');
getline(read_Data, airplane[a][i].faa, ',');
getline(read_Data, airplane[a][i].iata, ',');
getline(read_Data, airplane[a][i].icao, ',');
getline(read_Data, airplane[a][i].airport_Name, ',');
getline(read_Data, airplane[a][i].role, ',');
getline(read_Data, airplane[a][i].enplanements, ',');
}
}
switch (menu)
{
case 1:
for (int a = 0; a < C; a++)
{
for (int i = 1; i < MAX; i++)
{
cout << "Enter city name to find information on an airport: ";
getline(cin, airplane[a][i].city);
cout << "Here is information on the cities airport." << endl << endl;
cout << left << setw(15) << "State: " << airplane[a][i].state << endl;
cout << left << setw(15) << "City: " << airplane[a][i].city << endl;
cout << left << setw(15) << "FAA Code: " << airplane[a][i].faa << endl;
cout << left << setw(15) << "IATA Code: " << airplane[a][i].iata << endl;
cout << left << setw(15) << "ICAO Code: " << airplane[a][i].icao << endl;
cout << left << setw(15) << "Airport Name: " << airplane[a][i].airport_Name << endl;
cout << left << setw(15) << "Role: " << airplane[a][i].role << endl;
cout << left << setw(15) << "Enplanements: " << airplane[a][i].enplanements << endl;
}
}
}
} while (reset == true);
read_Data.close();
return 0;
}
答案 0 :(得分:1)
问题是您没有正确阅读Enplanements
字段。它是换行符,而不是逗号,因为它是该行的最后一个字段。结果是您正在阅读换行符,并包括下一行的城市名称。在第二行中,您将在州名中读取城市名称,并且在每一行中您将获得更多不同步的信息。
将该行更改为:
getline(read_Data, airplane[a][i].enplanements, '\n');
有关更多想法,请参见groupby