需要帮助创建C++公式以计算总数

Need help creating an equation in C++ to calculate total

本文关键字:计算 帮助 创建 C++      更新时间:2023-10-16

我更新的代码。当我运行代码时,它会不断输出所有软件包的价格,而不仅仅是我要求的软件包。

#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);

好了,就讲到这里了。两个做循环,其余的。编译时可能会有一些轻微的错误,真的,你应该尝试自己修复这些错误,因为这几乎是整个作业......