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

Gah, trying to write functions for the first time. The objective is the prompt the user to enter three different integers until they're all different and then display the maximum and minimum number. Is the error coming up b/c of the while loop and stuff?


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
#include <iostream>
using namespace std;

bool isDifferent(int x, int y, int z)
{
	if (!(x == y == z))
		return true;

	return false;

}

int max_of_three(int x, int y, int z)
{
	int greater_of_two(int, int);
	int first_comparison = greater_of_two(x, y);
	int second_comparison = greater_of_two(first_comparison, z);
	return second_comparison;
}

int greater_of_two(int first_number, int second_number)
{
	if (first_number > second_number)
		return first_number;
	return second_number;
}

int min_of_three(int x, int y, int z)
{
	int smaller_of_two(int x, int y);
	int first_comparison = smaller_of_two(x, y);
	int second_comparison = smaller_of_two(first_comparison, z);
	return second_comparison;
}

int smaller_of_two(int first_number, int second_number)
{
	if (first_number < second_number)
		return first_number;
	return second_number;
}

void print_result(int max, int min)
{
	cout << "The maximum of the three numbers is " << max << ", and the minimum of the three numbers is " << min_of_three << endl;
}


int main()
{

	bool isDifferent(int, int, int);
	int max_of_three(int, int, int);
	int min_of_three(int, int, int);
	void print_result(int, int);
	int a, b, c;

	while (isDifferent == false)
	{

		cout << "Please enter an integer" << endl;
		cin >> a;
		cout << "Please enter an integer" << endl;
		cin >> b;
		cout << "Please enter an integer" << endl;
		cin >> c;
	}

	int max = max_of_three(a, b, c);

	int min = min_of_three(a, b, c);

	print_result(max, min);

	system("pause");
	return 0;
}

line 45 and line 58. You're using function names without () or parameters: min_of_three and isDifferent.
Last edited on
closed account (EwCjE3v7)
You have a few problems, I fixed them and have commented on those lines.

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
#include <iostream>
using namespace std;

bool isDifferent(int x, int y, int z)
{
	if (x != y && x != z && y != z) // changed
		return true;

	return false;

}

int max_of_three(int x, int y, int z)
{
	int greater_of_two(int, int);
	int first_comparison = greater_of_two(x, y);
	int second_comparison = greater_of_two(first_comparison, z);
	return second_comparison;
}

int greater_of_two(int first_number, int second_number)
{
	if (first_number > second_number)
		return first_number;
	return second_number;
}

int min_of_three(int x, int y, int z)
{
	int smaller_of_two(int x, int y);
	int first_comparison = smaller_of_two(x, y);
	int second_comparison = smaller_of_two(first_comparison, z);
	return second_comparison;
}

int smaller_of_two(int first_number, int second_number)
{
	if (first_number < second_number)
		return first_number;
	return second_number;
}

void print_result(int max, int min)
{
	cout << "The maximum of the three numbers is " << max << ", and the minimum of the three numbers is " << min << endl; // should be min not min_if_three
}


int main()
{
	int a = 0, b = 0, c = 0; // You should initialize

     // can't call functions without the call operator and the right amount of arguments

    while (!isDifferent(a, b, c)) { // while we dont have three different numbers
	cout << "Enter three different integers:" << endl;
	cin >> a >> b >> c;
    }
    
    if (isDifferent(a, b, c)) { // make sure there are three different numbers
        int max = max_of_three(a, b, c);

        int min = min_of_three(a, b, c);

    	print_result(max, min);
    }

	system("pause");
	return 0;
}

Last edited on
@Fr0zen1

Thank you for your corrections. I have some errors now though. I get Error C4700 on line 58 for the parameters of isDifferent

** never mind I fixed it by changing it to a do while loop! But my boolean is not working, when two of the numbers are the same, it doesn't continue to prompt for imput
Last edited on
What's up with line 15?
closed account (EwCjE3v7)
Mm everything seems to be working with the modified code above.

