Wants me to find the area

Jun 1, 2016 at 5:40pm
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



Jun 1, 2016 at 5:45pm
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;
Jun 1, 2016 at 5:56pm
closed account (D4NbpfjN)
I am not sure how to start the code
Jun 1, 2016 at 6:05pm
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');
}
Jun 1, 2016 at 6:31pm
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?
Jun 1, 2016 at 6:36pm
By "blanks" DrZoidberg meant write the calc_square(), calc_circle() and calc_triangle() functions.
Jun 1, 2016 at 7:01pm
closed account (D4NbpfjN)
so in the () i need to add the area of a square, area of a circle and area of a traingle?
Jun 1, 2016 at 7:05pm
Those are functions you need to write.
Jun 1, 2016 at 7:11pm
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 Jun 1, 2016 at 7:13pm
Jun 1, 2016 at 8:13pm
closed account (D4NbpfjN)
isn't the area of a square width*length?
Jun 1, 2016 at 8:17pm
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.
Jun 1, 2016 at 8:37pm
closed account (D4NbpfjN)
how do i go about doing the area of the circle if it is already given?
Jun 1, 2016 at 8:46pm
If what is already given?

Just calculate area = pi * radius * radius
Last edited on Jun 1, 2016 at 8:47pm
Jun 1, 2016 at 8:56pm
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; 


}
Jun 1, 2016 at 9:36pm
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.
Jun 1, 2016 at 10:00pm
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
Jun 1, 2016 at 10:24pm
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";
    }
Jun 2, 2016 at 7:09am
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.