need help !!!!!!!!!!

This is a discussion on need help !!!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; The program should list all flight numbers that have at least one seat available. The screen display should show the ...

  1. #1
    Unregistered
    Guest

    need help !!!!!!!!!!

    The program should list all flight numbers that have at least one seat available. The screen display should show the flight number, departure date and time, departure airport code, arrival airport code and the number of seats available.


    int main()
    {
    int const maxflights = 5;
    struct flight
    {
    char flightnum[5];
    char detail[14];
    char departure[20];
    char arrival[20];
    char avail[12];
    };
    flight record[maxflights];
    int i = 0;
    int count = 0;
    ifstream infile;
    infile.open("flights.dat");
    if(infile.fail())
    cout << "File doesnt exist";
    else
    {
    while (!infile.eof())
    {
    infile >> record[count].flightnum;
    infile >> record[count].detail;
    infile >> record[count].departure;
    infile >> record[count].arrival;
    infile >> record[count].avail;
    infile >> ws;
    count ++;
    }
    infile.close();
    }

    for (i=0;i<=count;i++)
    cout << record[i].flightnum << endl;
    cout << record[i].detail[14] << endl;
    cout << record[i].departure << endl;
    cout << record[i].arrival << endl;
    cout << record[i].avail << endl;

    return 0;
    }

    i can read the file but wen i come to display it , it doesnt display properly..... and also if i just try flightnum commands only....it displays the whole file

    a sample of my .dat file
    QF502
    20/05/01 10:20
    SYDNEY
    ADELAIDE
    10 E AB
    15 E CDE
    22 E ADF
    31 E BCF
    35 E ABCD
    45 E AB
    50 E DEF
    99

    atm it displays sumthin like this
    QF50220/05/01
    10:20 SYDNEY
    ADELAIDE
    10
    E
    15
    E
    CDE
    etc

  2. #2
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    The input operator is treating the whitespaces in your file as a delimiter.

    Use infile.getline(char *, int array, "\n") instead of infile >> to read everything into your variable up to the newline character. That should solve your problem.

    Hint* You might want to read up on random access.
    "The mind, like a parachute, only functions when open."

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    If you need help with homework, do the right thing:

    1) Register with the board so people know who you are
    2) if you paste code into your post, then use [ code][ /code] tags so that your code is readable!

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Yeah, what he said...
    "The mind, like a parachute, only functions when open."

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    13

    Wink puts and gets

    Perhaps if you somethin like this for input:

    infile.get(array[num].star,##);

    and for output:

    infile << strupr(array[num].member) << endl;

    if you want to print your information, I would suggest using puts... At least this is what has worked for me!
    The answer, my friend, is blowing in the wind...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21