What compiler are you using? And version
Last edited on
The variable 'c' is being used without being initialized.


There is a golden rule about this: Always initialise your variables to something when you declare them.

Note that 0 or empty string is not always a good idea. Best to choose something that is going to result in an obvious & large wrong value, if the value is not changed from it's initial value. This is better than dealing with garbage values.

If you have compiler errors, then post all of them in full - we want to see what the compiler says, not your interpretation :+)

While I am at it, do have the compiler warnings set to their highest level? It's a bit of shit fight to find out where they are set in VS, but if you can figure it out it's worth it.

Line 2: is a problem, it brings in the entire std namespace which is huge, which leads to naming conflicts - which is exactly what you have. I am not going to tell where the conflict is, instead I suggest you get rid of line 2 and put std:: before each std thing - just use find & replace. If you look at code posted by some of the expert members ( Cubbi , JLBorges and many others say) you can see that they always do this.

The include file is not there for this to actually be a problem, but the compiler might (should IMO) issue a warning about not be able to find the offending item. There is a whole list of names in the STL that might be unwittingly used by a beginner, and cause a conflict. On top of that, the compiler errors can be hard to understand, because the internals of the STL can be complicated.

Even better, read up about what a namespace is, and realise that you should put your own stuff in it's own namespace.

I learnt all this from ne555 (bless his cotton socks :+D )a long time ago.

Hope all this added to your education a bit :+)
line 15 looks like a prototype inside another function. it should be moved out of there. It may be valid code, but it's just bad form.
closed account (EwCjE3v7)
@Esslercuffi I think he might be practicing his functions.

Thanks everyone, it works now I can't believe this!!

@Esslercuffi, when I move the prototypes out of the functions into main, for some reason complier says the function is not identified, so that's why I have it the way it is. I will ask my teacher about that as well though.
prototypes are usually placed at the top of a program before any functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int greater_of_two(int, int);

bool isDifferent(int x, int y, int z)
{
	if (!(x == y == z))
		return true;

	return false;

}

int max_of_three(int x, int y, int z)
{
	int first_comparison = greater_of_two(x, y);
	int second_comparison = greater_of_two(first_comparison, z);
	return second_comparison;
}
I agree, the normal order basically is: function prototypes, main(), function definitions. The reason for this is that someone reading the code doesn't want to go looking for main() - it should be in a standard place. And get rid of line 30 there is no need for it as you have already defined your function .

Has any one figured out where the naming conflict is, or indeed taken out the using statement on line 2, so there is no naming conflict?

lavender swann wrote:
Thanks everyone, it works now I can't believe this!!


Sorry to be boring, but this doesn't mean it couldn't be improved :+)

Always use braces whenever there is potential for a compound statement (More than 1 in a block), even if there is only 1 statement now - like on line 24. This will save you one day when you add more code.



One can split long std::cout statements up over several lines (so the code doesn't march off the right side of the screen) like this:

43
44
45
46
47
48
49
50
void print_result(int max, int min) //naming conflict ?
{
	std::cout << "The maximum of the three numbers is " << max;
        std::cout  << ", and the minimum of the three numbers is " << min;
        std::cout  << std::endl; // should be min not min_if_three
        // one can use \n rather than std::endl as in:
        // std::cout << min << "\n";
}


And put comments before the pertinent code, not at the end of the line.

I am relating all this so you can improve your coding style & may be get better marks as well :+D

Hope all is well.
Also, having the prototype inside a function makes the prototype only visible to that function. This isn't a problem in this case, since max_of_three() is the only function which calls greater_of_two(). But in a general sense, this type of thing can cause headaches if someone decided to move function definitions around to reorganize the code.

I'm working on an article about declarations, prototypes, and definitions since I've seen multiple posts recently where people have demonstrated some confusion about what they are and why they're needed.
Last edited on
Topic archived. No new replies allowed.