My code gives a mistake after i run it.

I got a small task to do and i wrote some code -as far as i learned till now - it should work but it shows an error when i start the program.I use the Visual Studio Team System 2008.

Here is the task :
Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialog should appear as follows:


Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27

And here is my code:

//Thank you for your help!!!
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

#include <iostream>
using std::cin;
using std::cout;


int main()
{
	
	int a ,b ,c;
	int Sum ,Average ,Product ,Smallest ;
	int Largest ;
	
	cout << "Input three different integers: ";
	cin >> a >> b >> c;

	Sum = a + b + c;
	Average = Sum / 3;
	Product = a * b * c;
	
	if (a < b)
		if(a < c)
			 Smallest = a;
		else 
			if(b < c)
				Smallest = b;
			else 
				Smallest = c;
	if (a > b)
		if (a > c)
			Largest = a;
		else 
			if (b > c)
				Largest = b;
			else 
				Largest = c;

	cout << "Sum is " << Sum << "\n";
	cout << "Average is " << Average << "\n";
	cout << "Product is " << Product << "\n";
	cout << "Smallest is " << Smallest << "\n";
	cout << "Largest is " << Largest ;
	
	
	

	return 0;
}

The error it shows is :


Run-Time Check Failure #3 - The variable 'Largest' is being used without
being initialized.
(Press Retry to debug the application)

Last edited on
Hm, that's odd. Try assigning a value to Largest then...like Largest = 0 or something.
i gave Largest a zero

 
int Largest = 0;


But then it shows in the program

Largest = 0


The result from the original code(the one posted in the begining) is :

Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is -858993460Press any key . . .(this is in run without debug mode)


Just before it shows the "Largest ... " it says there was a mistake :


Run-Time Check Failure #3 - The variable 'Largest' is being used without
being initialized.
(Press Retry to debug the application)
Last edited on
if a <= b, then you never set Largest to anything. That whole if/else tree depends on a > b

just like you never set Smallest to anything if a >= b
Last edited on
So how should i fix it to work normally?
I would appreciate any help and solution.
Thanks for the post.
Your if/else stuff is quite complicated and hard to follow, which leads to errors like this.

KISS -- keep the logic as simple as possible. I'd just do something like this:
1
2
3
Smallest = a;
if(b < Smallest)   Smallest = b;
if(c < Smallest)   Smallest = c;


Similar for largest.
I found my own mistake - thank you Disch

It was in the
 
if (a > b)


it should be else // to the first if i place at line 21

Thanks A Lot - Consider it solved


By the way your way is better and easier to follow -I `ve just learned something new and useful.
Thanks again
Last edited on
Topic archived. No new replies allowed.