Check my code, Please?

I am to write a program that will determine the area of certain shapes (a rectangle, circle, sphere, and a box). I have written what I think will do that but I am having trouble because I cannot check my code with the debugger on my IDE.
I am using xcode on mac and I cannot find the damn "run" button. So here I am asking people to check my code for me.

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

#inlcude <iostream>
#include <cmath>
using namespace std;
int main()
{
    cout << "Please enter the shape that you would like to have"
    "have calculated?"
    "Please select select the letter that corresponds"
    "with the shape you desire"
    float rectangle
    int rectangle
    rectangle = length * width
    cin >> rectangle;
    int circle
    float circle
    circle = 2 * pi * radius
    cin >> circle;
    int sphere
    float sphere
    sphere = 4 * pi * radius ^ 2
    cin >> sphere;
    int box
    float box
    box = length * width * height
    cin >> box;
    if ( rectangle <= 0, circle <= 0, sphere <= 0, box <= 0, )
    {
        else
        {
            cout << "The requested shape is not recognized"
            "the system. Thank you" << endl;
        }
    }
        return 0;
}
Hi,

Did you know that you can compile & run your code right here by using the gear icon on the right of the code you posted?

You appear to have multiple problems going on here, I think you need to completely review your knowledge right from the beginning. Have a look at the tutorial at the top left of this page.

One can't have the same variable name with different types.

Golden Rule Always initialise variables to some value, preferably at the same time as declaration. You have multiple places which produce garbage (garbage in, garbage out)

Code is executed in the order you have it in your file, the order you have things is messed up.

It doesn't make sense to do a calculation with garbage, then ask for input for that variable, like you do on lines 21 & 22 for example. Get input with cin for each variable you need, then do the calculation.

The pi variable is neither declared nor initialised.

The ^ operator is not exponentiation, rather it is binary XOR - which is entirely different. If you want to square something just multiply as in radius * radius

Line 27 - the comma is not doing what you think, rather it should look like this:

27
28
29
if ( (rectangle <= 0) || (circle <= 0) || (sphere <= 0) || (box <= 0) ) {
   // your code here
}


An alternative to the || or operator is simply or :

27
28
29
if ( (rectangle <= 0) or (circle <= 0) or (sphere <= 0) or (box <= 0) ) {
   // your code here
}


Line 29 doesn't make sense at all.

Lines 31 & 32 cout strings must be separated by << operators , or just output 1 string literal:

cout << "The requested shape is not recognized" << "the system. Thank you\n" << endl;

or this:

1
2
cout << "The requested shape is not recognized";
cout << " by the system. Thank you\n";


or this:

cout << "The requested shape is not recognized by the system. Thank you\n";

As I said earlier, you need to review almost every aspect of your code, read up on how to do things properly - don't just do it how you might imagine it to be.


Good Luck :+)






Thank you. I am starting completely over by reading the book. I greatly appreciate your help!
Topic archived. No new replies allowed.