its me again!
(and yes im stuck on yet another problem ***)
i have created a code where the user is asked to choose a geometry formula and
can solve a problem using any of the choices that are shown.

now this code uses the switch function, but for my next assignment i have to take the exact same code and convert it so that it is a series of function calls inside the loop and switch statement. (the loop is for letting the user calc for more than one geom object)

i dont know how to start off with the functions and how my layout should be.
this is the original code that i made and that has to be converted into a function.

#include <iostream>
#include <iomanip> 
#include <cmath>
using namespace std;

int main ()
{
	
	

	//Declaration of Variables.
	double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;
	char choice;
	const double PI = 3.14159;
    
	
	//Display the menu.

	cout << "\tGeometry Calculator" << endl << endl;
	cout << setw(10) << "1. Calculate the Area of a [C]ircle" << endl;
	cout << setw(10) << "2. Calculate the Area of a [R]ectangle" << endl;
	cout << setw(10) << "3. Calculate the Area of a [T]riangle" << endl;
	cout << setw(6) << "4. [Q]uit" << endl;
    
	cout << endl;

	//Prompt User for choice.

	cout << "Enter your choice (1-4)" << endl; 
	cin >> choice;
	cout << endl;
    
  
	//Calculate the area of a Circle.
	
	switch (choice)
	{
	
	case 'C': 
	case 'c':
	case '1':
		
		cout << "Enter the radius of the circle: "; 
	    cin >> radius;
		
		if (radius < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else 
		{
			circleArea = PI * pow(radius, 2);
			cout << "The area of the circle is: " << circleArea;
		}
		
		cout << endl;
	
		break;

	
	//Calculate the area of a Rectangle.

	case 'R':
	case 'r':
	case '2':
		
		cout << "Enter the length and width of the rectangle: ";
		cin >> length >> width;
		
		if (length < 0 || width < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else 
		{
			rectangleArea = length * width;
			cout << "The area of the rectangle is: " << rectangleArea;
		}
		
		cout << endl;

		break;
		

	//Calculate the area of a Triangle.
	 
	case 'T':
	case 't':
	case '3':
        
		cout << "Enter the base and height of the triangle: ";
	    cin >> base >> height;
 
		if (base < 0 || height < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else 
		{
			triangleArea = base * height * .5;
			cout << "The area of the triangle is: " << triangleArea;
		} 	
		
		cout << endl;
	
		break;
	
	
	//Quit the program.
	
	case 'Q':
	case 'q':
	case '4':
		
		cout << "End of Program" << endl;
		cout << "Have a Great Day!" << endl;
	    cout << endl;
	
		break;
    
	
	//Invalid number.
	
	default:
	
		cout << "That is an invalid choice." << endl;
		cout << "Please select choices 1-4." << endl;
	
		break;
	} 
	
	return 0;
}

i know that i must have these function prototypes:
void printname();
void programInfo();
char Menu ();
void AreaCircle();
void AreaRectangle();
void AreaTriangle();
void Pause();

my assumption is that the function should ask the user for the values, validate the input, and display the area or an error message before it goes back to main.

the only problem for me is, i know in my head how it should be, but when it comes to writing out the actual code, im stuck!

please help!!
much appreciated!!

The way I understand it is all you have to do is create the functions you mentioned then move the code from the switch statement into the appropriate function, finally add function call inside the switch statement. Don't worry about menus etc until you get the hang of writing functions. Just make the changes one function at a time to keep you from getting confused and overwhelmed. Example:

void AreaCircle()
{
    cout << "Enter the radius of the circle: "; 
    cin >> radius;
		
    if (radius < 0)
    {
        cout << "Number must be greater than 0. Try again" << endl;
    }
    else 
    {
       circleArea = PI * pow(radius, 2);
       cout << "The area of the circle is: " << circleArea;
   }

    cout << endl;
}

Whenever you write a function you can ask yourself three questions.

What does the function need to do it's job and where does it get the information from? The information necessary may come from information passed to the function, information given to the function by the user through a keyboard or a mouse or some other user input device, or from a stream, like the ones used to read a file or whatever.

What does the function return? This can either be as a discrete return type, or it could be results to a variable passed by reference or it could be an altered file, etc.

What does the function do? In the best modeling a function would do only a single task or a set of related tasks.

You should be able to answer all three questions for any function you write before you try to write it.

Let's take the Menu() function. It will display the menu, presumably (if you name your function a name indicated it's job it makes it easier) and it will return the users choice of tasks as represented by a single char entered by through the keyboard. Everything is hardcoded and can be pulled out of main() line for line. It will need no input since all it's going to do is display the menu of options and return the users choice.

char Menu()
{
  char choice;
  cout << "\tGeometry Calculator" << endl << endl;
  cout << setw(10) << "1. Calculate the Area of a [C]ircle" << endl;
  cout << setw(10) << "2. Calculate the Area of a [R]ectangle" << endl;
  cout << setw(10) << "3. Calculate the Area of a [T]riangle" << endl;
  cout << setw(6) << "4. [Q]uit" << endl;
    
  cout << endl;

  //Prompt User for choice.

  cout << "Enter your choice (1-4)" << endl; 
  cin >> choice;
  cout << endl;
  return choice;
}

To use the function it must be declared, and may be defined, outside of main() (usually) before it is called. So something like this:

//list of includes here
char Menu();  //function declaration/prototype here
int main()
{
   char choice;
   choice = Menu();//function call
}
//function definition (copy the first code snippet and paste it here).

So pick another function and give it whirl. Do just one function at a time and keep working on it until you get it to work before you move to the next one.

This question has already been answered. Start a new discussion instead.