C++ 保存累加器的每次迭代?

C++ 保存累加器的每次迭代?,c++,loops,c++11,counter,accumulator,C++,Loops,C++11,Counter,Accumulator,(是的,这是家庭作业,但已经完成了,我现在只是想改进一下以便练习) 这基本上是一个销售计算器,允许您对销售项目进行多个输入,然后显示总额、销售税和总计 我试图做的修改是,我希望能够节省变量中每个项目数量的成本,而不需要重叠的内存,然后能够调用它们超过总数,这样您就可以看到每个项目的价值 =========================================================================== //importing libraries for cin

(是的,这是家庭作业,但已经完成了,我现在只是想改进一下以便练习)

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

我试图做的修改是,我希望能够节省变量中每个项目数量的成本,而不需要重叠的内存,然后能够调用它们超过总数,这样您就可以看到每个项目的价值

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

//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');
//导入cin和cout以及setw()和setprecision()的库
#包括
#包括
使用名称空间std//将所有代码设置为标准语法
int main(){//初始化main函数
//初始化变量
char-answer='';
int saleItems=0;
双重销售税=0.0;
总双倍=0.0;
双项目值=0.0;
双滴度值=0.0;
双taxPerc=0.0;
//开始测试后循环
做{
titemValue=0.0;//确保累加器在循环内重置
//提示销售项目金额
销售物品;
//创建一个循环,该循环显示saleItems每次迭代的提示

对于(intx=1;x,这里有一种使用简单数组存储项值的方法

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

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;
    }
正如我在评论中所说,使用
std::vector
会更好,但如果您还不能做到这一点,数组也可以

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

#include <vector>
在循环内部,不用按位置设置新项的数组值,只需使用
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这里有一种使用简单数组存储项值的方法

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

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;
    }
正如我在评论中所说,使用
std::vector
会更好,但如果您还不能做到这一点,数组也可以

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

#include <vector>
在循环内部,不用按位置设置新项的数组值,只需使用
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);

是否要保存输入的每个值,以便再次打印?使用
std::vector
std::list
将项目存储在
for
循环中。我如何使用数组?我想使用更基本的方法,因为我是初学者,数组似乎是我的下一步。你知道吗要保存输入的每个值以便再次打印出来吗?使用
std::vector
std::list
将项目存储在
for
循环中。如何使用数组?我想使用更基本的方法,因为我是初学者,数组似乎是我的下一步。谢谢你,我正在寻找我只是希望能够更容易地创建一个可变大小的数组。我将添加一个向量示例-它们没有那么复杂。谢谢,我一直在寻找这些方面的内容,我只是希望能够更容易地创建一个可变大小的数组。我将添加一个向量示例-它们没有那么复杂。