【问题标题】:c++ - Clearing an Accumulator in a 'while' Loopc++ - 在“while”循环中清除累加器
【发布时间】:2016-06-22 05:38:59
【问题描述】:

这段代码需要做 4 件事:

  • 输入销售商品数量
  • 输入每个销售项目的价格
  • 输入税率
  • 再次运行的选项

我已经完成了所有这些要求,但是当代码重复时,我唯一的问题出现了。我将总项目值保存在“while”后测试循环中的累加器中。当程序循环时,它不会清除累加器,而是继续在我的旧总值之上添加任何新值。

(例如,如果我第一次运行代码总共有 20 美元,而重复运行总共有 30 美元,它将显示我的总价格为 50 美元)

#include <iostream>
#include <iomanip>
using namespace std;

int main(){

char answer = ' ';
int saleItems = 0;
double itemValue = 0.0;
double titemValue = 0.0;
double taxPerc = 0.0;

do {
    cout << "How many sales items do you have? : ";
    cin >> saleItems;

    for (int x = 1; x <= saleItems; x += 1){
        cout << "Enter in the value of sales item " << x << " : $";
        cin >> itemValue;
        titemValue += itemValue;
    } 

    cout << endl << endl;
    cout << "Enter in the sales tax percentage(Enter 10 for 10%): ";
    cin >> taxPerc;
    cout << endl << endl;

    double saleTax = titemValue * (taxPerc / 100);
    double grandTotal = titemValue + saleTax;

    cout << fixed << setprecision(2);

    cout << "********************************************" << endl;
    cout << "********  S A L E S  R E C E I P T  ********" << endl;
    cout << "********************************************" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << "**  Total Sales            $" << setw(9) << titemValue << "     **" << endl;
    cout << "**  Sales Tax              $" << setw(9) << saleTax << "     **" << endl;
    cout << "**                          ----------    **" << endl;
    cout << "**  Grand Total            $" << setw(9) << grandTotal << "     **" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << "********************************************" << endl << endl << endl;

    cout << "Do you want to run this program again? (Y/N):";
    cin >> answer;
    answer = toupper(answer);
    cout << endl << endl;

    } while (answer == 'Y');


return 0;
}

【问题讨论】:

  • 您在程序开始时初始化变量。您运行程序一次,但在其中多次执行循环。因此,如果您希望在每次迭代开始时初始化变量,那么您应该将它们初始化为 0.0。尽管像您一样在声明中保留初始化也是一个好习惯。

标签: c++ for-loop while-loop do-while


【解决方案1】:

您需要在循环内重置 titemValue 的值,但它是在循环外设置的。改变

char answer = ' ';
int saleItems = 0;
double itemValue = 0.0;
double titemValue = 0.0;
double taxPerc = 0.0;

do {
    cout << "How many sales items do you have? : ";
    cin >> saleItems;

char answer = ' ';
int saleItems = 0;
double itemValue = 0.0;
double titemValue; // (changed)
double taxPerc = 0.0;

do {
    titemValue = 0.0; // (new)
    cout << "How many sales items do you have? : ";
    cin >> saleItems;

您不一定需要更改第一行,但将其值设置为相同的东西两次是没有意义的。不过,AFAIK 一些编译器可能会优化它,如果你仍然保留双重初始化。有些不会,比如 Visual Studio 中默认的 Debug 配置设置。

【讨论】:

  • 但将其值设置为相同的值两次没有意义。我认为始终初始化变量是一个好习惯。YMMV。
  • 可能取决于应用程序,即您对运行速度与潜在错误的关注程度。如果你有一个非常大的数组,它被初始化了两次,并且你确定它被初始化了两次,而且初始化需要很长时间,我肯定会看到这种情况下会出现异常。在这种情况下,我删除了它,因为它很明显,但如果不是,或者如果我希望共同开发人员可能会更改可能会破坏它的东西(例如,我通常总是立即在 ctor 中初始化的类成员) ,即使它们稍后再次初始化)。
  • 编译器会出现,看到double titemValue = 0.0;的赋值对程序完全没有影响,把它删掉。另一方面,如果它确实很重要并且你有一个错误,那么捕获 0.0 比碰巧在 RAM 中的一些随机垃圾要容易得多。
猜你喜欢
  • 2015-11-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2019-08-18
  • 2023-03-29
  • 2020-12-24
  • 2015-12-17
  • 2022-11-03
相关资源
最近更新 更多