Programming Project Help

The code compiles but it part of the input should not accept 0 or negative values and output certain statement in the code below. This is only the first part of my project which I have started,

Complete Project Guidelines are:


In this project you will be using a menu for the first time with some decisions, to calculate some values for a user. In particular, your program will be calculating the area or perimeter of various objects based on inputs from the user.


At the start of your program, display a menu that lets the user select between shapes (right triangle, square, circle, rectangle). Make it exactly clear how a user should enter input to select a particular menu choice. I would suggest a menu along the lines of the following:


1) Right triangle

2) Square

3) Circle

4) Rectangle

Please enter the shape you wish to use(1-4):


To implement the results of that menu, use a switch statement based on the input. In each case statement of the switch, prompt for the appropriate information necessary to calculate the perimeter and area of the associated shapes. Then display the results.


You should verify the validity of input. For example, if they enter an invalid menu choice your program should display the menu again and tell them that they entered some invalid value and prompt again. For other values (like the radius of a circle) you should verify values but can simply terminate the program if the value is invalid). For the numbers verification, ensure that no radius or size is 0 or negative.


Make sure to follow proper programming standards and try and indent in such a way that the logic of your program is clear.

If you have any advice regarding the above, please comment with 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
#include <iostream>
#include <iomanip>

using namespace std;

double lengthofrectangle;
double widthofrectangle;
double perimeterofrectangle;
double areaofrectangle;

int main () {
int shapenumber;
cout << "What type of shape would you like to find the area and perimeter for?" << endl;
cout << "1) Right Triangle " << endl;
cout << "2) Square " << endl;
cout << "3) Circle " << endl;
cout << "4) Rectangle " << endl;
cout << "Please enter the shape you wish to use(1-4)" << endl;
cin >> shapenumber;
if ( 1 <= shapenumber <= 4) {
    switch (shapenumber)
    case 4:

//Process for a rectangle
//Rectangle input variables

            cout << "What is the length of the rectangle?" << endl;
            cin >> lengthofrectangle;
            cout << "What is the width of the rectangle?" << endl;
            cin >> widthofrectangle;
//Calculate Perimeter of Rectangle
            if (lengthofrectangle, widthofrectangle > 0){
                perimeterofrectangle = (2*lengthofrectangle)+ (2*widthofrectangle);
                areaofrectangle = lengthofrectangle * widthofrectangle;
                cout << "The perimeter of the rectangle is " << perimeterofrectangle << endl;
                cout << "The area of rectangle is " << areaofrectangle << endl;
    }
            else {
    cout << "The value: " << lengthofrectangle << "or" << widthofrectangle << "is not valid." << endl;
    cout << "They should be greater than zero." << endl;
 }}
 else {
cout << "That is not a valid shape selection number. You input" << shapenumber << endl;
 }
}
closed account (48T7M4Gy)
line 20 is incorrect

