Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

The calculation in my c++ code is wrong. Please help.

+1 vote
asked Apr 25, 2021 by Crystal (130 points)

I'm having a problem with getting the right calculation for the subtotal of each ticket. I'm currently getting this type of result:


Premium Tickets: $ 80.00 x 2 = $ 5242800
Regular Tickets: $ 40.00 x 2 = $ 40
Sales Tax (9.25): $484962.72

What am I doing wrong? C++ code:

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
   cout << setprecision(2) << fixed << showpoint;
   
   const float SALES_TAX = 0.0925;

   float premiumTicket = 80;
   float regularTicket = 40;

   int premTicket;
   int regTicket;
   
   premTotal = premiumTicket * premTicket;
   regTotal = regularTicket * regTicket;

   float subTotal = premTotal + regTotal;
   float tax = subTotal * SALES_TAX;
   float total = subTotal + tax;

   cout << "How many Premium Tickets? ($80): " << setw(4) << endl;
   cin >> premTicket;
   cout << "How many Regular Tickets? ($40): " << setw(4) << endl;
   cin >> regTicket;
   cout << "Premium Tickets: " << setw(4) << " $ " << setw(4) << premiumTicket << " x " << premTicket << " = " << setw(4) << " $ " << setw(4) << premTotal << endl;
   cout << "Regular Tickets: " << setw(4) << " $ " << setw(4) << regularTicket << " x " << regTicket << " = " << setw(4) << " $ " << setw(4) << regTotal << endl;
   cout << "Sales Tax (9.25):    $" << setw(4) << tax << endl;
   
   return 0;
}

1 Answer

+1 vote
answered Apr 27, 2021 by Peter Minarik (86,040 points)

Your problem is that you use the values of premTicket and regTicket for calculations before you'd ask the user to provide these values.

The fix is to ask the user first, then do the calculations with the provided input:

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
   cout << setprecision(2) << fixed << showpoint;
   
   const float SALES_TAX = 0.0925;

   float premiumTicket = 80;
   float regularTicket = 40;

   int premTicket;
   int regTicket;

   cout << "How many Premium Tickets? ($80): " << setw(4) << endl;
   cin >> premTicket;
   cout << "How many Regular Tickets? ($40): " << setw(4) << endl;
   cin >> regTicket;
   
   int premTotal = premiumTicket * premTicket;
   int regTotal = regularTicket * regTicket;

   float subTotal = premTotal + regTotal;
   float tax = subTotal * SALES_TAX;
   float total = subTotal + tax;

   cout << "Premium Tickets: " << setw(4) << " $ " << setw(4) << premiumTicket << " x " << premTicket << " = " << setw(4) << " $ " << setw(4) << premTotal << endl;
   cout << "Regular Tickets: " << setw(4) << " $ " << setw(4) << regularTicket << " x " << regTicket << " = " << setw(4) << " $ " << setw(4) << regTotal << endl;
   cout << "Sales Tax (9.25):    $" << setw(4) << tax << endl;
   
   return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...