2

I was instructed to design an algorithm that would allow the user to calculate the perimeter and area of a rectangle, but that's too easy:

Perimeter = 2 (Length + Width)
Area = (Length * Width)

I wanted to build a program using a similar construction to one my professor showed us on the first day so that I could have a program collect data from the user, calculate the perimeter and area of the rectangle for me, and then output the answer back to the user. That was easy enough, so I decided I wanted the program to also tell me whether or not the length and width that the user entered was the length and width of a rectangle or a square.

With the code I wrote, the program always returns that the length and width entered are the length and width of a square. I am not sure where I went wrong:

Here is my code:

#include<iostream>
using namespace std;

int main() {

    //Declaration
    int length;
    int width;
    int perimeter;
    int area;

    //Collect Data
    cout << "Let's calculate the perimeter and area of a rectangle\n";
    cout << "What is the length of the rectangle?: ";
    cin >> (length);

    cout << "What is the width of the rectangle?: ";
    cin >> (width);

    //Calculation
    (perimeter) = 2 * (length + width);
    (area) = (length) * (width);

    //Output Data
    cout << "The perimeter of the rectangle is: " << (perimeter) << "\n";
    cout << "The area of the rectangle is: " << (area) << "\n";

    //For some reason, the code is not able to recognize what I have designed.
    //No matter what input for length and width, when the program executes it returns that I entered the length and width of a square.
    if ((length) = (width))
        cout << "Hey! You entered the length and width of a Square!\n";
    else
        cout << "You entered the length and width of a Rectangle!\n";

    system("pause");
    return 0;

}

Any help would be greatly appreciated! Thank you so much!

Ken White
  • 123,280
  • 14
  • 225
  • 444
Matthew
  • 21
  • 1
  • 2
    As a side note, kudos for going above and beyond the assignment and exploring on your own. That habit will definitely accelerate your learning! – user94559 Jun 30 '17 at 02:38
  • BTW, you don't need the parentheses around all your variables. E.g. `area = length * width;` – user94559 Jun 30 '17 at 02:41
  • I like how you put () around the variables even though you don't have to I think it helps you understand the program better. [What is the purpose of a declaration like int (x); or int (x) = 10;](https://stackoverflow.com/questions/26832321/what-is-the-purpose-of-a-declaration-like-int-x-or-int-x-10) – Manvir Jun 30 '17 at 02:45
  • = is for assignment , == is for comparison. check your operators – Kim Clarence Penaflor Jun 30 '17 at 02:45
  • Your compiler should have given you a warning about using assignment where it's ordinarily out of place; namely in the `if` condition. Let this be a lesson to _always_ take note of and ***fix*** compiler hints & warnings. – Disillusioned Jun 30 '17 at 03:34

3 Answers3

6

You want a double equals:

if (length == width)

A single equals (=) performs assignment, double equals (==) performs a comparison.

user94559
  • 59,196
  • 6
  • 103
  • 103
  • Thank you so much... I was literally sitting there looking at the code for 10 minutes trying to figure out where I went wrong. That makes sense considering >= and <= are chosen to represent greater than or equal to or less than or equal to. It works correctly now! – Matthew Jun 30 '17 at 02:40
3

In your if-statement you are assigning width to length, not comparing.

It should be: if (length == width)

The reason why your if-statement evaluates to true is because it checks whether the result of the evaluation (which is the value of length) is different from zero.

That's because a bool is internally represented as an int, where false is defined as 0 and true else.

And since width is usually not 0 it would always result in true. You can test it out by entering 0 for width.

Shiro
  • 2,610
  • 2
  • 20
  • 36
1

One thing I would recommend is looking into using a debugger to step through each line of your code as it runs one at a time. If you are using VS you have one built right in!

I think if you used a debugger you would have noticed that when the if was running it was doing the assignment rather than the comparison. Just a thought for the future.

I'm sure everyone can say they have caught many silly bugs with a debugger. Good job :)

GavinHenderson5
  • 33
  • 2
  • 11