On November 19 2010 02:39 Marahumm wrote:
EDIT: Seems I don't look at forum names until AFTER I posted.
You didn't comment your code one bit! Commenting your code is essential when you are trying to figure out what exactly you want your code to do. It's especially necessary when you want others to take a look at it so they don't have to wrack their minds over what exactly you're doing.
To clean up your code you can definitely initialize more functions so that your main function isn't so complicated:
+ Show Spoiler +
void ReadFile(string sFile); // reads in a file and passes all integers to a vector
int LowestScore(); // loops through all scores and returns the lowest one
int HighestScore(); // loops through all scores and returns the highest one
int ScoreCount(); // returns the number of scores in the vector
float AverageScore(); // returns the average score from all elements in the vector
This way when you're debugging you won't have to scroll through ALL of your code to try and find out where your problem is. Each task has it's own function, that way if you run across a problem with, say, the average score, you can skip right to the AverageScore() function and figure out what's wrong.
EDIT: Seems I don't look at forum names until AFTER I posted.
You didn't comment your code one bit! Commenting your code is essential when you are trying to figure out what exactly you want your code to do. It's especially necessary when you want others to take a look at it so they don't have to wrack their minds over what exactly you're doing.
To clean up your code you can definitely initialize more functions so that your main function isn't so complicated:
+ Show Spoiler +
void ReadFile(string sFile); // reads in a file and passes all integers to a vector
int LowestScore(); // loops through all scores and returns the lowest one
int HighestScore(); // loops through all scores and returns the highest one
int ScoreCount(); // returns the number of scores in the vector
float AverageScore(); // returns the average score from all elements in the vector
This way when you're debugging you won't have to scroll through ALL of your code to try and find out where your problem is. Each task has it's own function, that way if you run across a problem with, say, the average score, you can skip right to the AverageScore() function and figure out what's wrong.
I'm going to stab you.
while(!file.EOF())
{
int a = line;
if(a > max) max = a;
if(a < min) min = a;
sum += a;
total++;
}
That's all you need for the loop (roughly). The file IO is more important to learn on your own. With (i)fstream you'll need to use atoi to convert string -> int. Good luck.