Question

Can I get help with adding the following to this code please? 1. When buying multiple...

Can I get help with adding the following to this code please?

1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead)

2. Print the total cost after all of the tickets are selected/bought

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

int const ROWS = 9; // number of rows
int const COLS = 10; // number of columns

using namespace std;

void printSeats(int seats[ROWS][COLS])
{

cout << left;
cout << setw(8) << " ";

for (int i = 1; i <= 10; i++)
{
cout << setw(4) << i;

}

cout << endl; // line break

cout << setw(8) << " ";

for (int i = 1; i <= 38; i++)
{

cout << "-";

}
cout << endl;

for (int i = 0; i < ROWS; i++)
{

cout << setw(4) << "Row" << setw(1) << (i + 1) << setw(3) << ": ";

for (int j = 0; j < COLS; j++)
{

cout << setw(4) << seats[i][j];

}

cout << endl;

}

}

void findSeatByPrice(int seats[ROWS][COLS],int price)
{

for (int i = 0; i < ROWS; i++)
{

for (int j = 0; j < COLS; j++)
{

if (seats[i][j] == price)
{

seats[i][j] = 0;

cout << "Found! Purchased seat -> Row: " << (i + 1) << ", Col: " << (j + 1) << " Enjoy the show!" << endl;

return;

}

}

}

cout << "Sorry, no seats found/left with price $" << price << endl;

}

void findSeatByNumber(int seats[ROWS][COLS],int row, int col)
{

row--;

if (row < 0 || row >= ROWS)
{

cout << "Invalid row!" << endl;
return;

}


col--;

if (col < 0 || col >= COLS)
{

cout << "Invalid column!" << endl;
return;

}

if (seats[row][col] == 0)
{

cout << "Sorry! That seat is already taken." << endl;
cout << "\nPlease select another seat" << endl;

return;

}

cout << "Seat booked! The price for this seat is $" << seats[row][col] << ". Enjoy the show!" << endl; // confirmation message

// marking seat as reserved

seats[row][col] = 0;

}

