【问题标题】:Saving each iteration of an accumulator?保存累加器的每次迭代?
【发布时间】:2016-07-11 00:29:22
【问题描述】:

(是的,这是作业,但已经完成,我现在只是在尝试改进它以进行练习)

这基本上是一个销售计算器,允许您输入多个销售项目,然后显示总计、销售税和总计。

我正在尝试进行的修改是,我希望能够保存变量中每个项目的成本,而不会使它们重叠内存,然后能够在总计之上调用它们,所以你可以看到每件物品的价值。

================================================ ==============================

//importing libraries for cin and cout, as well as setw() and setprecision()
#include <iostream>
#include <iomanip>
using namespace std; //sets all code to standard syntax

int main(){ //initializes the main function

    //initializing variables
    char answer = ' ';
    int saleItems = 0;
    double saleTax = 0.0;
    double grandTotal = 0.0;
    double itemValue = 0.0;
    double titemValue = 0.0;
    double taxPerc = 0.0;

    //begins a post-test loop
    do {
        titemValue = 0.0; //makes sure the accumulator resets WITHIN the loop

        //prompts for sale items amount
        cout << "How many sales items do you have? : ";
        cin >> saleItems;

        //creates a loop that displays the prompt for each iteration of saleItems
        for (int x = 1; x <= saleItems; x += 1){
            cout << "Enter in the value of sales item " << x << " : $";
            cin >> itemValue;
            titemValue += itemValue; //accumulator for adding up the iterated values
        }

        //prompts the user to enter a sales percentage
        cout << endl << endl;
        cout << "Enter in the sales tax percentage(Enter 10 for 10%): ";
        cin >> taxPerc;
        cout << endl << endl;

        //processes the variables after taxPerc has been given
        saleTax = titemValue * (taxPerc / 100);
        grandTotal = titemValue + saleTax;

        //sets decimal precision to 2 places
        cout << fixed << setprecision(2);

        //displays receipt with the calculated and input values
        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;

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

        } while (answer == 'Y');

================================================ ==============================

所以,本质上,我需要能够将每个 itemValue 保存到多个不同的值,而不会重复循环,只是替换它们,考虑到累加器只会继续循环,我真的不知道该怎么做,并将 itemValue 值相加。

【问题讨论】:

  • 您想保存输入的每个值以便再次打印出来吗?使用std::vectorstd::list 将项目存储在for 循环中。
  • 如何使用数组来做到这一点?我想使用更基本的方法,因为我是初学者,看起来数组对我来说是下一步。

标签: c++ loops c++11 counter accumulator


【解决方案1】:

这是使用简单数组存储项目值的一种方法。

在顶部声明一个数组。注意:你必须给它一个固定的大小。有一些方法可以改变大小,但它们变得更复杂(例如向量)。最好使用常量而不是硬编码的数字来指定大小,因为稍后您将需要该常量。

const int maxSaleItems = 100;
double itemValues[maxSaleItems];

在您询问用户项目的数量后,请确保他们没有输入太大的数字。

    cout << "How many sales items do you have? : ";
    cin >> saleItems;
    if (saleItems > maxSaleItems) {
        cout << "Sorry, I can only handle " << maxSaleItems << " items.";
        continue;
    }

在您输入项目值的循环内,将项目值保存到数组中:

        cout << "Enter in the value of sales item " << x << " : $";
        cin >> itemValue;
        titemValue += itemValue; //accumulator for adding up the iterated values
        itemValues[x - 1] = itemValue;

注意数组访问中的x-1 - 数组是基于 0 的(即它们的索引从 0 开始)。通常我会将x0 循环到&lt; saleItems,但我不想更改您现有的循环。

打印收据时,添加一个循环打印出所有值(您需要添加格式):

    cout << "**                                        **" << endl;
    for (int x = 1; x <= saleItems; x += 1){
       cout << "** Item " << x << " $" << itemValues[x-1] << "   **" <<endl;
    }
    cout << "**                                        **" << endl;

正如我在 cmets 中所说,使用 std::vector 会更好,但如果您还没有做到这一点,数组也可以。

编辑:简单的vector 示例。 要添加向量,您需要包含适当的标题:

#include <vector>

不再需要maxSaleItems,因为向量可以增长。声明向量变量。 &lt;double&gt; 使它成为一个包含 double 值的向量:

std::vector<double> itemValues;

在循环内部,不用按位置为新项目设置数组值,只需使用push_back 将其添加到向量的末尾即可。

    cout << "Enter in the value of sales item " << x << " : $";
    cin >> itemValue;
    titemValue += itemValue; //accumulator for adding up the iterated values
    itemValues.push_back(itemValue);

打印收据代码可以与数组版本完全相同,因为您可以访问数组等向量:

cout << "**                                        **" << endl;
for (int x = 1; x <= saleItems; x += 1){
   cout << "** Item " << x << " $" << itemValues[x-1] << "   **" <<endl;
}
cout << "**                                        **" << endl;

您可以进行其他更改以使 vector 版本更简单,但我希望尽可能少地进行更改。

【讨论】:

  • 谢谢,我一直在寻找类似的东西,我只是希望创建一个可变大小的数组更容易。
  • 我将添加一个矢量示例 - 它们并没有那么复杂。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多