Hello, I have a code I'm working on that is supposed to keep track of airline tracks. You need to know the airline name, the flight number, and the date and time of a set of flights. Once you have a set of flights, you would like to be able to print them out, and to search for a specific flight by number.

This is my code so far:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int SIZE = 100;

struct flightInfo
{
    int number;
    string airline;
    string date;
    string time;
};

void listFlights(flightInfo flights[], int count);
void newFlight();
void flightNumber();



int main()
{
    char choice = 'a'; 'p'; 's'; 'q';
    flightInfo flights[SIZE];
    flightInfo count;
    cout << "Welcome to the flight tracker." << endl;

    cout << "What would you like to do? (a)dd a flight, (p)rint, (s)earch, or (q)uit: ";
    cin >> choice;
    cout << "What is the airline name of the flight to add? ";
    getline(cin, flights[0].airline);
    index++
    listFlights(flights[], index);

    system("pause");
    return 0;
}

{
void listFlights(flightInfo flights[], int cout) = 100;


    int i = 0;
    for (i = 0, i < count; i++)
    {
        cout << "Airline: " << flights[i].airline;
        cout << "Number: " << flights[i].number;
        cout << "Date: " << flights[i].date;
        cout << "Time: " << flights[i].time;
        }
        return;
}

{
void insertFlight(flightInfo[], newFlight);

}

{
void listFlights(flightInfo, flights[]);
}

{
void searchFlights(flightInfo, flights[], int flightnumber);


}

// In the main function, I can't figure out how to make the choice, I know I'm doing it wrong with the char.
// I'm also supposed to create seerate function for each operation I listed such as
void insertFlight(flights[], flightInfo newFlight);

I am brand new to programming. and would like some help in direction of how to continue or maybe some websites that could help me with structure. The book I have from class really didn't help me, I feel stuck. Help would be appreciated!

Thanks.

char choice = 'a'; 'p'; 's'; 'q';

Let's take a look at this line.
It creates a char, named choice, and then gives it the value a.

All the rest of the line does nothing. It's jsut you saying letters. 'p'; does nothing,'s'; does nothing, 'q'; does nothing.

I suspect what you're trying to do is get the user to enter a letter, and use that to determine what your program should do next. Something like this to get you started:

char choice;
cout << "Enter your choice" << endl;
cin >> choice;

if (choice == 'a')
{
  // call some function here
}
else if (choice == 'p')
{
  // call some function here
}
else if (choice == 's')
{
  // call some function here
}
else if (choice == 'q')
{
  // call some function here
}

Your functions are not formatted correctly, you have things like

{
void foo();

}

when it should be:

void foo()
{

}

Notice how there is no ; after the function (when you are implementing them) and the opening bracket { appears after the function prototype (void foo()).

Your functions should be as so:

void listFlights(flightInfo flights[], int count)
{
    /*...*/
}
void insertFlight(flightInfo[], newFlight)
{
    /*...*/
}
void searchFlights(flightInfo, flights[], int flightnumber)
{
    /*...*/
}

Notice also that you had an error in your definition of the "listFlights" prototype, you had cout instead of count, which would cause a conflict with the standard output std::cout that you use within that function, which might explain why it wasn't working (or causing compilation errors). Also, the = 100 cannot be where it was. You could put that default value on the function declaration (near the start of the code) as so:

void listFlights(flightInfo flights[], int count = 100);
void newFlight();
void flightNumber();

But I would not recommend doing this, because there should not be magic numbers like that.

For your choices, you have char choice = 'a'; 'p'; 's'; 'q'; which is not really meaningful at all, all this does is initialize the choice character to 'a' and then there are 3 meaningless statements (still valid code, but it has no effect at all). The choice character is something that you get from the user (through cin) which means that its value will be determined by whatever the user decides to enter. You first get the value by asking the user to enter it, and then you check what he entered, as follows:

char choice = 'a';

cout << "What would you like to do? (a)dd a flight, (p)rint, (s)earch, or (q)uit: ";
cin >> choice;
cin.ignore();  // <<---- This is important to remove stray characters.

// at this point, 'choice' should contain the user's choice, check it:
switch(choice) {
  case 'a':
    /* code to add a flight record goes here. */
    break; // skip the other cases.
  case 'p':
    /* code to print the flight records goes here, such as calling the 'listFlights' function. */
    break; // skip the other cases.
  case 's':
    /* code to search for a flight record goes here, such as getting user to input the flight number and call the 'searchFlights'. */
    break; // skip the other cases.
  case 'q':
    break; // nothing to do.
  default:
    /* the user entered something else (invalid), do something about it here */
    break;
};

But this is not all, because you probably also want to be able to loop around until the user chooses to quit. A simple way to do this is to wrap the whole thing in a while loop:

char choice = 'a';

while(choice != 'q') {
  cout << "What would you like to do? (a)dd a flight, (p)rint, (s)earch, or (q)uit: ";
  cin >> choice;
  cin.ignore();  // <<---- This is important to remove stray characters.

  switch(choice) {
    /* same as before */
  };
};

Now, all you have to do is figure out how the implement the three different behaviors (add, print, search).

So far, you seem to be doing OK with dealing with the structures (struct). But please ask more questions if there is anything else that is giving you trouble.

I appreciate your time in explaining all of that, it really does help a lot, thank you.

I do have one more question, on the part where it says:

 cout << "What is the airline name of the flight to add? ";
    getline(cin, flights[0].airline);
    index++
    listFlights(flights[], index);

line 3: index ++ I get an error where it says it cannot increment value of type 'char'. Any idea how to fix it?

For a start, index is not defined, also you have no ; to end the line.

Ok that fixed the error, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.