cin.getline() OR strcmp() causing errors

Hello,
I am in need of some guidance regarding, yes, homework.
I have to make a program that reads airline info from a file, sorts it into an array, and displays it. It also includes a simple menu that lets the user manipulate the array using functions(ie. addFlight(), delFlight(), etc).

Now the trouble I'm having, is with my display_CITY() function. What this function is supposed to do is read a user inputted name, char inputName[NAME_SIZE], filter through the main array, specifically where city names are stored, and display info based on the city name.

I keep getting stuck here so I wrote the function to simply output duplicates of the inputted name.

Here is my code for the funtion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void display_CITY( const flightType alpha[], int count )
{
    char inputName[NAME_SIZE];
    int index = 0;

    cout << "Enter city name: ";
    cin.ignore();
    cin.getline( inputName, NAME_SIZE );

    cout << "You have entered the name" << " '" << inputName << "' " << endl;       // Test to check what getline() read

    cout << endl;


    // Find the appointment in the array
    while( index < count )
    {
        if( !strcmp( inputName, alpha[index].city ) )
        {
            cout << index;
            index++;
        }
        else
        {
            cout << alpha[index].city << endl;
            index++;
        }
    }
}


Now while writing this I ran into a few problems. Firstly, when I enter a name for inputName, even if it is identical to a city name in the array, it displays every city name in the array, not just the name I need.

Enter city name: Dallas
You have entered the name 'Dallas'

San Franciso
Kansas City
Dallas
Las Vegas
San Francisco
San Francisco
Dallas
Austin
Las Vegas
San Francisco
Kansas City
Las Vegas
Las Vegas
Las Vegas
Las Vegas

// End of program


There are two elements in the array with the name "Dallas", however the code reads and displays every element, not just the one I need.

So I thought, okay, maybe I'm reading it wrong and therefore no names are equal. I think it has to do with the cin.ignore(); bit, however, if I were to take that out, my program would no longer pause for any input and would continue on and executes the loop.

Enter city name: You have entered the name ''

San Franciso
Kansas City
Dallas
Las Vegas
San Francisco
San Francisco
Dallas
Austin
Las Vegas
San Francisco
Kansas City
Las Vegas
Las Vegas
Las Vegas
Las Vegas

// End of program


Any advice on either, what I'm doing wrong or hints on how to improve it, or both would be appreciated. Thanks.
I tried to copy/paste your code but strcmp, NAME_SIZE, and flightType are all undefined... obviously they come from other parts of your code, but you didn't paste that part here.

You might find this thread somewhat helpful (though not entirely).. it's a little different technique for accessing a file, but you can still convert it to use cin << fileName; to get the file path.

http://www.cplusplus.com/forum/beginner/86013/
strcmp uses the <cstring> header file

NAME_SIZE = 20;

and flightType is a struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct flightTime
{
    int pubTime;
    int expTime;
};
struct flightType
{
    char carrier[NAME_SIZE];
    int flightNum;
    int arrival;
    char city[NAME_SIZE];
    flightTime Time;
};



The thread you directed me too didn't make much sense, or at least not for what I was looking for.
Topic archived. No new replies allowed.