1

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;
}
  • "`if ("Airports.csv")`" and "`if (!"Airports.csv")`" don't do what you think they do. You want to use `if(read_Data.is_open())`. Also there is no need for reopening the file on every iteration of your `do ... while`-loop. – Swordfish Nov 13 '18 at 16:19
  • Please don't post pictures of text, post text. – Swordfish Nov 13 '18 at 16:20
  • Don't use screenshots. Provide a sample input of your csv, and then from a quick look, you will have a minimal example, ready for people that have time to help! – gsamaras Nov 13 '18 at 16:21
  • I'm not sure you're reading in the csv data correctly - you're calling getline for each column field when in fact getline will read the entire row. Have you stepped through this code in the debugger ? – auburg Nov 13 '18 at 16:28
  • @auburg It specifies `,` as the line delimiter, so it only reads one column. – Barmar Nov 13 '18 at 16:29
  • @Barmar so it does - my mistake – auburg Nov 13 '18 at 16:30

1 Answers1

1

The problem is that you're not reading the Enplanements field correctly. It ends with a newline, not a comma, since it's the last field on the line. The result is that you're reading past the newline, and including the city name from the next line. On the second line you'll read the city name into the state field, and you'll get more out of sync on each line.

Change that line to:

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

For more ideas, see How can I read and parse CSV files in C++?

Barmar
  • 741,623
  • 53
  • 500
  • 612