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.

#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

The code above currently gives me an undeclared identifier error.

Thanks in advance!

Recommended Answers

All 8 Replies

Hiya, well I've had a look through the code and there's more than a few errors in it. To begin with, I suspect that this shouldn't even compile as "rerun" is being used outside of the scope in which you declared it. Related to this, you have failed to initialise the variable. (HINT: '\0' is the null character and can be used to initialise char if you don't want to put a value in there)

Also, your middle loop will never execute, but I'll leave that for you to figure out why. It's fairly straight forward, just step through your program.

Finally, as the middle loop will never execute, you will never be asked if you wish to run the program again.

If you fix what I've mentioned above, that should give you a working application as you want it.

I am not all that well versed with C++ and am having a little hard time understanding what you mean. But I have tried to put the char rerun outside of the do/while loop but then the program just opens and closes instantly.

I pointed out that's what would happen in my first post.

You need to step through your program either in your head or with your debugger. It's a simple logic error and easily overlooked. But, on the upside, it's a very easy fix =)

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:

#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

This about how loops work and where you want your item price loop to finish. A computer will do exactly as you tell it, it's not so intuitive as to be able to "read" your code and guess what you want it to do without implicit instruction.

(Ok, this isn't entirely true but that's an entirely different subject ;))

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:

#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

Nevermind! FIXED IT! Works the way i want it!

THANK YOU!!!! Finally have it working the way I need it.

Good job on figuring it out =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.