Need Help Rerunning Program And Display

This is a discussion on Need Help Rerunning Program And Display within the C++ Programming forums, part of the General Programming Boards category; I need help rerunning the program from the very beginning and I can't figure out what I am doing wrong... ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    17

    Need Help Rerunning Program And Display

    I need help rerunning the program from the very beginning and I can't figure out what I am doing wrong...

    Code:
    #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
    I've managed to look back but I need it to loop back to the beginning where it asks to input the number of items again.

    In addition, I want to be able to display the sales items and their prices in the sales receipt as well.

    Really appreciate the help! THANKS!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I need it to loop back to the beginning where it asks to input the number of items again.
    So start your loop earlier?

    In addition, I want to be able to display the sales items and their prices in the sales receipt as well.
    Then you will need to store that data somewhere. (A vector would be the "natural" choice here.)

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    I am not sure I follow with the looping and including the variables

    Code:
    #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
    I get a syntax error doing this but all my brackets seem to be closed.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to close off your do curly-braces before you get to the while, not after.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    You have initilized float count = 0,items = 0; then while (count < items); i think you should enter your data before that sentence,and i really don't know whether it is rignt or not , i'm a newer.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Quote Originally Posted by tabstop View Post
    You have to close off your do curly-braces before you get to the while, not after.
    When I do that, I get an undeclared identifier error...

    Code:
    // TipCalculator.cpp
    // Neel Patel
    // Homework4, Sales Receipt, Because it can calculate and print to the screen a user inputted sales receipt.
    
    #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

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    I have even tried to put the char rerun outside of the do/while loop but then the program just opens and closes instantly.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your rerun variable ceases to exist at the end of the curly brackets in which it was defined, so any attempt to use it outside those brackets is an error. Is there a reason all your variables are local to the loop? Rerun, at any rate, needs to belong to main, not the loop.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Made some progress, the program finally loops back to the very beginning but now when you input a number for amount of items, it only asks for the price of just one item... what am I overlooking here now?

    here's the new code:
    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
    float items = 0;
    float count = 0;
    float price = 0;
    float total = 0;
    float percent;
    float g_t;
    float tax;
    
    
    			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');
    	
    	system ("PAUSE");
    		
    } //End Main Function

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Quote Originally Posted by tabstop View Post
    Your rerun variable ceases to exist at the end of the curly brackets in which it was defined, so any attempt to use it outside those brackets is an error. Is there a reason all your variables are local to the loop? Rerun, at any rate, needs to belong to main, not the loop.
    I am completely new to C++ so I don't quite understand...

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Look at your code where it asks for the price:
    Code:
    				cout<<"Enter the value of the sales item. : $";
    				cin>>price;
    				total = total + price;
    				count ++;
    What do you see there that suggests things happening more than once?

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    the count++?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by xenhancd View Post
    I am completely new to C++ so I don't quite understand...
    You should read up on scope. The easy version: find the curly brace block that "contains" the declaration of a variable. Any use of that variable before or after the block is an error.
    Code:
    {
        {
             int a; //this variable is only good inside these inner curly braces
        }
        //no using a here!
    }

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    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:
    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

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    FIXED IT! Works the way i want it!

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to display something before terminating the program
    By cprogrammer1980 in forum C Programming
    Replies: 1
    Last Post: 03-17-2011, 01:43 PM
  2. My Program Won't Display First Few Data...
    By nino613 in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 01:50 PM
  3. Program Display
    By Axiom in forum C Programming
    Replies: 3
    Last Post: 11-08-2008, 04:48 AM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. why this program display this error:
    By rjhome in forum C Programming
    Replies: 5
    Last Post: 10-05-2001, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21