I'm supposed to output a program that comes out like this:
http://img638.imageshack.us/img638/4135/screen3t.jpg

And here is the top part of the code. It is supposed to read a .txt file that has the numbers of the: flight; passengers; and miles;

int flight, passengers, miles;
    float income, cost, profit, base, fuel;
    ifstream airline;
    ofstream outfile;
    airline.open("airline.txt");
    outfile.open("outair.txt");

And here are the equations on how we are supposed to calculate the income; cost; and profit;

Here is the equation part of the code:

if (miles>>1000)
          {
          income=1.2*base*passengers;
          cost=1.25*miles*6.50;
          }
          if (miles>=200 || miles<=1000)
          {
            income=1.4*base*passengers;
            cost=1.5*miles*6.50;
          }
          if (miles<<200)
          {
            income=1.6*base*passengers;
            cost=2*miles*6.50;
          }
            profit=income-cost;

And here is the .txt (airline.txt) file that it is reading:
Flight # Passengers Miles Traveled
1001 125 600
1002 105 1200
1004 134 150
1005 240 1500
1010 125 400
1011 99 115
1012 140 750
1013 250 1350

But the problem is that I have no idea how to declare what the passengers, miles, flight # are according to the .txt file. I'm not sure how it takes all the numbers from the .txt file and be able to implement in the equation.

Every time I do it I just get a complete mess of numbers that have no rhyme or reason to why they are being outputted. Here is the entire code that I have:

using namespace std;

#include <iostream>
#include <iomanip>
#include <fstream>

int main()
{
    int flight, passengers, miles;
    float income, cost, profit, base, fuel;
    ifstream airline;
    ofstream outfile;
    airline.open("airline.txt");
    outfile.open("outair.txt");

        airline >> flight
                      >> passengers
                      >> miles;

        cout <<left <<setprecision(2) <<fixed;
        cout <<"Please Enter Base Fare" <<endl;
        cin >> base;
        cout <<"Please Enter Fuel Cost" <<endl;
        cin >> fuel;
        cout <<endl <<endl;

        cout <<right <<setprecision(2)<<fixed;
        cout <<setw(10) <<"Flight"
             <<setw(10) <<"Income"
             <<setw(10) <<"Cost"
             <<setw(10) <<"Profit" <<"\n\n";

      outfile<<right <<setprecision(2)<<fixed;
      outfile<<setw(10) <<"Flight"
             <<setw(10) <<"Income"
             <<setw(10) <<"Cost"
             <<setw(10) <<"Profit" <<"\n\n";


        while ( ! airline.eof())
        {
              airline >> flight
                      >> passengers
                      >> miles;



          if (miles>>1000)
          {
          income=1.2*base*passengers;
          cost=1.25*miles*6.50;
          }
          if (miles>=200 || miles<=1000)
          {
            income=1.4*base*passengers;
            cost=1.5*miles*6.50;
          }
          if (miles<<200)
          {
            income=1.6*base*passengers;
            cost=2*miles*6.50;
          }
            profit=income-cost;

        cout << setw(10) <<flight <<"     "
             << setw(10) <<income <<"     "
             << setw(10) <<cost <<"     "
             << setw(10) <<profit <<endl;
     outfile << setw(10) <<flight <<"     "
             << setw(10) <<income <<"     "
             << setw(10) <<cost <<"     "
             << setw(10) <<profit <<endl;




    airline.close();
    outfile.close();


  }
return 0;
}

I'm really just lost now, and I need help as soon as possible. Hopefully you guys can help me and understand all this mess...because I have no idea anymore. And my code is probably all wrong now, but it worked for the first assignment...but not anymore :@

Thanks!

Recommended Answers

All 4 Replies

Without going through all of your code I can see that your using >> instead of just > for testing greater than and less than so your miles >> 1000 is shifting your miles int 1000 bits to the right which will always make it zero. Try fixing this and see what happens.

It does help somewhat...but it's still not coming out to the right numbers. It's closer, but I still don't understand the process on how it takes numbers from the .txt and register that the belong to a certain variable.

Like how does it know that the numbers under Flight # on the airplane.txt file are for the flight; variable in the program?

And it only shows up with 1 line of horizontal text instead of the 8 like on the .txt file

You're next problem is that you close the files inside of your while loop. Move those to the outside of the loop and it will go through the whole file. The other problem is that you input new flight data before you do your calcs for that flight. Move the input statement thats inside of your loop to the very end of the loop and that should fix your input problems.

As far as how the program knows which values correspond to which variables. You have hard coded that. The system has no clue what values are what in the file it just reads until white space for each number.

Thanks for the help!

It's all working fine now

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.