Im new to coding , See the reply below

Given task. Write a program that will produce the output as figure below

#include<iostream>
using namespace std;

main ()
{

cout<<"Rectangle Menu ";
cout<<"\n1. Area ";
cout<<"\n2. Perimeter ";
cout<<"\n3. Diagonal ";

cout<<"\n\nEnter your choice:1 ";
cout<<"\nEnter length and breadth:10 5 ";
cout<<"\nArea is :157.1";

cout<<"\n\nWant to enter more<y/n>?y";
cout<<"\nRectangle Menu";
cout<<"\n1. Area ";
cout<<"\n2. Perimeter ";
cout<<"\n3. Diagonal ";

cout<<"\n\nEnter your choice:2";
cout<<"\nEnter length and breadth:10 3";
cout<<"\nPerimeter is:26";

cout<<"\n\nWant to enter more<y/n>?n";

return 0;


}//close main function
Last edited on
Hello CooloN,

Welcome to the forum.

First:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can us the preview button at the bottom to see how it looks.

Second: you will get a better response by posting code that you have written and describing what your problem is. That being said.

I would think about what variables you will need and what type they should be.

Looking at what you posted I am thinking of at least four functions i.e., menu, area, perimeter and diagonal. There is a fifth possibility for a function to get the input since it is always the same.

In main and the menu functions I would most likely use a do while loop to keep the functions running until ended by the user. With the menu function checking for valid input.

Put some code together and post it back here to this thread to get the help that you need.

Hope that helps,

Andy
Ty for replying and sorry if i dint follow the rule, im new here . Below is my problem.

Given task: Write a program that will produce the output as figure below

I dont know how to calculate/use formula.
Below is the example of program the task given.

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
#include<iostream>
using namespace std;

main ()
{

cout<<"Rectangle Menu ";
cout<<"\n1. Area ";
cout<<"\n2. Perimeter ";
cout<<"\n3. Diagonal ";

cout<<"\n\nEnter your choice:1 ";
cout<<"\nEnter length and breadth:10 5 ";
cout<<"\nArea is :157.1";

cout<<"\n\nWant to enter more<y/n>?y";
cout<<"\nRectangle Menu";
cout<<"\n1. Area ";
cout<<"\n2. Perimeter ";
cout<<"\n3. Diagonal ";

cout<<"\n\nEnter your choice:2";
cout<<"\nEnter length and breadth:10 3";
cout<<"\nPerimeter is:26";

cout<<"\n\nWant to enter more<y/n>?n";

return 0;


}//close main function 
The way I would go about this exercise is by:

- creating a switch statement
- use variable names and cin to read in the info
- Get the required formulas and perform the calculations
- put the switch statement inside a do while loop while yes


I dont know how to calculate/use formula.


To calculate the area of a rectangle is the length x width
This could be represented by
1
2
3
4
5
6
int l = 0, w = 0;
cout << "Enter the length";
cin >> l;
cout << "Enter the width";
cin >> w;
int rectArea = l * w;
Hello CooloN,

No worries about being new, it took me awhile to figure out how everything works.

I will still refer you to my first reply. What I started with was working with the menu. Although mine are in two different files this is what I came up with,

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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

#define CLS system("cls")  //  quick version. Newer version calls a function.

//  Proto types of forward declarations.
int Menu();

int main()
{
    int menuChoice{ 0 };
    char cont{'Y'};

    menuChoice = Menu();

    return 0;
}  //  End main function

//  This is a reduced version from what I did. It should work. Let me know if it does not.

int Menu()
{
	int menuChoice{ 0 };
	bool cont{ true };

	CLS;
	
	do
	{
		std::cout << "\n Rectangle Menu ";
		std::cout << "\n 1. Area ";
		std::cout << "\n 2. Perimeter ";
		std::cout << "\n 3. Diagonal ";
		std::cout << "\n 4. Exit ";
		std::cout << "\n\n Enter your choice:1 ";
		menuChoice = _getch() - 48;  //  Gives menuChoice the numbers 1, 2... with one key press.

    //  Next two blocks are for validation.

		while (!std::cin)  //  Runs if something other than a number is entered.
		{
			std::cout << "\n\n Invalid entry. Choices are 1, 2, 3 or 4" << std::endl;
			std::this_thread::sleep_for(std::chrono::seconds(3));  //  include header files chrono and thread.
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			std::cout << "Enter your choice:1 ";
			menuChoice = _getch() - 48;
		}

		if (menuChoice < 1 || menuChoice > 4)
		{
			std::cout << "\n\n Invalid entry. Choices are 1, 2, 3 or 4" << std::endl;
			std::this_thread::sleep_for(std::chrono::seconds(3));
		}
		else break;

		CLS;
	} while (cont);

		return menuChoice;
}


