Need help with SIMPLE program for my class

I'm just now learning C++, and this simple program is driving me nuts! It's a program that prompts the user to enter the radius of a circle and outputs the area and circumference. I thought I did everything correctly, but I get this error message:

"Run-Time Check Failure #3 - The variable 'radius' is being used without being initialized."


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include <iostream>
using namespace std;

const double PI = 3.14;
	int main()
{
	double radius;
	double area;
	double circumference;
	area = PI * radius * radius;
	circumference = 2 * PI * radius;

	cout << "Enter the radius: ";
	cin >> radius;
	cout << endl;

	
	cout << "Area = " << area << endl;
	cout << "Circumference = " << circumference << endl;
		

	return 0;
}
Last edited on
You are calculating the area before the user has given a value to radius.
Like Peter87 said, you're trying to use a variable in your area calculation before you establish what radius you're using. When you say "double radius;" you're allocating a space in memory for a double named "radius", but it doesn't have a value yet, just random garbage values. Now go down to line 11 in the area calculation, you're multiplying PI (which you set to 3.14 earlier) by radius, only problem is, you never gave radius a value!

Make sure the user enters a value before you use variable. A good general rule is to always initialize variables to something (like 0) before using them because using uninitialized variables can lead to some big problems. So set them equal to some base value, then have change the value by having the user input the new value. By doing this, if something goes wrong, your variables automatically default to a valid value before using them.
Thanks guys, I got it done. This site is very helpful!
Last edited on
Topic archived. No new replies allowed.