Need help on code correction

Hey , I need help , everytime I type in a name and surname of the person it just throws a loop. I can only type the name . do I need to change the string?

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int SEATS_PER_ROW = 6;
const int MAX_ROWS = 9;
const int FIRST_CLASS_ROWS = 4;
const int MAX_FLIGHTS = 5;
const int LAST_ROW_SEATS = 2;
const int AISLE_COL = 3;
const int SEATS_PER_FLIGHT = (MAX_ROWS - 1) * SEATS_PER_ROW + LAST_ROW_SEATS;
const float COST_FIRST_CLASS = 1920.00;
const float COST_ECONOMY = 1600.00;

struct SeatMap
{
    struct
    {
        bool col[SEATS_PER_ROW];
    } row[MAX_ROWS];
    int seats_sold;
};

struct Flight
{
    string      depart;
    string      arrive;
};
const Flight flights[MAX_FLIGHTS] =
{   "07:00", "09:30",
    "09:00", "11:30",
    "11:00", "13:30",
    "13:00", "15:30",
    "15:00", "17:30" };
SeatMap seatmap[MAX_FLIGHTS] = {};

void show_flights()
{
    cout << "The available travel times for flights are : " << endl;
    cout << "\tDepart\tArrive" << endl;
    for (int f = 0; f < MAX_FLIGHTS; f++)
        cout << f + 1 << "\t" << flights[f].depart << "\t" << flights[f].arrive << endl;
}

int choose_flight()
{
    int f;

    cout << "Choose the time by entering the option number from the displayed list : ";
    while (1)
    {
        cin >> f;
        if (f < 1 || f > MAX_FLIGHTS)
        {
            cout << "Incorrect Option! Please choose 1 - " << MAX_FLIGHTS << endl;
            continue;
        }
        if (seatmap[f - 1].seats_sold == SEATS_PER_FLIGHT)
        {
            cout << "That flight is full." << endl;
            continue;
        }
        return f - 1;
    };
}

void display_row (SeatMap& seatmap, int r)
{
    int seats_per_row = SEATS_PER_ROW;
    char row = r + 'A';
    char col;

    if (r == MAX_ROWS - 1)
        seats_per_row = LAST_ROW_SEATS;
    for (int c = 0; c < seats_per_row; c++)
    {
        row = r + 'A';
        col = c + '1';
        cout << row << setw(1) << col;

        if (seatmap.row[r].col[c])
            cout << "**|";
        else
            cout << "__|";
        if (c == AISLE_COL - 1)
            cout << "  |";
    }
    cout << endl;
}

void select_seat(SeatMap& seatmap, string & seat)
{
    int seats_per_row;
    char row;
    char col;
    const char last_row = MAX_ROWS + 'A' - 1;
    int r, c;

    while (1)
    {
        cout << "Select seat: ";
        cin >> seat;
        if (seat.size() != 2)
        {
            cout << "Invalid seat number.  Must be two characters" << endl;
            continue;
        }
        row = seat[0];
        row = toupper(row);
        if (row < 'A' || row > last_row)
        {
            cout << "Invalid seat number.  Row must be A - " << last_row << endl;
            continue;
        }
        r = row - 'A';

        seats_per_row = (r == MAX_ROWS - 1) ? LAST_ROW_SEATS : SEATS_PER_ROW;
        col = seat[1];
        if (col < '1' || col > seats_per_row + '0')
        {
            cout << "Invalid seat number.  Col must be 1 - " << seats_per_row + '0' << endl;
            continue;
        }
        c = col - '1';
        if (seatmap.row[r].col[c])
        {
            cout << "That seat is occupied, please pick another" << endl;
            continue;
        }
        seatmap.row[r].col[c] = true;
        seatmap.seats_sold++;
        return;
    }
}

void display_seat_map(const Flight& flight, SeatMap& seatmap)
{
    int r;

    cout << "The available seats for " << flight.depart << " are as follows : " << endl;
    cout << "First Class (" << COST_FIRST_CLASS << ")" << endl;
    for (r = 0; r < FIRST_CLASS_ROWS; r++)
        display_row(seatmap, r);
    cout << "Economy class (" << COST_ECONOMY << ")" << endl;
    for (; r < MAX_ROWS; r++)
        display_row(seatmap, r);
}

void print_ticket(const string& name, int f, const string & seat)
{
    char row = seat[0];
    bool first_class = row - 'A' < FIRST_CLASS_ROWS;
    double amount;

    cout <<
        "*******************************************************************************\n"
        "              TRAVEL             TICKET               FOR              FLIGHT\n"
        "*******************************************************************************\n"
        "Name              :   " << name << endl;
    cout << "                                           Travel Ticket class : ";
    if (first_class)
        cout << "First Class" << endl;
    else
        cout << "Economy" << endl;
    cout << "                                           Seat No             : ";
    cout << seat << endl;
    cout << "Departure         :   Johannesburg         Departure Time      : " << flights[f].depart << endl;
    cout << "Destination       :   Cape Town            Arrival Time        : " << flights[f].arrive << endl;
    cout << "*******************************************************************************\n";
    cout << "Amount:  ";
    amount = (first_class) ? COST_FIRST_CLASS : COST_ECONOMY;
    cout << amount << endl;
    cout << "Thank you for booking with COS1511. Your travel agent for queries is Annie Mathew\n"
            "*******************************************************************************" << endl;
}

void sell_ticket()
{
    string  name;
    int     f;
    string seat;

    cout << "Enter full name : ";
    cin >> name ;
    show_flights();
    f = choose_flight();
    display_seat_map(flights[f], seatmap[f]);
    select_seat(seatmap[f], seat);
    print_ticket(name,f,seat);
}

int main()
{
    char another;

    cout << "Welcome to COS1511 Flight Booking System" << endl;
    do
    {
        sell_ticket();
        cout << "Sell another ticket (y/n)? ";
        cin >> another;
        another = toupper(another);
    } while (another == 'Y');
    for (int f = 0; f < MAX_FLIGHTS; f++)
        cout << "Number of bookings made for flight " << f+1 << ": " << seatmap[f].seats_sold << endl;
    return 0;
}
Last edited on
> cin >> name ;
The stream extraction operators use white space as a delimiter.

To read a whole line, use getline()
https://en.cppreference.com/w/cpp/string/basic_string/getline

But beware of mixing getline() with >>, as this recent post found out.
https://www.cplusplus.com/forum/general/282600/
changed it getline(cin,name) ;

but now it only it doesn't ask me to input another name on the loop , it continues without me typing in a name for the rest. Also , if I use lowercase then number for the seat it gives 1600 for all. but if I use a capital for the same seat it gives 1920 (correct). anyway to fix this?

Last edited on
getline() and >> don't get along together. >> leaves the terminating '\n' and getline() doesn't ignore it but takes it as empty input. Before the getline(), use cin.ignore() to remove the left '\n'.

ok that fixed it , now how do let both 'A' and 'a' - 'D' and 'd' be First class (1920) and 'E' 'e' - 'I' 'i' be Economy class? cause it works only if I capitalise it but if I don't it doesn't
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum class_t { FIRST, ECONOMY, INVALID };

class_t determine_class( const std::string& seat )
{
    static const std::string first_class_rows = "ABCDabcd" ;
    static const std::string economy_class_rows = "EFGHIefghi" ;

    if( seat.size() == 2 )
    {
        if( first_class_rows.find( seat.front() ) != std::string::npos ) return FIRST ;
        else if( economy_class_rows.find( seat.front() ) != std::string::npos ) return ECONOMY ;
    }

    return INVALID ;
}
Topic archived. No new replies allowed.