# Need help, (Calculate statistics - Reading from a text file)

 Bawer Sultan · March 22, 2015 Ok, I have worked on this for approximately 5 hours, and I have not received the output I wanted, not even slose. The program is pretty simple, its not long at all,  here are the specs. She wants us to use a While loop only the by the way. //SPEC SHEET://1. Declare a variable to hold a counter (that will count the number of// values read from the file)//2. (NO stop value is needed. The loop stops when all values have been read// from the file)//3. Declare one variable to be used (And re-used) for the reading of each // value from the file.//4. Declare variables for the variance, the mean, and the standard deviation//5. Declare variable for two total sums: sum of values and sum of each value // squared//6. Read the values from the file one at a time, and update each of the 2 // totals//7. use both totals to calculate the variance//8. Use the first total (sum of values) to calculate the mean (mean = total// / number of values)//9. Display the variance, standard deviation and meanI am having trouble, specifically with number 1, 6, and 7. I am positive that I have a mistake in at least one of those. Number 6, is one that I dont even know how to do. Here is the first link to  the program, which also contains the specs at the bottom. http://ideone.com/369X6OHere is the link to the file which I am reading from, and it also displays what the specific output of the program should look like at the bottom. (My professor provides us with these to help us.) A few numbers may also be in different colors, but pay no mind to it. http://ideone.com/kZ6g77

## Replies

 Dol Lod · March 23, 2015 I1)  would like to add that you have undefined behaviour. sumValue is added to itself despite the fact that it is never initialized. At the start, initialize sumValue to 0. The same thing should be done with sumValueSquare. That should also be initialized to 0. 2) Additionally like Krootushas says, numValue never changes. Initialize it to 0. Then add one to it every time in the while loop. 3) Are you supposed to calculate the variance, standard deviation and mean and print them out every single time you add a number or just once at the end. I think it should just be once at the end. I did manage to create a variant of the code you put up and it mostly works. It's a bit off as var as variance goes, but everything else looks fine based on having 131 instead of 135. It just needs a few corrections here and there and it should be good. That's the good news anyway.
• 1

## C++

129,977 followers