Something to start with. When "Menu()" returns to main I used a "switch case" to deal with the menu choice.

Waiting to see what you have come up with.

Hope that helps,

Andy

P.S. to find the formulas I did a google search on "area of a rectangle" then changed "area" to "perimeter" and "diagonal". The formula for "diagonal" was a little more involved.
Last edited on
Here is my version
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
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
	int area = 0, perimiter = 0, diagonal = 0;
	int length = 0, width = 0;
	int choice = 0;
	
	 
	cout << "\nEnter the length of the rectangle: ";
	cin >> length;
	cout << "\nEnter the width of the rectangle: ";
	cin >> width;

	do
	{
		cout << "\n\nRectangle Menu ";
		cout << "\n1. Area ";
		cout << "\n2. Perimeter ";
		cout << "\n3. Diagonal ";
		cout << "\n4. Enter new rectangle";
		cout << "\n5. Exit";
		cout << "\nEnter options <1 - 4>";
		cin >> choice;
		switch (choice)
		{
		case 1:
			area = length * width;
			cout << "\nThe area is " << area;
			break;
		case 2:
			perimiter = 2*(length + width);
			cout << "\nThe perimter is " << perimiter;
			break;
		case 3:
			diagonal = sqrt((width * width) + (length * length));
			cout << "\nThe diagonal is " << diagonal;
			break;
		case 4:
			cout << "\nEnter the length of the new rectangle: ";
			cin >> length;
			cout << "\nEnter the width of the new rectangle: ";
			cin >> width;
			break;
		default:
			 cout << "Incorrect Entry";
		}
	} while (choice != 5);

	cout << "\nGoodbye thanks for using this program!";

	cin.get();
	return 0;

}
Last edited on
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
#include<iostream>
using namespace std;

int main () 
{

cout<<"Rectangle Menu ";
cout<<"\n1. Area ";
cout<<"\n2. Perimeter ";
cout<<"\n3. Diagonal ";

cout<<"\n\nEnter your choice:1 ";
cout<<"\nEnter length and breadth:10 5 ";
cout<<"\nArea is :157.1";

cout<<"\n\nWant to enter more<y/n>?y";
cout<<"\nRectangle Menu";
cout<<"\n1. Area ";
cout<<"\n2. Perimeter ";
cout<<"\n3. Diagonal ";

cout<<"\n\nEnter your choice:2";
cout<<"\nEnter length and breadth:10 3";
cout<<"\nPerimeter is:26";

cout<<"\n\nWant to enter more<y/n>?n";

return 0; // use int to return something if you don't want to use int use void main()


}//close main function
Hey Andy , ty for the code . But , got some error. Line 26,36 thread has not been declared , i already fix it , added #include <cstdlib> #include<conio.h> . But line 43 , it said std::this_thread' has not been declared , i dont know how to fix it.
imohamme5 , ty for the formula and code . Your code work fine , ill update soon if the asnwer is correct.
Last edited on
bird1234 . Ty , hahah
Hello CooloN,

As the comment said, include the header files "chrono" and "thread".

Andy
TY guys for helping me , this what its look like.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//This is solution 

#include<iostream>
#include<math.h>

using namespace std;

main()
{
    //local declaration
    //io statement

   int choice;
   float length, breadth, area, perimeter, diagonal;
   char answer;

do
  {

   //Display Rectangle Menu
   cout<<"Rectangle Menu\n";
   cout<<"1. Area\n";
   cout<<"2. Perimeter\n";
   cout<<"3. Diagonal\n";

   cout<<"\n\nEnter your choice: ";
   cin>>choice;

   if (choice ==1)

   {
       cout<<"\nArea calculation\n";
       cout<<"Enter length and breadth:";
       cin>>length>>breadth;
       area=length * breadth;
       cout<<"\nThe area of rectangle is :"<<area<<endl;
   }

else if (choice ==2)
{
    cout<<"Perimeter Calculation\n";
    cout<<"Enter length and breadth:";
    cin>>length>>breadth;
    perimeter= (2* length)* (2* breadth);
    cout<<"\nThe perimeter of rectangle is :"<<perimeter<<endl;


}

else
{
    cout<<"\nDiagonal Calculation\n";
    cout<<"Enter length and breadth:";
    cin>>length>>breadth;
    diagonal=sqrt(length*length)+(breadth*breadth);
    cout<<"\nThe diagonal of rectangle is :"<<diagonal<<endl;

}//exit if

cout<<"Want to enter more (y/n)";
cin>>answer;

}//close do

while (answer =='y');

}
Topic archived. No new replies allowed.