Help with averaging and conditions.

Hello there,i am having a bit of trouble here, i am making a program that will average 3 test scores. the scores should be between 0 and 100 and if it does not meet that requirement it will say "invalid score". This is what i got, but i am not sure what is wrong with it. also i have no idea how to get decimal points on the average, need to have 1 number in the decimal area. Thank you.


#include <iostream>
using namespace std;

int main()
{
int score1, score2, score3, sum;
double average;

cout << "Enter your three test scores: " << endl;

cin >> score1;
cin.get();
cin >> score2;
cin.get();
cin >> score3;
cin.get();

sum = score1 + score2 + score3;
average = sum / 3;

if ((score1 > 100) && (score1 < 0));
{
cout << "Invalid score.\n";
}
if ((score2 > 100) && (score2 < 0));
{
cout << "Invalid score.\n";
}
if ((score3 > 100) && (score3 < 0));
{
cout << "Invalid score.\n";
}
else
{
cout << "Your total average: " << average << endl;
}

sum = score1 + score2 + score3;
average = sum / 3;

return 0;
}
dont put ; at the end of if and use else if as below


int main()
{
int score1, score2, score3, sum;
double average;

cout << "Enter your three test scores: " << endl;

cin >> score1;
cin.get();
cin >> score2;
cin.get();
cin >> score3;
cin.get();

sum = score1 + score2 + score3;
average = sum / 3;

if ((score1 > 100) && (score1 < 0))
{
cout << "Invalid score.\n";
}
else if ((score2 > 100) && (score2 < 0))
{
cout << "Invalid score.\n";
}
else if ((score3 > 100) && (score3 < 0))
{
cout << "Invalid score.\n";
}
else
{
sum = score1 + score2 + score3;
average = sum / 3;
cout << "Your total average: " << average << endl;
sum = score1 + score2 + score3;
average = sum / 3;
}



return 0;
}

IT would be better to make a function that prompts for a number inside a loop that checks for out-of-bounds error.
if ((score1 > 100) && (score1 < 0))
What number is both greater than 100 and less than 0?
average = sum / 3;
Although you've correctly chosen a float instead of an int to store average, sum/3 is computed using integer arithmetic, so it gets truncated. To fix this, change one argument to float or double. The easiest way to do that is just to add a ".0" after 3:
average = sum / 3.0;
Topic archived. No new replies allowed.