如何在C ++中添加一组整数

时间:2016-01-18 05:13:48

标签: c++

我是用C ++编程的新手,我试图理解我在这段代码中缺少算法的流程。该代码应该花费一套门票(25美元,30美元,15美元)的费用。当我运行代码时,它将添加购买的门票总数,但不会增加所有门票的总费用。我相信我已经正确分配了变量,但我不知道为什么它不会将总成本加在一起,除非我需要单独分配这些变量。

任何帮助将不胜感激,谢谢!

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 

3 个答案:

答案 0 :(得分:5)

正如另一条评论中指出的那样,没有任何解释。

您将成本分配给您用于输入票数(并因此覆盖成本)的相同变量。相反,将成本放在单独的变量(或者你喜欢的常量)中,然后在获得用户输入后进行数学运算。

试试这个:

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 

答案 1 :(得分:4)

您使用cin语句覆盖价格值:更好地为价格创建单独的变量,并将它们与您的输入相乘。

#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 

答案 2 :(得分:0)

而不是每个票证值的静态声明,您可以在运行时获取它。是的,正如之前的回答者提到的新输入值是覆盖变量。

#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