Trying to find average.

I don't understand what I am doing wrong. I'm trying to find the average of three numbers. When I run the program and I enter score 1 there is a 0 in front already, for score 2 a random number shows up 4883904, and for score 3 there is a 0 in front again. I can enter scores, so I entered 100 for all 3 and the avg. shows up correctly, but I don't know where the random number and 0 are coming from.

Here is a pic: http://imgur.com/a/2KekZ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  // Find average of three numbers 
#include <iostream>
using namespace std;
main()
	{ 
		int s1,s2,s3,t;
		float a;
		cout <<"\nEnter score 1 :" <<s1;
		cin >>s1;
		cout <<"\nEnter score 2 :" <<s2;
		cin >>s2;
		cout <<"\nEnter score 3 :" <<s3;
		cin >>s3;
		t=s1+s2+s3;
		a=t/3.0;
		cout <<"\nScore 1:" <<s1;
		cout <<"\nScore 2:" <<s2;
		cout <<"\nScore 3:" <<s3;
		cout <<"\nTotal:" <<t;
		cout <<"\nAverage:" <<a;
		system("pause");
		return 0;
	}



Last edited on
Line 8, 10, and 12: You are printing out the scores before you enter them. So the values showing up are garbage.

Line 16, 17, and 18 are good because they are printing out the numbers AFTER you've entered them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  // Find average of three numbers 
#include <iostream>
using namespace std;
main()
	{ 
		int s1,s2,s3,t;
		float a;
		cout <<"\nEnter score 1 : "
		cin >>s1;
		cout <<"\nEnter score 2 : " 
		cin >>s2;
		cout <<"\nEnter score 3 : "
		cin >>s3;
		t=s1+s2+s3;
		a=t/3.0;
		cout <<"\nScore 1:" <<s1;
		cout <<"\nScore 2:" <<s2;
		cout <<"\nScore 3:" <<s3;
		cout <<"\nTotal:" <<t;
		cout <<"\nAverage:" <<a;
		system("pause");
		return 0;
	}
Last edited on
This line:
cout <<"\nEnter score 1 :" <<s1;
is printing "\nEnter score 1 :" on a screen, and after this a value of the s1 variable. You did not assign the value to the s1 yet, that is why it contains something that we call garbage.
Oh!! okay I see what I was doing wrong. Thanks for the help.
Topic archived. No new replies allowed.