The variable is being used without being initialized

Im using Visual Studio 2015.

When i am running my code i am able to build the file and run it however after i enter in all of my variables i get this error message "Run-Time Check Failure #3 - The variable 'cCy' is being used without being initialized."

I have the variable initialized so i am confused to why this is happening.
If anyone is able to help it would be really appreciated.


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  #include<iostream>
#include <iomanip>
#define PI 3.14159
using namespace::std;

int main() {
	void dimChecker(double*, char*);
	int countChecker(int, char*);
	double calcPercentage(double, double, double, int, double, int, double, double);

	double len, wd, ht, bRad, cyRad, cyHt, percent;
	int cb; 
	int cCy;

	cout << "Enter length of Sample: ";
	cin >> len;
	dimChecker(&len, "length of Sample");
	cout << "Enter width of Sample: ";
	cin >> wd;
	dimChecker(&wd, "width of Sample");
	cout << "Enter height of Sample: ";
	cin >> ht;
	dimChecker(&ht, "height of Sample");

	cout << "Enter radius of Bubble: ";
	cin >> bRad;
	dimChecker(&bRad, "radius of Bubble");
	cout << "Enter count of Bubble: ";
	cin >> cb;
	cb = countChecker(cb, "count of Bubble");

	cout << "Enter radius of cylinder: ";
	cin >> cyRad;
	dimChecker(&cyRad, "radius of Cylinder");
	cout << "Enter height of Cylinder: ";
	cin >> cyHt;
	dimChecker(&cyHt, "height of Cylinders");
	cout << "Enter count of Cylinder: ";
	cin >> cb;
	cb = countChecker(cb, "count of Cylinders");

	percent = calcPercentage(len, wd, ht, cb, bRad, cCy, cyRad, cyHt);


	if (percent<1)
		cout << fixed << setprecision(3);
	else if (percent<10)
		cout << fixed << setprecision(2);
	else
		cout << fixed << setprecision(1);

	cout << percent;
}

void dimChecker(double *dim, char *param) {
	while (*dim <= 0) {
		cout << "Dimention of " << param << " is not more than zero, please re enter.\n";
		cin >> *dim;
	}
}

int countChecker(int count, char *param) {
	while (count <= 0) {
		cout << "Dimention of " << param << " is not more than zero, please re enter a whole number.\n";
		cin >> count;
	}
	return count;
}


double getBubbleVolume(double radius) {
	return radius*radius*radius * 4 * PI / 3;
}

double getCylinderVolume(double radius, double height) {
	return height*radius*radius*PI;
}

double getSampleVolume(double length, double height, double width) {
	return length*height*width;
}

double calcPercentage(double length, double height, double width, int BubbleCount, double spBubbleRadius, int cylinderCount, double cyRadius, double cyHeight) {
	double getSampleVolume(double, double, double);
	double getCylinderVolume(double, double);
	double getBubbleVolume(double);

	return (BubbleCount*getBubbleVolume(spBubbleRadius) + cylinderCount*getCylinderVolume(cyRadius, cyHeight)) / getSampleVolume(length, height, width) * 100;
}
I am in my first year of c++ but I'm pretty sure its because your passing it through a function but you only have the variable initialized in main. I would declare it as a global variable.
Hopefully somebody that has more experience confirms my answer.
Wow your right for some reason i didnt think of that thank you
I have the variable initialized so i am confused to why this is happening.


No you don't. It's a Golden Rule to always initialise your variables to something. Ideally wait until you have a sensible value to assign to it, then do the declaration and assignment in one statement. Otherwise declare and assign the variable some value, noting that zero is not always a good choice; sometimes an obviously wrong value is a good idea.

Lines 7 to 9 should go before main. It's a good idea to give the parameters variable names, make them the same in declaration and the definition.

Use const where ever you can.

I like to put digits before and after the decimal place for floating point values. It can help because ot promotes the expression to double and avoid s integer division if you have it:

return radius*radius*radius * 4.0 * PI / 3.0;


Edit: Pedantically: write your code the same as the math formula. The formula is 4/3*PI *r3 , but you have r3*4*PI/3

Don't over abbreviate variable names, I would not like to have to guess what they mean.

Good Luck !!
Last edited on
test1234 wrote:
I am in my first year of c++ but I'm pretty sure its because your passing it through a function but you only have the variable initialized in main. I would declare it as a global variable.


No, neither of those.
TheIdeasMan
How am I wrong if what I told him to do worked?
@test1234

Global variables are a bad idea.

The OP did not have the variable initialised in main.

Just because something works, doesn't make it correct :+)

Maybe you all are confused between the meanings of declaration and initialising / defining ?
Topic archived. No new replies allowed.