I'm a beginner C++ student and I am currently trying to wrap my head around arrays. I have an assignment where I need to read data from a csv file, store it into a structured array, then ask the user to input a city name. Based off this city name I need to output the information for this city. My problem arises when I input a city name, I get incorrect information back. I just can't seem to get it right. Any guidance on this and an explanation of where I am going wrong would be greatly appreciated.
Small block of info on csv file:
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
This is what I've written so far:
#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;
}