how to use functions

This is a discussion on how to use functions within the C++ Programming forums, part of the General Programming Boards category; i need to rewrite this program using functions, but i have no idea how. i've read up on it and ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    how to use functions

    i need to rewrite this program using functions, but i have no idea how. i've read up on it and the more i read the more confused i am. could someone give an example or show me how to use a function, say for the sphere, or any of the shapes? or give me some tips or advice. it'd be much appreciated, because i'm completely in over my head.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <math.h>
    using namespace std;
    int main()
    
    { 
    int choice, //declaring integer variables to be used
    	radius_sphere, 
    	radius_cone, 
    	length_cone, 
    	length_rectprism, 
    	width_rectprism, 
    	height_rectprism, 
    	length_pyramid, 
    	slant_height_pyramid;
    double surface_area_sphere, //declaring double variables to be used
    	   surface_area_cone, 
    	   surface_area_rectprism, 
    	   surface_area_pyramid;
    const double PI=3.14159; //setting PI to be a constant double with a value of 3.14159
    
    cout<<"\nSurface Area Calculator" //displaying the initial menu to the user
    <<"\n1) Sphere"
    <<"\n2) Cone"
    <<"\n3) Rectangular Prism"
    <<"\n4) Pyramid"
    
    <<"\n\n5) Quit"
    <<"\n\nEnter Your Choice: "; //prompting user to select which option from the menu they would like
    cin>>choice; //accepting user's choice from the menu
    
    //setting while loop so that if a number larger than 5 or less than 1 is entered for choice, the user will receive an error message and be re-prompted to enter a new choice until he enters a valid one
    while (choice>5||choice<1)
    	{
    	cout<<"\nInvalid number, please try again: "; 
    	cin>>choice;
    	}
    	//setting while loop so that the body will continue to run as long as choice is less than 5
    while (choice<5)
    {
    
    	if (choice==1) //setting if statement so that user enters 1 for choice, they will be able to calculate the surface area of a sphere
    	{
    
    	
    	cout<<"\nPlease enter the radius of the sphere: "; 
    	cin>>radius_sphere; 
    	surface_area_sphere=4*PI*radius_sphere*radius_sphere; //setting equation to calculate the surface area of the sphere based on the user's input
    	cout<<"\nThe surface area of a sphere with radius "<<radius_sphere<<" is "<<fixed<<setprecision (4)<<surface_area_sphere; //displaying the calculated surface area of the sphere with 4 places after the decimal point
    
    	cout<<"\n\nSurface Area Calculator" //displaying the menu again for the user to choose another option
    	<<"\n1) Sphere"
    	<<"\n2) Cone"
    	<<"\n3) Rectangular Prism"
    	<<"\n4) Pyramid"
    
    	<<"\n\n5) Quit"
    	<<"\n\nEnter Your Choice: ";
    	cin>>choice;
    		while (choice>5||choice<1) //setting while loop so if choice is less than 1 or more than 5, the user will receive an error message and be prompted to try again
    		{
    		cout<<"\nInvalid number, please try again: ";
    		cin>>choice;
    		}
    	}
    	if (choice==2) //setting if statement so that if the user enters 2 for choice, they will be able to calculate the surface area of a cone based on their inputs
    	{
    
    	cout<<"\nPlease enter the radius of the cone: ";
    	cin>>radius_cone; 
    	cout<< "\nplease enter the length of the cone: ";
    	cin>>length_cone; 
    	surface_area_cone=(PI * radius_cone * length_cone) + (PI * radius_cone * radius_cone); //setting equation to calculate the surface area of the cone based on the user inputs
    	cout<<"\nThe surface area of a cone with radius "<<radius_cone 
    	<<" and length "<<length_cone<<" is "<< fixed<<setprecision (4)<<surface_area_cone; //displaying the surface area of the cone to 4 decimal places
    
    	cout<<"\n\nSurface Area Calculator"
    	<<"\n1) Sphere"
    	<<"\n2) Cone"
    	<<"\n3) Rectangular Prism"
    	<<"\n4) Pyramid"
    
    	<<"\n\n5) Quit"
    	<<"\n\nEnter Your Choice: ";
    	cin>>choice;
    		while (choice>5||choice<1) //setting while loop to display error message if user enters an invalid number, and prompting them to enter a new one. 
    		{
    		cout<<"\nInvalid number, please try again: ";
    		cin>>choice;
    		}
    	}
    	if (choice==3) //setting if statement so that if user enters 3 for choice, they will be able to calculate the area of a rectangular prism
    	{
    	cout<<"\nPlease enter the length of the prism: ";
    	cin>>length_rectprism;
    	cout<<"\nPlease enter the width of the prism: ";
    	cin>>width_rectprism;
    	cout<<"\nPlease enter the height of the prism: ";
    	cin>>height_rectprism;
    	surface_area_rectprism=(2*length_rectprism*width_rectprism)+(2*width_rectprism*height_rectprism)+(2*length_rectprism*height_rectprism); //setting the formula for the surface area of a rectangular prism
    	cout<<"\nThe surface area of a rectangular prism with length "<<length_rectprism<<" and width "<<width_rectprism<<"\nand height "<<height_rectprism<<" is: "<<fixed<<setprecision (4)<<surface_area_rectprism; //displaying the surface area based on user's inputs, and re-displaying the menu
    	cout<<"\n\nSurface Area Calculator"
    	<<"\n1) Sphere"
    	<<"\n2) Cone"
    	<<"\n3) Rectangular Prism"
    	<<"\n4) Pyramid"
    
    	<<"\n\n5) Quit"
    	<<"\n\nEnter Your Choice: ";
    	cin>>choice;
    		while (choice>5||choice<1) //setting while loop to display error message if user enters invalid input and prompting them to try again.
    		{
    		cout<<"\nInvalid number, please try again: ";
    		cin>>choice;
    	    }
    	}
    	if (choice==4) //setting if statement so that if user enters 4 for choice, they will be able to calculate the area of a pyramid
    	{
    	cout<<"\nPlease enter the length of the pyramid: ";
    	cin>>length_pyramid;
    	cout<<"\nPlease enter the slant height of the pyramid: ";
    	cin>>slant_height_pyramid;
    	surface_area_pyramid=(2*length_pyramid*slant_height_pyramid)+(length_pyramid*length_pyramid); //setting equation to calculate the surface area of a pyramid based on their inputs
    	cout<<"\nThe surface area a pyramid with length "<<length_pyramid<<" and height "<< slant_height_pyramid<<" is: "<<fixed<<setprecision (4)<<surface_area_pyramid; //displaying the surface area based on user's inputs and re-displaying menu
    	cout<<"\n\nSurface Area Calculator"
    	<<"\n1) Sphere"
    	<<"\n2) Cone"
    	<<"\n3) Rectangular Prism"
    	<<"\n4) Pyramid"
    
    	<<"\n\n5) Quit"
    	<<"\n\nEnter Your Choice: ";
    	cin>>choice;
    		while (choice>5||choice<1)//setting while loop to display error message if user enters and invalid number and prompting them to try again
    		{
    		cout<<"\nInvalid number, please try again: ";
    		cin>>choice;
    		}
    	}
    
    
    
    }//closing while loop, loop will end when user enters 5 for choice, and the program will terminate.
    
    return 0;
    }
    Last edited by joeman; 02-17-2010 at 01:37 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    For example,
    Code:
    void ShowMenu()
    {
    cout<<"\nSurface Area Calculator" //displaying the initial menu to the user
    <<"\n1) Sphere"
    <<"\n2) Cone"
    <<"\n3) Rectangular Prism"
    <<"\n4) Pyramid"
    
    <<"\n\n5) Quit"
    <<"\n\nEnter Your Choice: "; //prompting user to select which option from the menu they would like
    }
    
    int main()
    {
        // ...
        ShowMenu();
        // ...
    }
    You also need to indent more consistently.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    cool, thanks a lot for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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