Reading from a file

Im fairly new to C++ and am currently in class. The homework problem i've been given says to download an instructor provided txt doc with 100 numbers. I have to write a program that will count the numbers, sum them and average them. Below is my code. I thought this would work but the numbers are all screwy. Can someone explain what I did wrong? thanks

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
ifstream inFile;
int num;
int sum = 0;
int avg;"Random.txt");

while(inFile >> num)

{for(int count=0; count <=num; count++) //this is so everytime a number is read in count increments

sum += num; //to keep your running total


avg = sum/num;

cout <<"count is " <<num <<endl;
cout <<"sum is " <<sum <<endl;
cout <<"average is " <<avg <<endl;

return 0;
That for loop is going to add num into your sum num times, when you only should be doing it once for each number. You shouldn't really be looping on count at all; just use that to record the total amount of numbers you get and use it to calculate the average.
Topic archived. No new replies allowed.