Thread: Noobie wanting to Expand and simplify this prog

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

    Noobie wanting to Expand and simplify this prog

    I am wanting to add more math functions to this prog.
    As I said I'm a noobie(learning c++ 6 mo's )

    I am starting to learn pointers / arrays and need help.
    any way to simplify and make program flow better

    Thanks

    Code:
    #include<iostream>
    int main()
    
    {
    	int area, volume, height, width, length, sphere, circ, vols, pi, done, 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;					//make pi=to value
    	
    	do									//start do loop
    	{
    		std::cout<<"\nDo you wish to calculate Area or Volume or vol of Sphere?(A/V/S)";
    
    		char avs;						//int chars a v and s
    
    		std::cin>>avs;					//input one of the three
    
    		if (avs == 'a')					//if input=this  do the below then goto the end
    		{ 
    			//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;			//formula for context
    			std::cout<<"\narea is:";
    			std::cout<<area;
    			std::cout<<" Square Inches\n";
    		}
    
    
    		else if (avs == 'v')			//if input=this  do the below then goto the end
    		{	
    				//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;	//formula for context
    			std::cout<<"\nvolume is:";
    			std::cout<<volume;
    			std::cout<< " Cubic Inches\n";
    		}
    
    		else if (avs == 's')			//if input=this  do the below then goto the end
    
    		{		
    				//input and calculate circumfrence
    			std::cout<<"\n input diameter/2 or radius";
    			std::cin>>sphere;
    			vols = 4*pi*(sphere*sphere);	//formula for context
    			circ = (2*sphere)*pi;			//formula for context
    			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\n";
    		}
    		
    											// Does the user wish to continue or quit?
    		std::cout<<"\nDo you want to quit? (Y)es or (N)o?";
    		std::cin >> temp;
    		
    		if(temp == 'n' || temp == 'N')
    			done = false;
    		else if(temp == 'y' || temp == 'Y')
    			done = true;
    		else
    		{
    			std::cout << "Invalid Input...";
    			return 0;
    		}
    	} while(!done)
    		
    ;
    	
    return 0;
    		//end of volarea2,1.cpp
    }

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    First off, you might want to add
    Code:
    using namespace std;
    underneath your includes so you can use cout instead of typing std::cout every time. Your main is rather large, you may want to break it up into functions. Your main could get the choice from the user, then set up something like this:
    Code:
    switch(avs)
    {
         case 'a': area_function(); break;
         case 'v': volume_function(); break;
         case 'c': circumference_function(); break;
         default: cout << "Bad Choice.\n"; break;
    }
    Lastly, you might want to think of creating a sphere class that would hold all of the variables and have functions to calculate the area, volume, and circumference.

  3. #3
    Registered User bandito9111's Avatar
    Join Date
    Feb 2003
    Posts
    4
    Sorry I must have uploaded old code
    I already did namespace std

    thanks for suggestion
    that was going to be my next step to create functions for different operations.

    any more anyone!?

  4. #4
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Code:
    const float pi = 3.14159265;
    float area, volume, height, width, length, sphere, circ, vols;
    bool done;
    area, height, width, and length could remain int, but the rest should definetely be floating point values (especially pi).

  5. #5
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33
    Code:
    std::cout<<"\nDo you want to quit? (Y)es or (N)o?";
    std::cin >> temp;
    
    if(temp == 'n' || temp == 'N')
    done = false;
    else if(temp == 'y' || temp == 'Y')
    done = true;
    else
    {
    std::cout << "Invalid Input...";
    return 0;
    }
    you can change this in to this
    Code:
    std::cout<<"\nDo you want to quit? (Y)es or (N)o?";
    std::cin >> temp;
    
    if(toupper(temp)=='N')
    done = false;
    else if(toupper(temp)=='Y')
    done = true;
    else
    {
    std::cout << "Invalid Input...";
    return 0;
    }

Popular pages Recent additions subscribe to a feed