Hey all, I'm trying to make a basic if else program. I keep getting a "Run-Time Check Failure #3 - The variable 'square' is being used without being initialized" error. How do I initialize 'square'?
#include <iostream>
int main()
{
int shape;
int pi = 22/7;
int square;
int lengthOfSide;
int areaOfSquare;
int circle;
int radiusOfCircle;
int areaOfCircle;
std::cout << "Please state if finding area of square of circle: ";
std::cin >> shape;
if (shape == square)
{
std::cout << "Please enter length of side:" << std::endl;
std::cin >> lengthOfSide;
areaOfSquare = lengthOfSide * lengthOfSide;
std::cout << areaOfSquare << "! This is the area of the square" << std::endl;
}else{
std::cout << "Please enter radius of circle:" <<std::endl;
std::cin >> radiusOfCircle;
areaOfCircle = pi * radiusOfCircle * radiusOfCircle;
std::cout << areaOfCircle << "! This is the area of the circle" << std::endl;
}
No, I was trying to type in either 'square' or 'circle' after being asked which shape I was finding the area for. But i now realized you can only input numbers?
Then you have a few problems, one, you cant ask for 'square' or 'circle' and store that value in an int. Unless you are saying 1 = square or 2 = circle (or whatever number represents). Make shape a std::string, check to see if shape is a "square" or "circle", I think this is what you are trying to do.
You can only input numbers because you have declared shape as an integer.
Ok thanks. Well we haven't reached strings yet but I understand what you're saying. This will have to do for now. I have a new problem though:
I want to first determine the shape (square, circle or rectangle) and then find the area of that shape. I'm thinking of using nested If Else loops, but I can't seem to get it to work? Are my braces off? It keeps telling me invalid Else.
int shape;
int pi = 22/7; //declaring variables
int square = 1;
int lengthOfSide;
int areaOfSquare;
int circle = 2;
int radiusOfCircle;
int areaOfCircle;
int rectangle = 3;
int length;
int breadth;
int areaOfRectangle;
std::cout << "Please state if finding area of square of circle: ";
std::cin >> shape;
if (shape == square) //if area of square
{
std::cout << "Please enter length of side:" << std::endl;
std::cin >> lengthOfSide;
areaOfSquare = lengthOfSide * lengthOfSide;
std::cout << areaOfSquare << "! This is the area of the square" << std::endl;
}
else
{
if (shape == 2) //if area of circle
std::cout << "Please enter radius of circle:" <<std::endl;
std::cin >> radiusOfCircle;
areaOfCircle = pi * radiusOfCircle * radiusOfCircle;
std::cout << areaOfCircle << "! This is the area of the circle" << std::endl;
else //if area of rectangle
std::cout <<"enter length"<<std::endl;
std::cin >> length;
std::cout <<"enter breadth"<<std::endl;
std::cin >> breadth;
areaOfRectangle = length * breadth;
std::cout << areaOfRectangle << "! AREA OF RECTANGLE" << std::endl;
}
return 0;
}
if (shape == square)
{
std::cout << "Please enter length of side:" << std::endl;
std::cin >> lengthOfSide;
areaOfSquare = lengthOfSide * lengthOfSide;
std::cout << areaOfSquare << "! This is the area of the square" << std::endl;
}
else
{
if (shape == 2)
std::cout << "Please enter radius of circle:" <<std::endl;
std::cin >> radiusOfCircle;
areaOfCircle = pi * radiusOfCircle * radiusOfCircle;
std::cout << areaOfCircle << "! This is the area of the circle" << std::endl;
else // there is not an if statement immediately before this else.
std::cout <<"enter length"<<std::endl;
std::cin >> length;
std::cout <<"enter breadth"<<std::endl;
std::cin >> breadth;
areaOfRectangle = length * breadth;
std::cout << areaOfRectangle << "! AREA OF RECTANGLE" << std::endl;
}
return 0;
}