Member Avatar for FrancisLazo

Hi there guys I can't seem to get the logic on how to put the total price of tickets bought in a movie reservation program. For example, a ticket price is sold for $2 and a customer reserved more than one seat, the price should add all the tickets bought by the customer then display the total amount to be paid... My professor didn't show us how to do it, he just showed us how to do the seats. Here is my code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
     char ans;\
     int seatnumber;
     string s1 = "free", s2 = "free", s3 ="free";
     string reserved;
     double ticketprice(3.5);


     cout << "Reserve Seat no: ";

     do
     {
      system ("cls");
      cout << "|| seat 1 - " << s1 << " || seat 2 - " << s2 << " seat 3 - " << s3 << " || \n";

      cin >> seatnumber;

      cout << endl;

      switch (seatnumber)
      {
       case 1:
           if (seatnumber == 1)
           {
                          if (s1 == "reserved")
                          cout << "Already reserved\n";
           } 
           s1 ="reserved";
           break;

        case 2:
           if (seatnumber == 1)
           {
                          if  (s2 == "reserved")
                          cout << "Already reserved\n";
           } 
           s2 ="reserved";
           break;   

        case 3:
           if (seatnumber == 1)
           {
                          if  (s3 == "reserved")
                          cout << "Already reserved\n";
           } 
           s3 ="reserved";
           break;
        default:
        cout <<"Error!";
        }

   cout << "|| seat 1 - " << s1 << " || seat 2 - " << s2 << " seat 3 - " << s3 << " || \n";
   cout << "Do you wish to reserve another seat?";
   cin >> ans;
}
while (ans!='n'&&ans!='N');
} 

Now all this program can do is to show whether a seat is reserved or not. Please help me...

Keep a running total in a double variable. Add the ticket price into that total within each of the cases of the switch statement, like right after you set the seat to reserved (note that you want to make sure that you're only charging once per ticket).

In other words, add a else if condition to your if statement that says if the seat is unreserved, reserve it, and then add the price into the total.

Give that a try and post back.

Member Avatar for FrancisLazo

Here is my code, take note of the highlighted statements I have added, I still can't get the sum of all the ticket prices, please help me revise my code...

#include <iostream>
#include <string>

using namespace std;

int main()
{
     char ans;\
     int seatnumber;
     string s1 = "free", s2 = "free", s3 ="free";
     string reserved;
    double total(0);
    double ticketprice(3.5);
     
     cout << "Reserve Seat no: ";
     
     do
     {
      system ("cls");
      cout << "|| seat 1 - " << s1 << " || seat 2 - " << s2 << " seat 3 - " << s3 << " || \n";
      
      cin >> seatnumber;
      
      cout << endl;
      
      switch (seatnumber)
      {
       case 1:
           if (seatnumber == 1)
           {
                          if (s1 == "reserved")
                          cout << "Already reserved\n";
                          if (s1 == "reserved")
                          cout << ticketprice + total <<endl;
           } 
           s1 ="reserved";
           break;
           
        case 2:
           if (seatnumber == 1)
           {
                          if  (s2 == "reserved")
                          cout << "Already reserved\n";
                          if (s2 == "reserved")
                          cout << ticketprice + total <<endl;
           } 
           s2 ="reserved";
           break;   
           
        case 3:
           if (seatnumber == 1)
           {
                          if  (s3 == "reserved")
                          cout << "Already reserved\n";
                          if (s3 == "reserved")
                          cout << ticketprice + total <<endl;
           } 
           s3 ="reserved";
           break;
        default:
        cout <<"Error!";
        }
        
        total = ticketprice;
   cout << "|| seat 1 - " << s1 << " || seat 2 - " << s2 << " seat 3 - " << s3 << " || \n";
   cout << "Do you wish to reserve another seat?";
   cin >> ans;
}
while (ans!='n'&&ans!='N');
 

cout << "Here is the total amount\n";
cout << total <<endl;

system ("pause");

return 0; 

}
Member Avatar for FrancisLazo

now what do you think is wrong with my new code???

You are simply outputting your variable total (which hasn't been changed from when you initialized it) plus the ticket price. You need something like total+=ticketprice; . You want to do this if the seat is not reserved, so you can put it under an else branch of your if(s1==reserved) block (obviously, do the same for s2 and s3).

now what do you think is wrong with my new code???

As always, you aren't using CODE Tags.

Also, give a detailed description of what is happening wrong. Each time you post, explain! With CODE TAGS!

Member Avatar for FrancisLazo

You are simply outputting your variable total (which hasn't been changed from when you initialized it) plus the ticket price. You need something like total+=ticketprice; . You want to do this if the seat is not reserved, so you can put it under an else branch of your if(s1==reserved) block (obviously, do the same for s2 and s3).

thank you very much! you have solved my problem! :)

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.