Wants me to find the area

closed account (D4NbpfjN)
I will be honest i have no clue where to begin with this code. I need some help with this code.



Design and implement the areas.cpp program (skeleton give so that it correctly meets the program
specifications given below.
Specifications:
Create a menu-driven program that finds and displays
areas of 3 different objects.
The menu should have the following 4 choices:
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
• If the user selects choice 1, the program should
find the area of a square.
• If the user selects choice 2, the program should
find the area of a circle.
• If the user selects choice 3, the program should
find the area of a right triangle.
• If the user selects choice 4, the program should
quit without doing anything.
• If the user selects anything else (i.e., an invalid
choice) an appropriate error message should be
printed.
Sample Run
Program to calculate areas of objects
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
2
Radius of the circle: 3.0
Area = 28.2743



In a while loop, cout the menu options and accept a char as input (cin). If it's 4, terminate the program. If the input is 1-3, call the appropriate function to perform the calculation they asked for. Get the measurement with another cin>>some_float_var;
closed account (D4NbpfjN)
I am not sure how to start the code
Just fill in the blanks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(){
	char choice;
	do{
		std::cout<<"*some input prompt*:\n";
		std::cout<<"1 -- square\n";
		std::cout<<"2 -- circle\n";
		std::cout<<"3 -- right triangle\n";
		std::cout<<"4 -- quit\n";
		std::cin>>choice;
		if(choice=='1'){calc_square();}
		else if(choice=='2'){calc_circle();}
		else if(choice=='3'){calc_triangle();}
	} while(choice!='4');
}
closed account (D4NbpfjN)
So i just use this program? Were do i fill in the blanks? I know i have to add the radius of a circle into this and the area, but how do i go about doing that?
By "blanks" DrZoidberg meant write the calc_square(), calc_circle() and calc_triangle() functions.
closed account (D4NbpfjN)
so in the () i need to add the area of a square, area of a circle and area of a traingle?
Those are functions you need to write.
Like this:

1
2
3
4
5
6
void calc_square(){
	float length;
	std::cout<<"Side length of square: "; 
	std::cin>>length;
	std::cout<<"Area = "<<length*length<<"\n\n";
}



() is for arguments
Last edited on
closed account (D4NbpfjN)
isn't the area of a square width*length?
isn't the area of a square width*length?
That gives the area of a rectangle. A square is a special case of a rectangle where height and width are the same.
closed account (D4NbpfjN)
how do i go about doing the area of the circle if it is already given?
If what is already given?

Just calculate area = pi * radius * radius
Last edited on
closed account (D4NbpfjN)
This is my code so far and i am getting errors. Could someone tell me what I am doing wrong? thank you



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;
	int main()
{
	char choice;
	do{
	std::cout << "Please choose from the choices listed.";
	std::cout << "1 --Area of a square\n";
	std::cout << "2 -- Area of a circle\n";
	std::cout << "3 -- Area of a right triangle\n";
	std::cout << "4 -- Quit\n";
	std::cin >> choice;
	
	if (choice == '1')
	float length;
	std::cout << "Enter length of square.";
	std::cin >> length;
	std::cout << "Area of the square =" << length*length << "\n\n";

	else if (choice == '2')
	float radius;
	std::cout << "Enter the radius of the circle: ";
	std::cin >> radius;
	area = 3.142 * radius * radius
		cout << "Area of a circle is =";


	else if (choice == '3')
		std::cout << "Enter the length of the right triangle:";
		std::cin >> length;
		std::cout << "Enter the width of the right traingle: ";
		std::cin >> width;
		std::cout << "Area of the right triangle =" << length*width/2 << "\n\n";

	} while (choice != '4');



	return 0; 


}
Your compiler should be telling you what is wrong. Are you looking at its traceback? It's best to ALWAYS post it so we don't have to waste time guessing or running your code.
closed account (D4NbpfjN)
one error is in line 13 saying that choice is undefined. Another error is in line 6 saying char is is expected ';'. Another error is in line 21 saying the else is suppose to be a expected statement. In line 24 it says area is undefined. Line 25 it says cout is expected a ';' Line 28 it says the else is suppose to be expected statement. Line 35 it says that the word choice is undefined . this is all the errors
One of the significant problems is that the if affects only a single statement.
1
2
    if (choice == '1')
        float length;


In order to control a group of statements under the same if, use braces {} to enclose them.
1
2
3
4
5
6
7
    if (choice == '1')
    {
        float length;
        std::cout << "Enter length of square.";
        std::cin >> length;
        std::cout << "Area of the square =" << length*length << "\n\n";
    }
Here I have updated your code and made some minor changes.As per past suggestions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
	int main()
{
	char choice;
	float radius,area,length,width;
	do
	{
	std::cout << "Please choose from the choices listed.";
	std::cout << "1 --Area of a square\n";
	std::cout << "2 -- Area of a circle\n";
	std::cout << "3 -- Area of a right triangle\n";
	std::cout << "4 -- Quit\n";
	std::cin >> choice;
	
	if (choice == '1')
	{
	std::cout << "Enter length of square.";
	std::cin >> length;
	std::cout << "Area of the square =" << length*length << "\n\n";
	}
	else if (choice == '2')
	{
	std::cout << "Enter the radius of the circle: ";
	std::cin >> radius;
	area = 3.142 * radius * radius;
	cout << "Area of a circle is =";
	}
	else if (choice == '3')
	{
	    std::cout << "Enter the length of the right triangle:";
		std::cin >> length;
		std::cout << "Enter the width of the right traingle: ";
		std::cin >> width;
		std::cout << "Area of the right triangle =" << length*width/2 << "\n\n";
	}
	} while (choice != '4');



	return 0; 


}

   

Topic archived. No new replies allowed.