需要帮助创建C++公式以计算总数
Need help creating an equation in C++ to calculate total
我更新的代码。当我运行代码时,它会不断输出所有软件包的价格,而不仅仅是我要求的软件包。
#include <iostream>
using namespace std;
int main() {
// to keep it simple
int choice_a = 995;
int choice_b = 1995;
int choice_c = 3995;
char choice;
int message_units, x=1;
double price;
bool selected = false;
// this loop shows the options initially
do {
cout << "Which package do you choose (enter A, B or C)" << endl;
// you will need to check this
cin >> choice;
// keeping it simple
if (choice == 'A') { price = choice_a; selected = true; }
else if (choice == 'B') { price = choice_b; selected = true; }
else if (choice == 'C') { price = choice_c; selected = true; }
cout << endl;
}
// loops until something was selected
while (selected == false);
do{
cout << "How many message units (enter 1 - 672)" << endl;
// again check this
cin >> message_units;
x++;
}
while(x<2);
if(message_units > 5){
choice_a += 100 * (message_units - 5);
}
cout << "Your total cost is " << choice_a /100 << "." <<choice_a%100 endl
if(message_units > 15){
choice_b += 50 * (message_units - 15);
}
cout <<"Yourtotalcostis"<<choice_b /100 << "." << choice_b%100<<endl;
(你错过了一两个"i",但英语对于非母语人士来说很难。
Atotalcost = 9.95;
if(messageunits>5)
Atotalcost += 1.0 * (messageunits-5);
编辑:
有几种方法可以处理金额。其中之一是将金额存储为美分数,然后小心打印出来。例如,金额 $2.34 存储为 int price = 234
,然后要打印出来,我们打印price/100
(即 2),然后是小数点,然后是 price%100
(即 34,"%"是模运算符,您可以查找)。所以代码将如下所示:
#include <iostream>
using namespace std;
int main()
{
int messageunits;
cout << "how many message units(enter 1 - 672)" << endl;
cin >> messageunits;
int Atotalcost = 995; // cost of package a, in cents
if(messageunits > 5){
Atotalcost += 100 * (messageunits - 5);
}
cout << "Your total cost is " << Atotalcost/100 << "." << Atotalcost%100 << endl;
}
仍有许多工作要做,但这是一个良好的开端。
按照这些思路,这个例子可能会有一些小错误,我试图保持简单。
#include <iostream>
using namespace std;
int main() {
bool finished = false;
do {
// to keep it simple
double choice_a = 9.95;
double choice_b = 19.95;
double choice_c = 39.95;
char choice;
int message_units;
double price;
bool selected = false;
// this loop shows the options initially
do {
cout << "Which package do you choose (enter A, B or C)" << endl;
// you will need to check this
cin >> choice
// keeping it simple
if (choice == 'A') { price = choice_a; selected = true; }
else if (choice == 'B') { price = choice_b; selected = true; }
else if (choice == 'C') { price = choice_c; selected = true; }
cout << endl;
}
// loops until something was selected
while (selected == false);
// user enters how many units is wanted
cout << "How many message units (enter 1 - 999)" << endl;
// again check this (if homework requires checking input)
cin >> message_units;
// Calculating message units
if (message_units > 5) price += message_units * 1;
else price += message_units * 2; // if $2.00 normal?
// Total Price Output
cout << "Total: " << price << endl;
// Is user done?
char done;
cout << "Do you want to enter another? press enter to continue. If you are done, type something and press enter.";
cin >> done;
// check
if (done != '') {
finished = true;
}
}
while (finished = false);
好了,就讲到这里了。两个做循环,其余的。编译时可能会有一些轻微的错误,真的,你应该尝试自己修复这些错误,因为这几乎是整个作业......
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 计算每个节点的树高,帮助我解释这个代码解决方案
- C++ sqrt() 计算帮助
- 谁可以帮助解释我的代码中出了什么问题.计算C ++中的数组
- 初学者需要帮助对数组进行排序和计算
- 需要帮助,需要计算链表中负值的数量,C++
- 有人可以帮助我做一个时循环,以便在每次计算后我都可以继续使用CIN值
- 我需要将Valus与字符串关联的帮助才能计算平均值
- 需要帮助弄清楚此 c++ 代码的计算方式
- 对于循环帮助来计算值
- 需要帮助计算增益.遇到错误.请帮助
- C++ 使用霍纳方法计算多项式的递归帮助
- 需要帮助理解移位计算和修剪一些脂肪从我的代码
- 需要帮助创建C++公式以计算总数
- 帮助成员函数计算距离
- 需要帮助计算 a、b、c、d 和 f 的实例