C++根据年龄计算票价,然后求和
C++ Calculating ticket prices based on age, then summing them
我必须制作一个非常简单的程序,询问用户他/她想买多少票。然后它会询问每张票的年龄。然后,它应该能够使用以下价格计算门票的总成本:
- 如果车主年龄超过15岁,那么这张票的价格是80英镑
- 否则,如果所有者至少8岁,那么他将支付30英镑
- 8岁以下的儿童可以免费获得一张票
我的问题是如何计算门票的总价?
这就是我所走的路:
我使用while循环让用户输入多个年龄以及一个if语句,用于将价格分配给不同的年龄。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int age, tickets, persons, price, total_price;
persons = 1, total_price = 0;
cout << "How many tickets do you want? ";
cin >> tickets;
cout << "Number of tickets: " << tickets << endl;
while (tickets >= persons) {
cout << "Enter age for person " << persons << ": ";
cin >> age;
{
if (age > 15)
price = 80;
else if (age < 8)
price = 0;
else
price = 30;
}
price + total_price;
persons++;
}
cout << "Total price is: " << total_price;
return 0;
}
使用当前语句price + total_price;
,您将一事无成。将其更改为total_price += price;
,您将开始在while
-循环的每次迭代中将price
添加到total_price
。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int age, tickets, persons, price, total_price;
persons = 1, total_price = 0;
cout << "How many tickets do you want? ";
cin >> tickets;
cout << "Number of tickets: " << tickets << endl;
while (tickets >= persons) {
cout << "Enter age for person " << persons << ": ";
cin >> age;
{
if (age > 15)
price = 80;
else if (age < 8)
price = 0;
else
price = 30;
}
total_price += price;
persons++;
}
cout << "Total price is: " << total_price;
return 0;
}
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 在std::thread中,joinable()然后join()线程安全吗
- C++:如何读取分离变量,然后读取向量
- 为什么我的递归函数按降序打印,然后按升序打印?
- 等待整个 omp 块完成,然后再调用第二个函数
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 求和 int 乘以双倍时的欠/过近似
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 给定一个类型,如何派生一个泛型更广泛的类型(例如,用于溢出安全求和)?
- 如何使变量从 x 到 y,然后从 y 返回到 x 并始终重复该过程
- 递归求和任务的错误答案
- 向量值(字符串和整数)求和C++
- 找到与另一个二进制模式匹配的所有 2 位值,然后对它们求和
- C++根据年龄计算票价,然后求和
- C++将字符串数组转换为整数,然后求和int数组