run-time check failure #3 - the variable 'num 3' is being used without being initialized.

I keep getting "run-time check failure #3 - the variable 'num 3' is being used without being initialized." whenever I try to debug or run this. If I declare a value for num3 then void func2() will carry out but 'c' wont carry over to num3. If I were to switch take line 26-28:
cout << "Please enter two integers: ";
cin >> num1 >> num2;
cout << endl;
And place it inside of the void func2(), it would do the same thing for num1 and num2 assuming num3 had a value declared for it leading me back to the problem of a and b not carrying back over into the main function from void func2(). 90% of this code is copied straight from the book for this problem but the code that's messed up, assuming it's not mine, is the books. The only portion of this code that I wrote was the definition for "void func2(int a, int b, double c)" and the code inside the brackets for it as well. Any help would be greatly appreciated. I've tried fiddling with it but can't wrap my brain around this one to save my life. I hope this makes sense to someone out there with my explanation. I'm using Visual Studio 2012.
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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void func1();
void func2(int a, int b, double c);

int main()
{
	int num1, num2;
	int choice;
	double num3;

	cout << fixed << showpoint << setprecision(2);

	do
	{
		func1();
		cin >> choice;
		cout << endl;

		if (choice == 1)
		{
			cout << "Please enter two integers: ";
			cin >> num1 >> num2;
			cout << endl;

			func2(num1, num2, num3);
			cout << num1 << ", " << num2 << ", " << num3 << endl;
		}

	}
	while ( choice != 99);

	return 0;
}

void func1()
{
	cout << "To run the program, enter 1." << endl;
	cout << "To exit the program, enter 99." << endl;
	cout << "Enter 1 or 99. ";
}

void func2(int a, int b, double c)
{
	if (a != 0 && b != 0)
	{
		if (a >=b)
			c = pow(a, b);
		else 
			c = pow(b, a);
	}
	else if (a != 0 && b == 0)
		c = sqrt(abs(a));
	else if (a == 0 && b != 0)
		c = sqrt(abs(b));
	else 
		c = 0;
}
func2 takes 'c' by value, meaning that c is a COPY of the variable you pass in, and is not the variable itself.

If you change 'c' in func2, it will not change 'num3' in main because it's a different variable.


You probably intended to pass it as a reference:

 
void func2(int a, int b, double& c) // <- note the & symbol 


By passing it as a reference, 'c' is no longer a copy, but actually is the variable being passed in.

But a better approach would be to return the number, rather than pass by value:

1
2
3
4
5
6
7
8
9
10
11
12
13
double func2(int a, int b) // <- only take input as params, return the output
{
	if (a != 0 && b != 0)
	{
		if (a >=b)
			return pow(a, b); // <- return output, rather than assign it to 'c'
		else 
			return pow(b, a);
	}
...


num3 = func2( num1, num2 ); // <- call the function like this, assign the returned value to num3 






Lastly... your names are terrible. 'func1', 'func2', 'num1', 'a', etc are not at all descriptive. You should pick names which identify the functions/variables by purpose. It will go a long way in improving code clarity.
Thank you Disch for the quick reply and help. I was able to fill in the gaps with what you told me and got it to work. And I didn't come up with the names, this is all for my homework assignment and it's all directly from the book. I have to turn over my .cpp file to my teacher and so for uniformity sake, I can't change them. Literally the only part of the code that I wasn't given for this exercise was
int a, int b, double c
inside of void func2()
and
1
2
3
4
5
6
7
8
9
10
11
12
13
if (a != 0 && b != 0)
	{
		if (a >=b)
			c = pow(a, b);
		else 
			c = pow(b, a);
	}
	else if (a != 0 && b == 0)
		c = sqrt(abs(a));
	else if (a == 0 && b != 0)
		c = sqrt(abs(b));
	else 
		c = 0;

These are the only piece of the code that I wrote base off what the assignment asked for. It was more a less fill in the blank problem with derpy code to begin with I guess.
*sigh* I'm so disappointed with how programming (and specifically C++) is taught these days.


Anyway I'm glad you got it working. =)
Topic archived. No new replies allowed.