What am I doing wrong here?

I'm just trying to do a simple program to find the sum and average. It seems right to me, but apparently I've got an error with my identifiers. Here is my program:

#include "stdafx.h"
#include <conio.h>
#include <iostream>

using namespace std;


int main()
{
int first = 2;
int second = 4;
int third = 6;
int fourth = 8;
int sum, // Sum of all values.
int average, // Average of sum of all values.

// Calculate the sum of all values.
sum = first + second + third + fourth;

// Calculate the average of all values.
average = sum / 4;

// Display the average.
cout << "The average is " << average << endl;

getche();

return 0;
}
Last edited on
Change 'int sum,' to int sum;
also, instead of integers you should use floats. So instead of int average use float average
You have commas where you should have semicolons in these lines:

1
2
int sum; // Sum of all values.
int average; // Average of sum of all values. 


Also note that you will lose any decimals in the result unless you make average a floating point number (a double).
The problem was the semicolons instead of commas. Thanks.
You will still get a wrong answer if you change first to 3, for example. The answer should be 5.25, but you wil still just get 5. You need to use a floating point variable (usually a double) to hold decimals. (You would also have to change 4 to 4.0 when calculating the average, to force the left-hand-side to be evaluated as in floating point.)
You need to change your int average to double average
so it can handle decimal values;
Topic archived. No new replies allowed.