Hello all, sorry if this question is in any way derivative. I searched through a lot of other similar questions, but I didn't find anything that was exactly the same.
This is a homework assignment, for an online-only class (huge mistake!) I have spent a lot of time on it, but apparently still do not understand functions. I am having so much trouble with this, I am probably making stupid mistakes because I have looked at it too many times.
#include <iostream>
#include <iomanip>
usingnamespace std;
int displayMenu();
double calcAreaCircle( double radius );
double calcAreaRect( double length, double width );
double calcAreaTriangle( double base, double height );
constdouble Pi = 3.14159;
int main ()
{
double menu;
menu=displayMenu();
double circ_area;
circ_area = calcAreaCircle ();
double rect_area
rect_area = calcAreaRect ();
double tri_area
tri_area = calcAreaTriangle ();
switch(choice)
{
case 1:
{
cout << "Please enter the Radius:" << endl;
cin >> radius;
}
break;
case 2:
{
cout << "Please enter the Length:" << endl;
cin >> length;
cout << endl;
cout << "Please enter the width:" << endl;
cin >> width;
cout << endl;
}
break;
case 3:
{
cout << "Please enter the Base:" << endl;
cin >> base;
cout << endl;
cout << "Please enter the Height:" << endl;
cin >> height;
cout << endl;
}
break;
case 4:
cout<< "Thank you for using the geometry calculator, Bye! "<<endl;
system ("pause");
break;
}
}
int displayMenu()
{
int choice;
cout << "Geometry Calculator"<<endl;
cout << "1. Calculate the Area of a Circle"<<endl;
cout << "2. Calculate the Area of a Rectangle"<<endl;
cout << "3. Calculate the Area of a Triangle"<<endl;
cout << "4. Quit";
cout << endl;
cin >> choice;
while (choice < 1 || choice > 4)
{
cout << "Invalid selection. Enter 1, 2, 3, or 4: ";
cin >> choice;
}
}
return choice;
}
double calcAreaCircle(double radius)
{
double circ_area = Pi*radius*radius;
if (radius > 0)
{
cout << "The area of the circle " << radius << " is: " << area << endl;
system ("pause");
return 0;
}
else
{
cout << "You did not enter a valid number" << endl;
cout << "The program will now restart" << endl;
system ("pause");
return circ_area;
}
}
double calcAreaRect (double width, double length)
{
double rect_area = length * width
if (length > 0, width > 0)
{
cout << "The Area for the Rectangle is: " << area << endl;
system ("pause");
return area;
}
else
{
cout << "You did not enter valid numbers." << endl;
cout << "The program will now restart." << endl;
system ("pause");
return rect_area;
}
}
double calcAreaTriangle (double base, double height )
{
double tri_area = base * height
if (base > 0, height > 0)
{
area = .5 * base * height;
}
else
{
cout << "You did not enter a valid number." << endl;
cout << "The program will now restart." << endl;
system ("pause");
return tri_area;
}
}
I am getting many errors, especially where the switch case is concerned. I would appreciate any help, even a tip in the right direction would be extremely helpful.
Thanks
For one, displayMenu() needs to return a value of type int. Two, you need to pass a value to the functions themselves. In the parenthesis after you make the function calls (such as calcAeraTriangle() on line 25) you actually have to put the two variables who hold the value for length and with that you want to use. Three, for the three functions you do have for calculations, they need to have their returns (return area, return rect_area, et cetera) outside of the if-else loop. Also, you keep referring to a variable called area... There is no variable called area. Fourthly, you need the function calls to be after you actually have something to put on for base and height, or radius. Put them inside of the switch statement for their respective area. Fifthly, your switch statement refers to a variable choice. There is no variable choice. You want to replace that with menu. The variable "choice" in menu is limited to the menu function. Inside of main(), the variable choice does not exist.