gpt4 book ai didi

c++ - 根据用户输入搜索结构化数组

转载 作者:行者123 更新时间:2023-11-30 04:55:04 24 4
gpt4 key购买 nike

我是一名初学 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;
}

最佳答案

问题是您没有正确阅读 Enplanements 字段。它以换行符而不是逗号结尾,因为它是该行的最后一个字段。结果是您正在阅读换行符,并包括下一行的城市名称。在第二行,您会将城市名称读入州字段,并且您会在每一行中获得更多不同步。

将该行更改为:

getline(read_Data, airplane[a][i].enplanements, '\n');

有关更多想法,请参阅 How can I read and parse CSV files in C++?

关于c++ - 根据用户输入搜索结构化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com