getting inf instead of a number

This is the first program in my class, and I'm already having trouble. I am supposed to write a program that calculated the area of one of three shapes. I'm stuck on the first shape right now. The problem, I believe, is in the formula for areaOfCircle, or maybe in the types? Why won't it calculate correctly? No matter what I input for radius when prompted, the answer that comes back is 'inf.' I've been working on it for hours and tried a bunch of things. Where am I going wrong?
Thanks so much in advance.

//This is a program that calculates the area of
//a geometric shape.
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
int shape; //Declaring variable for shape to get area of
float radius; //Declaring radius of circle
float PI = 3.14159; //Declaring and setting pi.

float areaOfCircle; //Declare variable for Area of a Circle
areaOfCircle = (PI * (radius * radius));//Set variable to PI *radius squared
//Displays menu
cout << "Geometry Calculator" << endl;
cout << "1. Calculate the Area of a Cirlce" << endl;
cout << "2. Calculate the Area of a Rectangle" << endl;
cout << "3. Calculate the Area of a Triangle" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice (1-4):" << endl;

//Read choice
cin >> shape;

if (shape == 1) //If shape is equal to 1
{
cout << "What is the radius of the circle?" << endl; //Ask for radius
cin >> radius; //Read radius
cout << "The area of a circle with a radius of " <<radius <<" is " << endl;
cout << fixed << setprecision(2) << areaOfCircle; //Gives the area }
else //If shape is not = 1
{
return 0; //Quit
}
}
Last edited on
You need to have the line that calculates the area after the user enters the radius, otherwise it is trying to find out the area using a radius before it has even been defined.

So move
areaOfCircle = (PI * (radius * radius));//Set variable to PI *radius squared

To after the cin >> radius;
Once you have read in the value of radius with the cin, that is when you need to calculate the areaOfCircle. It still has the value you set it to above.
Oh my goodness! Thank you so much! FIVE hours, I've been staring at that screen changing everything under the sun except that. Ugh. Thank you very much. :) That worked. Imagine that.
One more question though: What was it using to calculate the area before I moved the line? I hadn't initialized the variable to anything, but was getting a huge long answer...what number was it using?
Correct me if I'm wrong, but I believe that the system just "makes up" a value for the variable. For example, if you initialized a variable as:

 
int y;

I think it just makes a sort of random placeholder value for it until the variable is further determined.

You must have a fancy compiler, because mine gives me an error if I try to use a variable I haven't initialized.
Last edited on
Oh, okay. I'm using CodeBlocks.
Topic archived. No new replies allowed.