Outputting message log in a loop with multiple functions?

My assignment involves outputting 10 flights (numbered 0-9 through an array) and their corresponding seats booked. Originally they all had 0 seats booked, then I had to use a ready-made file to input data and end up with the final booked seats for the day. Some seats were also cancelled in the process.

Simply put, so far my output has two columns: One column simply lists the flight #'s (0-9). The other column gives the number of seats booked on that flight.

My problem is making the third column. This is the message log column where I am supposed to print out a message for the action I took for each flight.

example: "Added 3 seats to flight 8" or "cancelled 30 seats from flight 3" or "Transaction failed: seats cancelled exceed seats booked" etc...

How do I do this for all 10 flights? Every time I try my formatting gets messed up. Im confused. Any help would be appreciated. My code is below:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#define N 10

using namespace std;

//Prototype functions
int printBookings(int[], int);
double Income(int[], int, double);
int empty(int[], int);

int main()
{
//Declaring and opening the file
/************************************************/
    ifstream flightInfo;

    flightInfo.open("flightInfo.txt");

    if(!flightInfo.is_open())
        cout << "File did NOT open correctly";
/***********************************************/

cout.setf(ios::fixed, ios::floatfield); // used to set width/spacing

int bookings[N];
int trans, fn, seats, emptyFlight;
double totalIncome;

for(int i=0; i<N; i++)
    bookings[i]=0; // Initializes all elements to 0

flightInfo >> trans >> fn >> seats; // Reads all three lines of file
while(flightInfo){
    if(trans==1){
        bookings[fn]+=seats;
        cout << "Seats added";}
    if(trans==2 && bookings[fn]>seats)
        bookings[fn]-=seats; // only executes if seats currently booked are greater than being canceled
    else if(trans==2 && bookings[fn]<seats)
            bookings[fn]=bookings[fn]; // if seats canceled exceed currently booked, bookings stay the same.

    flightInfo >> trans >> fn >> seats;
}

printBookings(bookings, N);

emptyFlight = empty(bookings, N);
totalIncome = Income(bookings, N, 120.00);

cout << "\n\nThe number of flights with zero bookings are: " << emptyFlight << endl;
cout << "The total income generated by ticket sales today was: " << setprecision(2) << totalIncome << "\n\n\n\n";
    return 0;
}

int printBookings(int a[], int elements){

cout << "Flight #" << "\tBookings" << "\tMessage Log\n";

for(int i=0; i<elements; i++)
        cout << setw(5) << i << setw(15) << a[i] << endl; // Prints chart showing flight # alongside seats booked
}


double Income(int a[], int elements, double seatPrice){

int seatsUsed=0;
double income;

for(int i=0; i<elements; i++){
        if(a[i]>0)
        seatsUsed+=a[i]; // calculates sum of all booked seats
}

income = seatsUsed*seatPrice;

return income;
}

int empty(int a[], int elements){

int cnt0 = 0;

for(int i=0; i<elements; i++)
if(a[i]==0) // If zero seats are booked for a flight
    cnt0++;

return cnt0;
}


I also have two additional functions that calculate the income generated and the number of empty flights. Those are fine, you can ignore them.

I think the problem lies in the cout in the printBookings function. I may be completely wrong though.

Thank you in advance for any help.
Last edited on
Anyone?
For the format: When your header contains tabs ('\t') you need to included these tabs in each as well.

Where are the log messages?
I tried putting the tabs, but the format still comes out messed up. Take a look:

1
2
3
4
5
6
7
8
9
10
11
12
13
flightInfo >> trans >> fn >> seats; // Reads all three lines of file
while(flightInfo){
    if(trans==1){
        bookings[fn]+=seats;
        cout << "\t\tSeats added";}
    if(trans==2 && bookings[fn]>seats){
        bookings[fn]-=seats; // only executes if seats currently booked are greater than being canceled
        cout << "\t\tseats cancelled";}
    else if(trans==2 && bookings[fn]<seats){
            bookings[fn]=bookings[fn]; // if seats canceled exceed currently booked, bookings stay the same.
        cout << "\t\tTransaction Rejected";}
    flightInfo >> trans >> fn >> seats;
}


Im trying to add three messages: "Seats added" and "seats cancelled" and "transaction rejected"

I simply want these messages to be displayed for the corresponding transaction in the third column under the header "Message Log", but the format comes out entirely wrong.

As I stated before, I think the problem is the cout I have in the printBookings function. That cout somehow fails to line up with the cout-ed log messages.

1
2
3
4
5
6
7
int printBookings(int a[], int elements){

cout << "Flight #" << "\tBookings" << "\tMessage Log\n";

for(int i=0; i<elements; i++)
        cout << setw(5) << i << setw(15) << a[i] << endl; // Prints chart showing flight # alongside seats booked
}



How do I organize the formatting of two different couts in two different functions?
Last edited on
In other words, the program is doing the couts in order. First the couts in main occur, then the couts in printBookings. Problem is, that messes up the entire format.

After printing out "i" and "a[i]" I want the appropriate message (for that particular transaction) to print. How do I do this?
Last edited on
I'd guess the problem is that some headers are larger than the tab size. I suggest that you use the same setw(...) for the headers as well as the rows.

See:
http://www.cplusplus.com/reference/iomanip/setfill/
http://www.cplusplus.com/reference/iomanip/setiosflags/

for more options to manipulate the fields
Topic archived. No new replies allowed.