It should be if ( (shapenumber <= 4) && (shapenumber >= 1) ) {

In fact, if you use a switch control properly you can do away with the statement altogether by using default:

See http://www.cplusplus.com/doc/tutorial/control/
Hello StrikingSparrow,

kemort has set you straight about line 20, but you also have a similar problem with line 32. The comma operator is not working the way you might be thinking.

http://www.cplusplus.com/doc/tutorial/operators/

the comma operator is towards the end of the page. the warning that comes is 32:55: warning: left operand of comma operator has no effect [-Wunused-value].

If you are not familiar with it in the upper right corner of your code block is a gear icon that will allow you to compile and run your program. In the bottom window in the compilation tab you can see the warnings and errors that might be in your code.

The next problem you have is with the switch. There is no opening or closing brace "{}" for the switch which makes it hard to tell what belongs to the switch and what belongs to the if statement.

http://www.cplusplus.com/doc/tutorial/control/ at the bottom of the page.

I do not know if you have learned about funcions yet, but they would be a great help to the program.

Hope that helps,

Andy
Here is the updated version of my code with my progress on the entirety of the project
I can't quite get my code to work the way I want, particularly i can't tell if my switch and if statements are okay.
Can you guys help me edit it please. My errors are below as well and project guidelines are on top. I'm sorry I was trying to fix it but made it worse.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;


int main () {

bool shapenumber;

double lengthofrectangle;
double widthofrectangle;
double perimeterofrectangle;
double areaofrectangle;
const double pi = 3.14;
double lengthofradius;
double perimeterofcircle;
double areaofcircle;
double widthofsquare;
double perimeterofsquare;
double areaofsquare;
double widthofrighttriangle;
double lengthofrighttriangle;
double hypotenuseofrighttriangle;
double perimeterofrighttriangle;
double areaofrighttriangle;

do while (shapenumber < 1 && shapenumber > 4) {
cout << "What type of shape would you like to find the area and perimeter for?" << endl;
cout << "1) Right Triangle " << endl;
cout << "2) Square " << endl;
cout << "3) Circle " << endl;
cout << "4) Rectangle " << endl;
cout << "Please enter the shape you wish to use(1-4)" << endl;
cin >> shapenumber;

if (!(shapenumber == 1 || shapenumber == 2 || shapenumber == 3 || shapenumber == 4 )) {
    cout << "The value "  << shapenumber << " is not a valid value, please re-enter" << endl;
            } }

switch (shapenumber)
//Process for a rectangle
//Rectangle input variables
            case (4)

            cout << "What is the length of the rectangle in inches?" << endl;
            cin >> lengthofrectangle;
            cout << "What is the width of the rectangle in inches?" << endl;
            cin >> widthofrectangle;
//Calculate Perimeter of Rectangle

            if (lengthofrectangle > 0 && widthofrectangle > 0){
                perimeterofrectangle = (2*lengthofrectangle)+ (2*widthofrectangle);
                areaofrectangle = lengthofrectangle * widthofrectangle;
                cout << "The perimeter of the rectangle is " << perimeterofrectangle << " inches." << endl;
                cout << "The area of rectangle is " << areaofrectangle << " inches squared." << endl;
return 0;
            }

            do while ( lengthofrectangle < 0 || widthofrectangle < 0 ) {
    cout << "The value: " << lengthofrectangle << " and/or " << widthofrectangle << " is not valid." << endl;
    cout << "The length and width should be greater than zero, please re-enter." << endl;

 }

//Process for a Circle
//Rectangle input variables
    case (3):

            cout << "What is the length of the radius in inches?" << endl;
            cin >> lengthofradius;
//Calculate Perimeter of circle

                if (lengthofradius > 0){
                perimeterofcircle = pi * 2 * lengthofradius;
                // Calculate the Area of a Circle
                areaofcircle = pi * (pow(lengthofradius,2));
                cout << "The perimeter of the circle is " << perimeterofcircle << " inches." << endl;
                cout << "The area of rectangle is " << areaofcircle << "inches squared." << endl;
return 0;
    }

            do while (lengthofradius <=0) {
    cout << "The value: " << lengthofradius << " inches is not valid." << endl;
    cout << "The length and width should be greater than zero, please re-enter." << endl;

 }

//Process for a square
//square input variables
            case (2):
            cout << "What is the width of the square in inches?" << endl;
            cin >> widthofsquare;
//Calculate Perimeter of Rectangle

            if (widthofrectangle > 0){
                perimeterofsquare = 4*widthofsquare;
                areaofrectangle = widthofsquare * widthofsquare;
                cout << "The perimeter of the square is " << perimeterofsquare << " inches." << endl;
                cout << "The area of rectangle is " << areaofsquare << " inches squared." << endl;
return 0;
}
            do while (widthofsquare <= 0 ) {
    cout << "The value: " << widthofsquare << " is not valid." << endl;
    cout << "The width should be greater than zero, please re-enter." << endl;

 }
 }

//Process for a Right triangle
//Right Triangle input variables
     case (1):

            cout << "What is the length of one side of the right triangle in inches?" << endl;
            cin >> lengthofrighttriangle;
            cout << "What is the width of the rectangle in inches?" << endl;
            cin >> widthofrighttriangle;
//Calculate Perimeter of Rectangle
            if (lengthofrighttriangle > 0 && widthofrighttriangle > 0){
                hypotenuseofrighttriangle = sqrt((pow(lengthofrighttriangle,2))+ (pow(widthofrighttriangle,2)) );
                perimeterofrighttriangle  = lengthofrectangle + widthofrectangle + hypotenuseofrighttriangle ;
                areaofrectangle = lengthofrectangle * widthofrectangle;
                cout << "The perimeter of the right triangle is " << perimeterofrighttriangle << " inches." << endl;
                cout << "The area of right triangle is " << areaofrighttriangle << " inches squared." << endl;
return 0;
    }
            do while (lengthofrighttriangle <= 0 || widthofrighttriangle <= 0)  {
    cout << "The value: " << lengthofrighttriangle << " and/or " << widthofrighttriangle << " is not valid." << endl;
    cout << "The length and width should be greater than zero, please re-enter." << endl;

 }



Errors:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
\workspace\Project 4\Project 4.cpp||In function 'int main()':|
\workspace\Project 4\Project 4.cpp|42|error: expected 'while' before '(' token|
\workspace\Project 4\Project 4.cpp|45|error: expected ';' before 'case'|
\workspace\Project 4\Project 4.cpp|45|error: case label '4' not within a switch statement|
\workspace\Project 4\Project 4.cpp|47|error: expected ':' before 'cout'|
\workspace\Project 4\Project 4.cpp|69|error: expected 'while' before '(' token|
\workspace\Project 4\Project 4.cpp|69|error: expected ';' before ':' token|
\workspace\Project 4\Project 4.cpp|69|error: expected primary-expression before ':' token|
\workspace\Project 4\Project 4.cpp|92|error: expected 'while' before '(' token|
\workspace\Project 4\Project 4.cpp|92|error: expected ';' before ':' token|
\workspace\Project 4\Project 4.cpp|92|error: expected primary-expression before ':' token|
\workspace\Project 4\Project 4.cpp|109|error: expected 'while' before '}' token|
\workspace\Project 4\Project 4.cpp|109|error: expected '(' before '}' token|
\workspace\Project 4\Project 4.cpp|109|error: expected primary-expression before '}' token|
\workspace\Project 4\Project 4.cpp|109|error: expected ')' before '}' token|
\workspace\Project 4\Project 4.cpp|109|error: expected ';' before '}' token|
\workspace\Project 4\Project 4.cpp|113|error: expected unqualified-id before 'case'|
\workspace\Project 4\Project 4.cpp|116|error: 'cin' does not name a type|
\workspace\Project 4\Project 4.cpp|117|error: 'cout' does not name a type|
\workspace\Project 4\Project 4.cpp|118|error: 'cin' does not name a type|
\workspace\Project 4.cpp|120|error: expected unqualified-id before 'if'|
\workspace\Project 4.cpp|128|error: expected unqualified-id before 'do'|
||=== Build failed: 21 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Hello StrikingSparrow,

You may want to read this:

http://www.cplusplus.com/doc/tutorial/control/

I have not tried this yet, but what I see looking over what you have. Line 11bool shapenumber; is going to bee a problem. As a bool the only possible values are false or true or 0 or 1. Using numbers like 2, 3 and 4 may work, but not the way you might think. Any number greater than zero translates to true.

Your "do while" loops are the right idea, but the wrong execution see link above.
The first "do while" would be correctly written as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do
{
	cout << "What type of shape would you like to find the area and perimeter for?" << endl;
	cout << "1) Right Triangle " << endl;
	cout << "2) Square " << endl;
	cout << "3) Circle " << endl;
	cout << "4) Rectangle " << endl;
	cout << "Please enter the shape you wish to use(1-4)" << endl;
	cin >> shapenumber;

	if (!(shapenumber == 1 || shapenumber == 2 || shapenumber == 3 || shapenumber == 4))
		cout << "The value " << shapenumber << " is not a valid value, please re-enter" << endl;
        else              // <--- Added these two lines
                break; // <--- To break out of the loop with a valid value.
}while (true)


At first I did not think the if statement would work, but it does.

Lines 59, 82, 103 and 127 are not needed there. This is where you should use a break statement. to exit the switch. return 0; should be the last line before the closing brace of main.

I do not think it will make any difference, but case statements generally go from lowest to highest. See link above.

In your case statements I do not think the do while loops will work except to possibly create an endless loop, but I have not tested this yet.

There is a start.

Hope that helps,

Andy
Topic archived. No new replies allowed.