Write a program to compute the average of all the numbers stored in a text file. Assume the numbers are integers representing test scores, for example:
70 85 90
92 57 100 88
87 98
First use the extraction operator, >>. Then use a WordStreamIterator object. Since WordStreamIterator::Current returns a string, you’ll need to convert the string to the corresponding integer; that is, the string "123" should be converted to the int 123. The function atoi in "strutils.h" in Howto G will convert the string.
int atoi(string s)
// pre: s represents an int, that is "123", "-457", etc.
// post: returns int equivalent of s
// if s isn’t properly formatted (that is "12a3")
// then 0 (zero) is returned
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here