Reading in Data from a file, printing it then calculating the sum/ave/sc

Hi, I am vvv new to C++ and doing a course on it. The first assignment i have is to reading in and print out numbers from a file, then calculate the avearage of the numbers, the sum and the standard deviation.
Ive written two separate codes so far, one to print out the numbers and another to calc the sum and the average (not tackled sd yet). But when i put them together, the program only prints out the numbers and im not sure where im going wrong:

[code]

include <iostream>
#include <stdio.h>
#include <cmath>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
int n;
int Points[99] = {0};
cout << "Enter a file name" << endl;
string name;
cin >> name;
ifstream input(name);
if(input.is_open())
{
while(! input.eof())
{
getline (input,name);
stringstream convert(name);

if (!(convert >> Points[n]))
{
Points[n]=0;
}
cout <<Points[n]<< endl;
n++;
}

fstream input(name);
if (input)
{
double total = 0;
double count = 0;
double n;
while (input >> n) {
total = total + n;
count++;
}
double average = total / count;
cout << " the sum is " << total << endl;
cout << " the average is " << average << endl;
}
}
else
cout<<"file is not open"<< '\n';

input.close();
return 0;
}
These and others may need to be outside your loop.
1
2
3
		double total = 0;
		double count = 0;
		double n;


Every time a loop occurs it resets your values inside the loop.
You have 2 while loops, why would you need that ?

I'm not sure what double n; is doing.
You have int n at the top and double at the bottom.

You have no comments so I can't tell what your thinking, just looks like spaghetti.
Last edited on
Topic archived. No new replies allowed.