I need to add the option to rerun the following program. I have the program working however, as soon as I add the do/while, it messes up how the program runs.
usingnamespace std;
int main()
{
// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int counter;
int items;
float sales_item = 0;
float sales_total = 0;
float sales_tax = 0;
float grand_total = 0;
char rerun;
do {
while (counter < items)
{
// Enter number of items and price for each item
cout << "How many sales items do you have? : ";
cin >> items;
for(counter = 1; counter <= items; counter++)
cout << "Enter in the value of sales item "<< counter << " : $";
cin >> sales_item;
// Calculate sales total
sales_total = sales_total + sales_item;
}
cout << endl << endl;
// Enter in salex tax percentage
cout<<"Enter in the sales tax percentage:";
cout << endl;
cout<<"(Enter 10 for 10%): ";
cin>>sales_tax;
cout << endl << endl << endl;
// Calculate grand total
grand_total = sales_total + (sales_total * sales_tax/100);
// Output receipt
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 << setiosflags(ios::fixed) << setprecision(2);
cout << "** Total Sales $" << setw(9) << sales_total << " **" << endl;
cout << "** Sales tax $" << setw(9) << sales_total * sales_tax/100 << " **" << endl;
cout << "** ----------- **" << endl;
cout << "** Grand Total $" << setw(9) << grand_total << " **" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "********************************************" << endl;
cout<<"Do you want to run that again (y/n)";
cin>>rerun;
}
while (rerun == 'y' || rerun == 'Y');
return 0;
} // End Main Function
If I leave the do/while where it is now, it skips the entire beginning of the program. I've been pulling my hair out trying to figure this out. Really could use the help.
¿What is the problem?
I can change the tax in every rerun.
@Recosway: ISO C++ forbids taking address of function '::main'
main is a little special. You can't call it as another you do with any other function.
Besides, suppose that that work. You will be creating all the variables again, and if you do it enough times you can cause an stack overflow (like with infinite recursion).
I want to have the ability to rerun the following from the very beginning, however, I have only been able to get it to rerun from the middle of the program where it asks for sales tax. I've tried moving my do/while tags around to everywhere but nothing seems to work whatsoever. Would really appreciate the help.
while (count < items)
{
cout<<"Enter the value of the sales item. : $";
cin>>price;
total = total + price;
count ++;
}
//here count >= items. You never touch its value again.
Made some progress, the program finally loops back to the very beginning,however another issue arises now. When input the number of items for example 5, the program only asks for the price of 3 items...... what am I overlooking here now?