简体   繁体   中英

c++ - Clearing an Accumulator in a 'while' Loop

This code needed to do 4 things:

  • Enter the number of sales items
  • Enter the price of each sales item
  • Enter tax percentage
  • Option to run again

I have completed all these requirements, but my only issue comes in when the code repeats. I save the total item value in an accumulator within the 'while' post-test loop. When the program loops, it doesn't clear the accumulator, and continues to just add any new values on top of my old total value.

(Eg If my first run of the code had a total of 20$, and the repeated run has a total of 30$, it will display my total price being 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;
}

You need to reset the value of titemValue inside the loop, but it's set outside of the loop. Change

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;

to

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;

You don't necessarily need to change the first line, but no sense in setting its value to the same thing twice. AFAIK some compilers may optimize it away if you leave in the double initialization anyway, though. Some will not, such as Visual Studio in default Debug configuration settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM