如何在C++中添加一组整数
How to add a set of integers in 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
正如在另一条评论中指出的那样,没有解释。
您将成本分配给用于输入工单数量的同一变量(并因此覆盖成本)。 相反,将成本放在单独的变量(如果您愿意,也可以放在常量)中,然后在获得用户输入后进行数学运算。
试试这个:
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
相关文章:
- 在 c++ 中拥有一组结构的正确方法是什么?
- 如何从一组具有从左到右优先级的整数值创建有序整数键?
- 编写所需的代码以创建动态一维整数数组
- 我如何将一组整数集变成C 中的3D矢量
- 给定一组正整数 <=k 及其 n 个子集,找到哪些子集对给出原始集合的并集
- 如何将字符串形式的日期 ( "Dec 25, 2012" ) 转换为一组整数 (12/25/12)?
- 最长的子序列,其元素构成一组递增的整数
- 如何查找具有一组整数的函数的'max absolute sum'
- C++将一组整数变量映射到值的有效方法
- 在C++中生成一组整数的排列.获取分段错误
- 给定一组 12 个整数,如何递归生成所有可能的 4 组 3 个整数?C++
- 从另一组整数中有效地删除一组整数
- 按变量数组中的列将一组整数从文本文件读入
- 保存一组整数的最佳方法
- 计算一组关系的整数映射的更有效的算法
- 如何将一维整数数组从 QML 发送到C++
- 我需要标记一组匹配的整数,以便用分数进行数学检查
- 如何在C++中添加一组整数
- 将一组整数读取为C++中的字符串
- 如何在c++中创建一组非标准顺序的整数