int main()
{

// seating chart

int seats[ROWS][COLS] = { {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},

{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},

{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},

{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},

{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},

{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},

{20, 20, 30, 30, 40, 40, 30, 30, 20, 20},

{20, 30, 30, 40, 50, 50, 40, 30, 30, 20},

{30, 40, 50, 50, 50, 50, 50, 50, 40, 30} };

int choice = 0;

int price, row, col;

// loop

while (choice != 3)
{

cout << right << setw(45) << "Please choose a seat or a price:" << endl << endl;

printSeats(seats);

cout << endl << "Press (1) to choose a seat by its number: " << endl;
cout << "Press (2) to choose a seat by its price: " << endl;
cout << "Press (3) to quit: " << endl;

cin >> choice;

switch (choice)
{

case 1: {

// to select multiple seats

cout << "Please enter the number of seats you would like to purchase: ";
int n,i=0;
int row[20],col[20];
    cin >> n;
               cout << "Please enter the row and col numbers: ";
           while(i<n){
      
            cin >> row[i];
            cin >> col[i++];
            }
          
int ii = 0;

while (ii<n)
{

cout << "\nSeat " << ii+1 << endl;
findSeatByNumber(seats,row[ii],col[ii]);

ii++;

}

break;

}

case 2: {

// to select multiple seats

cout << "Please enter the number of seats you would like to purchase: ";
int n,i=0;
int price[20];
    cin >> n;
               cout << "Please enter the prices: ";
           while(i<n){
      
            cin >> price[i++];
            }
          
int ii = 0;
while (ii<n)
{

cout << "\nSeat " << ii+1 << endl;
findSeatByPrice(seats,price[ii]);

ii++;

}

break;

}

}

}

system("pause");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iostream>
#include <iomanip>
int const ROWS = 9; // number of rows
int const COLS = 10; // number of columns
using namespace std;
void printSeats(int seats[ROWS][COLS]){
   cout << left;
   cout << setw(8) << " ";
   for (int i = 1; i <= 10; i++){
       cout << setw(4) << i;
   }
   cout << endl; // line break
   cout << setw(8) << " ";
   for (int i = 1; i <= 38; i++){
       cout << "-";
   }
   cout << endl;
   for (int i = 0; i < ROWS; i++)   {
       cout << setw(4) << "Row" << setw(1) << (i + 1) << setw(3) << ": ";
       for (int j = 0; j < COLS; j++){
           cout << setw(4) << seats[i][j];
       }
   cout << endl;
   }
}

int printprice(int seats[ROWS][COLS], int total){
   total = total+seats[ROWS][COLS];
   return total;
}


int findSeatByPrice(int seats[ROWS][COLS],int price,int total){
   for (int i = 0; i < ROWS; i++)   {
       for (int j = 0; j < COLS; j++)   {
           if (seats[i][j] == price) {
               total = printprice(seats[row][col], total);
               seats[i][j] = 0;
               cout << "Found! Purchased seat -> Row: " << (i + 1) << ", Col: " << (j + 1) << " Enjoy the show!" << endl;
               return total;
           }
       }
   }
   cout << "Sorry, no seats found/left with price $" << price << endl;
   return total;
}

int findSeatByNumber(int seats[ROWS][COLS],int row, int col, int total ){
   row--;
   if (row < 0 || row >= ROWS){
       cout << "Invalid row!" << endl;
       return total;
   }
   col--;
   if (col < 0 || col >= COLS){
       cout << "Invalid column!" << endl;
       return total;
   }
   if (seats[row][col] == 0){
       cout << "Sorry! That seat is already taken." << endl;
       cout << "\nPlease select another seat" << endl;
       cout << "Please enter the row and col numbers: ";
cin >> row;
cin >> col;
   total = findSeatByNumber(seats[ROWS][COLS], row, col, total );
       return total;
   }
   cout << "Seat booked! The price for this seat is $" << seats[row][col] << ". Enjoy the show!" << endl; // confirmation message
   total = printprice(seats[row][col], total);
   // marking seat as reserved
   seats[row][col] = 0;
   return total;
}
int main() {
// seating chart
   int seats[ROWS][COLS] = {
       {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
       {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
       {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
       {10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
       {10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
       {10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
       {20, 20, 30, 30, 40, 40, 30, 30, 20, 20},
       {20, 30, 30, 40, 50, 50, 40, 30, 30, 20},
       {30, 40, 50, 50, 50, 50, 50, 50, 40, 30} };

   int choice = 0;
   int total = 0;
   int price, row, col;
   // loop
   while (choice != 3)
   {
   cout << right << setw(45) << "Please choose a seat or a price:" << endl << endl;
   printSeats(seats);
   cout << endl << "Press (1) to choose a seat by its number: " << endl;
   cout << "Press (2) to choose a seat by its price: " << endl;
   cout << "Press (3) to quit: " << endl;
   cin >> choice;
   switch (choice)   {
       case 1: {
       // to select multiple seats
       cout << "Please enter the number of seats you would like to purchase: ";
       int tickets,i=0;
       int row[20],col[20];
       cin >> tickets;
       cout << "Please enter the row and col numbers: ";
       while(i<tickets){
       cin >> row[i];
       cin >> col[i++];
       }
       int ii = 0;
       while (ii<tickets){
       cout << "\nSeat " << ii << endl;
       total=findSeatByNumber(seats,row[ii],col[ii],total);
       ii++;
       }
       break;
       }
       case 2: {
       // to select multiple seats
           cout << "Please enter the number of seats you would like to purchase: ";
           int n,i=0;
           int price[20];
       cin >> n;
   cout << "Please enter the prices: ";
   while(i<n){
       cin >> price[i++];
       }
           int ii = 0;
           while (ii<n){
               cout << "\nSeat " << ii+1 << endl;
               total= findSeatByPrice(seats,price[ii],total);
               ii++;
               }
           break;
           }
       }
   }
   if (choice == 3)
   {
       cout << "Total price : " << total << endl;
   }
       system("pause");
   return 0;
}

Here, i provide the edited code for the above question.

Add a comment
Know the answer?
Add Answer to:
Can I get help with adding the following to this code please? 1. When buying multiple...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program is in C++ which reserves flight seats. It uses a file imported to get...

    This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...

  • I need help fixing this code and adding a loop please. Here is the original problem: #include <iostream> using namespace std; //function to print seating chart void printSeatingChart(int **cha...

    I need help fixing this code and adding a loop please. Here is the original problem: #include <iostream> using namespace std; //function to print seating chart void printSeatingChart(int **chart) {    int i, j;    cout << "\nROW\t";    for (i = 1; i <= 10; i++)    {        cout << i << "\t";    }    cout << "\n-----------------------------------------------------------------------------------\n";    for (i = 8; i >= 0; i--)    {        cout << "\n" << i + 1 << "\t";        for (j = 0; j < 10; j++)       ...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • I wrote program that have to block all moves for player 1, for game TicTacTOE AI,...

    I wrote program that have to block all moves for player 1, for game TicTacTOE AI, but it is not working. Can someone check what I have not eliminated? #include using namespace std; const int ROWS = 3; const int COLS = 3; int counter = 0; void aITurn(char b[][COLS]); void fillBoard(char board[ROWS][COLS]); void getChoice(char b[][COLS], bool playerToggle); bool gameOver(char b[][COLS]); void showBoard(char board[ROWS][COLS]); int main() {    ///main is complete, nothing to do here    char board[ROWS][COLS];    bool playerToggle =...

  • Can you help us!! Thank you! C++ Write a program that can be used by a...

    Can you help us!! Thank you! C++ Write a program that can be used by a small theater to sell tickets for performances. The program should display a screen that shows which seats are available and which are taken. Here is a list of tasks this program must perform • The theater's auditorium has 15 rows, with 30 seats in each row. A two dimensional array can be used to represent the seats. The seats array can be initialized with...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT