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

How to add a set of integers in C++

本文关键字:一组 整数 添加 C++      更新时间:2023-10-16

我对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 

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

您将成本分配给用于输入工单数量的同一变量(并因此覆盖成本)。 相反,将成本放在单独的变量(如果您愿意,也可以放在常量)中,然后在获得用户输入后进行数学运算。

试试这个:

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 

您正在用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 

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

#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