Rerunning Program

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using namespace 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.

Thanks!
Please indent your code
while (counter < items) The first time, both counter and items have undefined values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
float items = 0;
float count = 0;
float price = 0;
float total = 0;
float percent;
float g_t;
float tax;
char rerun;
        
// Input information
        cout<<"How many sales items do you have? :";
        cin>>items;

                do //The do-while for a program rerun option
                {
                        while (count < items)
                        {
                                cout<<"Enter the value of the sales item. : $";
                                cin>>price;
                                total = total + price;
                                count ++;
                        }
                
        cout << endl << endl;

        cout<<"Enter in the sales tax percentage. :";
        cin>>percent;
                
                tax = total * (percent/100);
                g_t = tax + total;
                
        cout << endl << endl;
                
        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) << total << "            **" << endl;
        cout << "**  Sales Tax:      $" << setw(9) << tax << "            **" << endl;
        cout << "**                  -----------           **" << endl;
        cout << "**  Grand Total:    $" << setw(9) << g_t << "            **" << endl;
        cout << "**                                        **" << endl;
        cout << "**                                        **" << endl;
        cout << "********************************************" << endl;
                
        cout << endl << endl;

                cout<<"Do you want to run this program again? (y/n)";
                cin>>rerun;
                }
                
        while (rerun == 'y' || rerun == 'Y');

        return 0;
} //End Main Function 


Simplified the code and better formatting. sorry!
I'm not sure what I am supposed to do still.
closed account (9wX36Up4)
because my bad english maybe i understood ur question wrong but here is what u need i guess::

return main(); will rerun the program
¿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).
closed account (9wX36Up4)
i din't know that thanks ne555
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

		do //The do-while for a program rerun option
		{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
float items = 0;
float count = 0;
float price = 0;
float total = 0;
float percent;
float g_t;
float tax;
char rerun;

			while (count < items)
			{

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;


				cout<<"Enter the value of the sales item. : $";
				cin>>price;
				total = total + price;
				count ++;
			
		
	cout << endl << endl;

	cout<<"Enter in the sales tax percentage. :";
	cin>>percent;
		
		tax = total * (percent/100);
		g_t = tax + total;
		
	cout << endl << endl;
		
	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) << total << "            **" << endl;
	cout << "**  Sales Tax:      $" << setw(9) << tax << "            **" << endl;
	cout << "**                  -----------           **" << endl;
	cout << "**  Grand Total:    $" << setw(9) << g_t << "            **" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << "********************************************" << endl;
		
	cout << endl << endl;

		cout<<"Do you want to run this program again? (y/n)";
		cin>>rerun;
			}
		}

	while (rerun == 'y' || rerun == 'Y');
		
} //End Main Function 
1
2
3
4
float items = 0;
float count = 0; //¿floats?

while (count < items) //false 


http://www.cplusplus.com/forum/beginner/47630/#msg258362
1
2
3
4
5
6
7
8
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. 
Last edited on
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?

here's the new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char rerun;

		do //The do-while for a program rerun option
		{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items = 0;
int count = 0;
double price = 0;
double total = 0;
double percent;
double g_t;
double tax;

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;

			for (count = 0; count < items; count++)
			{
				cout<<"Enter the value of the sales item. : $";
				cin>>price;
				total = total + price;
				count ++;
			}
		
	cout << endl << endl;

	cout<<"Enter in the sales tax percentage. :";
	cin>>percent;
		
		tax = total * (percent/100);
		g_t = tax + total;
		
	cout << endl << endl;
		
	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) << total << "            **" << endl;
	cout << "**  Sales Tax:      $" << setw(9) << tax << "            **" << endl;
	cout << "**                  -----------           **" << endl;
	cout << "**  Grand Total:    $" << setw(9) << g_t << "            **" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << "********************************************" << endl;
		
	cout << endl << endl;

		cout<<"Do you want to run this program again? (y/n)";
		cin>>rerun;
			
		}

	while (rerun == 'y' || rerun == 'Y');
	
	system ("PAUSE");
		
} //End Main Function 
Last edited on
1
2
3
4
5
for (count = 0; count < items; count++)
{
	//...
	count ++;
}
You are incrementing count twice.
Topic archived. No new replies allowed.