starrynights89
starrynights89

Reputation: 139

How to add a set of integers in C++

I'm fairly new to programming with C++ and I'm trying to understand where I'm missing the flow of the algorithm in this code. The code is supposed to take the cost of a set of tickets ($25, $30, $15). When I run the code it will add the total number of tickets bought, but not the total cost of all the tickets. I'm believe I've assigned the variables correctly but I don't know why it isn't adding together the total cost unless I need to assign those variables separately.

Any help would be appreciated, thanks!

using namespace std; 

int main()

{
//declare variables
double orchestra = 25;
double mainFloor = 30;
double balcony = 15;
double totalSales = 0.0;

//enter input items
cout << "Enter orchestra tickets ";
cin >> orchestra;
cout << "Enter main floor tickets ";
cin >> mainFloor;
cout << "Enter balcony tickets ";
cin >> balcony;

//add the input items and print the total
totalSales = orchestra + mainFloor + balcony;


//display the total sales
cout << "Total Sales $" << totalSales << endl;

system("pause");
return 0;
}   //end of main function 

Upvotes: 1

Views: 170

Answers (3)

Kulamani
Kulamani

Reputation: 517

Instead of static declarations per ticket value you can take it at runtime. Yes, as previous answerer mentioned new input value is overwriting to variable.

#include <iostream>
#define MAX 3
using namespace std;
typedef struct 
{
double ticket_type_per_cost;
double total_no_of_tickets;
}ticket; 
int main()
{
double totalSales=0;
ticket t;
int i;
for(i=0; i<MAX; i++)
    {
    cout<< "\nenter cost per ticket of type "<<i+1 <<": ";
    cin>>t.ticket_type_per_cost;

    cout<<"\nenter number of tickets: ";
    cin>>t.total_no_of_tickets;

    totalSales= totalSales + (t.ticket_type_per_cost * t.total_no_of_tickets);
    }

//display the total sales
cout << "Total Sales $" << totalSales << endl;

system("pause");
return 0;
}   //end of main function 

Upvotes: 0

George Houpis
George Houpis

Reputation: 1729

As pointed it out in another comment, there was no explanation.

You are assigning the cost to the same variable that you are using for input on the number of tickets (and consequently overwriting the cost). Instead, put the costs in separate variables (or constants if you prefer) and then do the math after getting the user input.

Try this:

using namespace std; 

int main()
{
    //declare variables
    double cost_per_orchestra = 25;
    double cost_per_mainFloor = 30;
    double cost_per_balcony = 15;
    double orchestra = 0;
    double mainFloor = 0;
    double balcony = 0;
    double totalSales = 0.0;

    //enter input items
    cout << "Enter orchestra tickets ";
    cin >> orchestra;
    cout << "Enter main floor tickets ";
    cin >> mainFloor;
    cout << "Enter balcony tickets ";
    cin >> balcony;

    //add the input items and print the total
    totalSales = cost_per_orchestra * orchestra + cost_per_mainFloor * mainFloor + cost_per_balcony * balcony;


    //display the total sales
    cout << "Total Sales $" << totalSales << endl;

    system("pause");
    return 0;
}   //end of main function 

Upvotes: 5

Constantin
Constantin

Reputation: 8961

You're overwriting your price values with your cin statements: Better create separate variables for the price and multiply them with your input.

#include <iostream>

using namespace std;

int main(){
  //declare variables
  const double orchestraPrice = 25;
  const double mainFloorPrice = 30;
  const double balconyPrice = 15;
  double orchestra = 0;
  double mainFloor = 0;
  double balcony = 0;
  double totalSales = 0.0;
  //enter input items
  cout << "Enter orchestra tickets ";
  cin >> orchestra;
  cout << "Enter main floor tickets ";
  cin >> mainFloor;
  cout << "Enter balcony tickets ";
  cin >> balcony;
  //add the input items and print the total
  totalSales = orchestra * orchestraPrice + mainFloor * mainFloorPrice + balcony * balconyPrice;
  //display the total sales
  cout << "Total Sales $" << totalSales << endl;
  system("pause");
  return 0;
}   //end of main function 

Upvotes: 4

Related Questions