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!