If Statements

Hey,

I am trying to get the smallest and biggest number out of three numbers using if statements. My problem is that I keep getting an error that says I did not initialize the variables...

Error C4700 uninitialized local variable 'b' used
Error C4700 uninitialized local variable 'c' used

I am using visual studio

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

int main()
{

	double a, b, c;
	cout << "Please enter three numbers \n";
	cin  >> a, b, c;

	if ((a > b) && (a > c))
	{
		cout << a << " is the largest \n";
		if (b > c)
		{
			cout << c << " is the smallest";
	    }
		if (c > b)
		{
			cout << b << " is the smallest";
		}
    }
	
	if ((b > c) && (b > a))
	{
		cout << b << " is the largest \n";
		if (a > c)
		{
			cout << c << " is the smallest";
		}
		if (c > a)
		{
			cout << a << " is the smallest";
		}
	}

	if ((c > b) && (c > a))
	{
		cout << c << " is the largest \n";
		if (b > a)
		{
			cout << a << " is the smallest";
		}
		if (a > b)
		{
			cout << b << " is the smallest";
		}
	}

	return 0;

}
Last edited on
closed account (E0p9LyTq)
Line 9, cin >> a, b, c; should be cin >> a >> b >> c;

It could be rewritten as:
1
2
3
cin >> a;
cin >> b;
cin >> c;
Hello mclogical,

As I have read here many times always initialize your variables. Line 7
double a, b, c;would be better written:
1
2
3
4
5
double a = 0, b = 0, c = 0;
or
double a(0), b(0), c(0);
or
double a{ 0 }, b{ 0 }, c{ 0 };

any one of the three will work.

Andy
Last edited on
Topic archived. No new replies allowed.