Initalization problem..if else contruct

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;
}

return 0;
}

Last edited on
int square = 0; // or whatever value you want it to have
yeah but then i'll have to input a number when prompted for the shape? i want to input either 'square' or 'circle'.
Are you trying to find the word square? or are you expecting to get the number of sides? square needs to be in one form or the other.
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.


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
37
38
39
40
41
42
43
44
45
46
47
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; 
}
Last edited on
yes, your braces are off.

It helps to notice these things if you indent properly.

Here is what you are doing with proper indentation. Hopefully you can see how to fix it:

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

    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; 
}
Topic archived. No new replies allowed.