I'm battling to do the parameters of the input data 0 > score < 100 and then to display that the entry was wrong. Could someone please point me in the right direction
1) Please use code tags when posting code, to make it readable.
2) You have the following:
s1,s2,s3,s4,s5=score;
What are you trying to do here? Do you understand what the comma operator actually does in C++?
If you're trying to set all five variables to the same value, you need:
s1 = s2 = s3 = s4 = s5 = score;
I don't understand why you'd want to do that, though, because you'd be overwriting the values that the user has just entered for those 5 variables.
3) I really, really recommend you get out of the habit of using global variables. They will cause you all sorts if problems down the line. Declare variables where they're needed, and pass them to functions that need them.
4) Rather than using five different variables called s1, s2, s3, s4, s5, you'd be better off using an array.
(Edited, because I was confusing you with another user. My apologies.)
#include <iostream>
#include <iomanip>
usingnamespace std;
int getScore (int n);
void get_scores (int s[]);
void displayOutput (float avg);
float calcAverage(int s[]);
constint NUM_SCORES = 5;
// No global vbariables
int main()
{ int s[NUM_SCORES];
float average;
get_scores(s); // Get all 5 scores
average = calcAverage(s);
displayOutput(average);
}
// Prompt for a single score with edit checking
int getScore(int n)
{ int score;
while (true)
{ cout << "Enter test score #" << n << ": ";
cin >> score;
if (score >= 0 && score <= 100)
return score; // Good score
cout << "Enter a test score between 0 and 100: ";
}
}
// Added function to get all 5 scores
void get_scores (int s[])
{ for (int i=0; i<NUM_SCORES; i++)
s[i] = getScore (i+1);
}
int findLowest(int s[])
{ int lowest = s[0];
for (int i=0; i<NUM_SCORES; i++)
if (s[i] < lowest)
lowest = s[i];
return lowest;
}
float calcAverage(int s[])
{ float sum = 0;
int lowest;
float averageScore;
lowest = findLowest (s);
for (int i=0; i<NUM_SCORES; i++)
sum += s[i];
sum -= lowest;
averageScore = sum / 4;
return averageScore;
}
void displayOutput(float avg)
{ cout << endl;
cout << "The average is: " << avg << endl;
}
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
EDIT - you're using global variables, instead of passing and returning parameters. If you want to use reference variables, great - stick them in MAIN. Drives me buggy to see someone using globals when they don't need to.
I'm sure someone will come up and play Devil's Advocate, but it's just plain lazy coding.
Try changing your while statement to:
1 2 3 4 5 6 7 8 9
cout << "Enter test score : ";
while ((!cin >> score) || (score<0)||(score>100)) {
cin.sync();
cin.clear();
cout << "Please enter a valid test score that between 0 to 100\n";
}
In other words, as long as they didn't enter an integer, or score <0 or score>100, clear any error flag, flush the iostream, and ask them to put it in in again..
as for formatting decimals - how did you get to reference variables, yet not learn how to use iomanip, setprecision, or showpoint?
Ive been doing C++ for two weeks. How did you do after your first two weeks? I asked for assistance not a lecture on how pathetic i'm doing. If you look at my first attempt all done by myself you will see that its about the level I'm at. I do not understand reference variables but adapted it from another program. I will understand all you mentioning soonest I hope. You could have just said look at how to do showpoint. We all learn differently. I never read manuals when I get a new device ..I play and read when I get stuck. We all have different ways of approaching something. Sorry for intruding on your time and space
I never read manuals when I get a new device ..I play and read when I get stuck. We all have different ways of approaching something.
That can work for a lot of things. With C++, it's unlikely to be a helpful approach. Just muddling through like that, you're going to end up with a lot of misunderstandings, and picking up some very bad habits. It's a language based on some subtle, and at times complicated, rules and principles, and you'll do yourself a big favour by learning properly, from the ground up, from a good book (or, perhaps, a set of tutorials).
Not a criticism, or a lecture - just (I hope) some helpful advice.