an option at the end of this prog to ask if I want to run again

This is a discussion on an option at the end of this prog to ask if I want to run again within the C++ Programming forums, part of the General Programming Boards category; I am wanting to put an option at the end of this prog to ask if I want to run ...

  1. #1
    Registered User bandito9111's Avatar
    Join Date
    Feb 2003
    Posts
    4

    an option at the end of this prog to ask if I want to run again

    I am wanting to put an option at the end of this prog to ask if I want to run again or exit how would i go about doing this
    i have already tried this below
    Thanks for your help
    This is what i tried
    ((((
    std::cout<<"\nall done?";

    bool yn;

    std::cin>>yn;
    if (yn==n)
    goto z;

    else if

    return 0;
    }




    Start of real prog!!!!!!!


    #include<iostream>
    int main()

    {
    z:
    int area, volume, height, width, length, sphere, circ, vols, pi, y, n ;

    pi = 3.14159265;

    std::cout<<"\nDo you wish to calculate Area or Volume or vol of Sphere?(A/V/S)";

    char avs;

    std::cin>>avs;

    if (avs == 'a')
    {
    //input and calculate area
    std::cout<<"\ninput width in inches:";
    std::cin>>width;
    std::cout<<"\ninput length in inches:";
    std::cin>>length;
    area=length*width;
    std::cout<<"\narea is:";
    std::cout<<area;
    std::cout<<" Square Inches";
    }


    else if (avs == 'v')
    {
    //input and calculate volume
    std::cout<<"\ninput width in inches:";
    std::cin>>width;
    std::cout<<"\ninput height in inches:";
    std::cin>>height;
    std::cout<<"\ninput length in inches:";
    std::cin>>length;
    volume=height*width*length;
    std::cout<<"\nvolume is:";
    std::cout<<volume;
    std::cout<< " Cubic Inches";
    }

    else if (avs == 's')

    { //input and calculate circumfrence
    std::cout<<"\n input diameter/2 or radius";
    std::cin>>sphere;
    vols = 4*pi*(sphere*sphere);
    circ = (2*sphere)*pi;
    std::cout<<"\narea is:";
    std::cout<<vols;
    std::cout<< "Cubic inches";
    std::cout<<"\nby the way the circumfrence is";
    std::cout<<circ;
    std::cout<< " Inches";
    }

    return 0;

    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    first of here is how the part that asks if he wants to restart should be written:

    Code:
                  std::cout<<"\nall done?";
    
                  char yn;
    
                  std::cin>>yn;
                  if (yn=='n' || yn == 'N')
                  goto z;
    
                  else
    
                  return 0;
    second the use of goto is not advisable, it generally thought of to be a bad thing in C++. A better way to write you program would be something like this:

    Code:
    #include<iostream>
    int main()
    
    {
    	int area, volume, height, width, length, sphere, circ, vols, pi, y, n ;
    	
    	bool finished = true; // ADDED THIS: will hold true if finished else it will hold false
    	char temp = '\0'; 	  // Used for user input, set to empty.
    
    	pi = 3.14159265;
    	
    	do
    	{
    		std::cout<<"\nDo you wish to calculate Area or Volume or vol of Sphere?(A/V/S)";
    
    		char avs;
    
    		std::cin>>avs;
    
    		if (avs == 'a')
    		{ 
    			//input and calculate area
    			std::cout<<"\ninput width in inches:";
    			std::cin>>width;
    			std::cout<<"\ninput length in inches:";
    			std::cin>>length;
    			area=length*width;
    			std::cout<<"\narea is:";
    			std::cout<<area;
    			std::cout<<" Square Inches";
    		}
    
    
    		else if (avs == 'v')
    		{ 
    			//input and calculate volume
    			std::cout<<"\ninput width in inches:";
    			std::cin>>width;
    			std::cout<<"\ninput height in inches:";
    			std::cin>>height;
    			std::cout<<"\ninput length in inches:";
    			std::cin>>length;
    			volume=height*width*length;
    			std::cout<<"\nvolume is:";
    			std::cout<<volume;
    			std::cout<< " Cubic Inches";
    		}
    
    		else if (avs == 's')
    
    		{ //input and calculate circumfrence
    			std::cout<<"\n input diameter/2 or radius";
    			std::cin>>sphere;
    			vols = 4*pi*(sphere*sphere);
    			circ = (2*sphere)*pi;
    			std::cout<<"\narea is:";
    			std::cout<<vols;
    			std::cout<< "Cubic inches";
    			std::cout<<"\nby the way the circumfrence is";
    			std::cout<<circ;
    			std::cout<< " Inches";
    		}
    		
    		// Does the user wish to continue or quit?
    		cout << "Do you want to quit? (Y)es or (N)o?";
    		cin >> temp;
    		
    		if(temp == 'n' || temp == 'N')
    			done = false;
    		else if(temp == 'y' || temp == 'Y')
    			done = true;
    		else
    		{
    			cout << "Invalid Input...";
    			return 0;
    		}
    	} while(!done)
    		
    	return 0;
    		
    
    }
    further more, i didn't do it in the above code, but always initialize your variables when you create them.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    coverd many times, search it. Very quick way:

    Code:
    #include<iostream>
    #include<ctype.h>
    
    using namespace std;
    
    const char EXIT = 'Y';
    
    int main()
    
    {
    z:
    int area, volume, height, width, length, sphere, circ, vols, pi, y, n ;
    
    pi = 3.14159265;
    
    char prompt;
    
    cout << "enter 'y' to run (again): ";
    cin >> prompt;
    
    prompt = toupper(prompt);
    
    do
    {
    std::cout<<"\nDo you wish to calculate Area or Volume or vol of Sphere?(A/V/S)";
    
    char avs;
    
    std::cin>>avs;
    
    if (avs == 'a')
    { 
    //input and calculate area
    std::cout<<"\ninput width in inches:";
    std::cin>>width;
    std::cout<<"\ninput length in inches:";
    std::cin>>length;
    area=length*width;
    std::cout<<"\narea is:";
    std::cout<<area;
    std::cout<<" Square Inches";
    }
    
    
    else if (avs == 'v')
    { 
    //input and calculate volume
    std::cout<<"\ninput width in inches:";
    std::cin>>width;
    std::cout<<"\ninput height in inches:";
    std::cin>>height;
    std::cout<<"\ninput length in inches:";
    std::cin>>length;
    volume=height*width*length;
    std::cout<<"\nvolume is:";
    std::cout<<volume;
    std::cout<< " Cubic Inches";
    }
    
    else if (avs == 's')
    
    { //input and calculate circumfrence
    std::cout<<"\n input diameter/2 or radius";
    std::cin>>sphere;
    vols = 4*pi*(sphere*sphere);
    circ = (2*sphere)*pi;
    std::cout<<"\narea is:";
    std::cout<<vols;
    std::cout<< "Cubic inches";
    std::cout<<"\nby the way the circumfrence is";
    std::cout<<circ;
    std::cout<< " Inches";
    }
    }while (prompt = EXIT);
    
    
    
    return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify to make Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 10
    Last Post: 11-03-2008, 07:25 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 06:35 PM
  4. Making standalone APP run in background
    By hart in forum Windows Programming
    Replies: 3
    Last Post: 02-27-2005, 11:20 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

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