Can you please help me and tell me what I am doing wrong? I have been looking at the program for two hours and need help. I keep getting this error: 'triangle' was not declared in this scope. Also, that is the only error, so I don't have anymore. Thank you.

#include <iostream>

using namespace std;

int main()
{
    typedef unsigned short USHORT;

    int shape;
    cout << "What shape do you want: ";
    cin >> shape;

    USHORT shape1 = triangle, square, rectangle, circle;

    if (shape = triangle)
    {
        int triangle;
        cout << "What is the height: ";
        cin >> triangle;

        int triangle1;
        cout << "What is the length of the base: ";
        cin >> triangle1;

        int side;
        cout << "What is the length of side 1: ";
        cin >> side;

        int side1;
        cout << "What is the length of side 2: ";
        cin >> side1;

        int side2;
        cout << "What is the length of side 3: ";
        cin >> side2;

        USHORT base = triangle1, height;
        height = triangle;
        USHORT bug1 = side, bug2;
        bug2 = side1;
        USHORT bug3 = side2;

        USHORT area = 1/2 * base * height;
        USHORT perimeter = bug1 + bug2 + bug3;

        cout << "\nArea: " << area << "\n";
        cout << "Perimeter: " << perimeter << "\n";
        return 0;
    }
    if (shape = square)
    {
        int sides;
        cout << "What is the length of the sides: ";
        cin >> sides;

        USHORT width = sides, length;
        length = sides;

        USHORT area = width * length;
        USHORT perimeter = width * 4;

        cout << "\nArea: " << area << "\n";
        cout << "Perimeter: " << perimeter << "\n";
        return 0;
    }
    if (shape = rectangle)
    {
        int number;
        cout << "What is the width: ";
        cin >> number;

        int number1;
        cout << "What is the length: ";
        cin >> number1;

        USHORT width = number, length;
        length = number1;

        USHORT area = width * length;
        USHORT perimeter = (width * 2) + (length * 2);

        cout << "\nArea: " << area << "\n";
        cout << "Perimeter: " << perimeter << "\n";
        return 0;
    }
    if (shape = circle)
    {
        int radius;
        cout << "What is the radius: ";
        cin >> radius;

        USHORT half = radius;

        USHORT area = (3.14 * half) * (3.14 * half);
        USHORT circumfrence = 2 * 3.14 * half;
        USHORT volume = (4/3 * 3.14 * half) * (4/3 * 3.14 * half) * (4/3 * 3.14 * half);

        cout << "\nArea: " << area << "\n";
        cout << "Circumfrence: " << circumfrence << "\n";
        return 0;
    }
    else
        cout << "Shape not available.";
    return 0;
}

Recommended Answers

All 13 Replies

BTW, it seems to only be the first if statement that is not declared in scope. Please help. The error is on line 13.

Can you explain what Line 13 is supposed to be. The line goes like that:

USHORT shape1 = triangle, square, rectangle, circle;

What do you expect that line to mean? Because I have no clue. It is definitely not valid C++ code, but I cannot tell you how to fix it until I get a clue as to what you intended it to mean.

@ mike_2000_17

I was trying different things out and when I did that, the compiler didn't say anything so I just went along with it. BTW, thank you for replying so quickly.
If you recommend doing it differently please tell me so I may do so.

In line 13 you are assigning the value of triangle to shape1. The problem is, triangle doesn't even exist (at least not at this point) so the compiler is complaining that triangle has not been defined (i.e., brought into existance).

@ MandrewP

Well, how do you propose I fix that because it only says that for triangle and technically, I did the same thing for all the shapes and it keeps talking about the first one.

So basically:

I'm guessing by this line:

USHORT triangle, square, rectangle, circle;

You're defining variables which can then be compareable, why?

Also, your comparision is wrong, like this:

if (shape = triangle)
{
    ...
}

A single "=" indicates to the compiler that you are trying to initalise a variable with a value:

int age = 10; // I've set age to 10

So we use DOUBLE "==" in order to make a comparision of EQUAL TO.

Which brings me onto my next point, why are you using if statements for this? Ok, I guess you can, but, surely it would be better / more effective just to use switch statements? Let me give you an example:

switch(shape)
{
    case 0:

        // code for triangle

    break;

    case 1:

       // code for square
    break;

    // OTHER CASES

    default:

       cout << "Your option was unknown";
}

You're on the right track, I just think you're going about this problem the wrong way!

ALSO with the sides, why are these values not an array? Instead of writing:

int side1;
int side2;
int side3;

That's not a good programming technique!

Oh, well I purchased this book and I have been learning some of the lessons. I have finished many lessons and the last one contained if and else statements so I just tried using that. As for the "=" instead of "==", that was just a stupid mistake because I forgot about it. For the rest of the program, I haven't gotten to switches and arrays and figured that if and else statements would do just fine. Thank you for telling me. I really appreciate the help from everyone that helped me and in such little time. I will be sure to refer back to this website for things C++ related.

Ok, well at this stage you can compare the values that are inputted from the user:

int main()
{

    int shape;

    cout << "What shape do you want: ";
    cin >> shape;

    if(shape == 0)
    {
       // triangle
    }

    if(shape == 1)
    {
       // square 
    }
}

This is good:

typedef unsigned short USHORT;

More effective than using:

ushort s = FOOOO;

:) Good luck!

In fact, I am 1 chapter away from switches and 2 chapters away from arrays so THANKS!!

Thank you so much for the source code examples and telling me what to improve on. You had the best answer so far. :)

No problem - Is there anything specific that confuses you?

Well, how do you propose I fix that because it only says that for triangle and technically, I did the same thing for all the shapes and it keeps talking about the first one.

You did not do the same thing for all of the shapes, this is the same for all the shapes:

int a,b,c,d;

But here is what you did:

int a = e, b,c,d;

Here, a = e which is not the same as the others. In fact, e doesn't even exist at this point. Now look what you did:

USHORT shape1 = triangle, square, rectangle, circle;

How can shape1 be assigned the VALUE of triangle when triangle doesn't even exist yet?? Here is what that above line of code is saying: Create a type unsigned short variable and name it shape1. Then whatever value triangle holds assign that to shape1. After that, create 3 more unsigned short variables and name them square, rectangle and circle. Like I said, you are trying to assign the value in triangle to shape1 when it doesn't exist at this point in your code. That is what the compiler is complaining about.

@phorce No, thank you. That is all I needed to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.