i have this code here that i have written out and im i want to convert it using functions.
basically it is a geometry calculator that will let you calculate the area for any geometric object (ie rectangle, cirlce, triangle)

basically i have to rewrite the code and add a loop so that the uuser can calculate more than one geometric object.

here was my code that was only with switch statements:

#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 << setw(10) << "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;
}

im thinking that the layout should be like this:

function prototypes

int main()

loop (on yes)
function calls
-->switch statement

loop (on no)
---> end program

///functions below

it seems that this is something that is very easy--but the switch statment throws me off as im not sure if it should be part of the function or in main itself.

thanks for the help!!

Recommended Answers

All 6 Replies

17 views no replies??

im really stuck here guys!
any guidance would be of great help!

I usually approach this kind of a problem like:

do
{
    menu_func( )  //which may also get the choice and return it

    switch( user_choice )
    {
       //do the work here

    }
}while (user_choice != quit_value );

I really didnt understand your question, as per my understanding..

int main(){
displayMessages();
do{
int choice = displayMenu();
switch(choice)
{
case 1:
doWork();
-
-
while(choice != 'q');
}
}

basically i take the same code and use functions in it.

^^i followed your layout and this is what i got:

#include <iostream>
#include <iomanip> 
#include <cmath>
using namespace std;
int menu(ostream& out);



char choice;

int main ()
{

	double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;
	const double PI = 3.14159;

	do
	{
	
		menu(cout);

		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;
	} while

	return 0;
}

//functions
int menu(ostream& out)
{
	out << "\tGeometry Calculator" << endl << endl;
	out << setw(30) << "1. Calculate the Area of a [C]ircle" << endl;
	out << setw(30) << "2. Calculate the Area of a [R]ectangle" << endl;
	out << setw(30) << "3. Calculate the Area of a [T]riangle" << endl;
	out << "4. [Q]uit" << endl;

	out << setw(10) << "Enter your choice (1-4)\n";
	cin >> choice;
	
	return choice;

}

Couple of suggestions,
1. Avoid using global variables as you used choice here..
2. you can further shrink the main by moving choices to methods here..
3. Display result can also be another method to display the result in generic format

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.