Help with sum, mean and variance.

Hello!

I am completely new to c++ and have decided to teach myself!
For an exercise I want to use the numbers (1-100) in a file called Numbers.txt., then to generate the sum, mean and standard deviation.

The nature of my txt file requires me to convert the characters to integers and use the getline function. Here it was I have so far, but I gave lots or error messages!

Any help would be very much appreciated.


[code]#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;

ifstream Myfile ("StatNum.txt.");
if (Myfile.is_open()); //If file is open, proceeds with output
{
while(Myfile.good());
{
getline (Myfile,line);
stringstream convert(line);

if (!(convert >> Points[n])) //uses integers in string to give value to points[n]
{
Points[n]=0; //If not set Points[n]=0
}
cout << Points[n] << endl; // output the points
}

int main ()
{
//declaring variables
int n;
float sum, avg, vari, sd;
int variables=100;
}
for (int n=1;n<=100;n++) {
sum = sum+1;
}
avg = sum / n;
for (int n = 1; n<=100; n++) {
var += (x[n] - avg) * 2;
}
sd = sqrt ( var );
cout << "Sum is" << sum << endl;
cout << "Mean is" << avg << endl;
cout << "Standard Deviation is" << sd << endl;

myfile.close(); //closes the file
return 0; //terminates programme
}

You may not place any statements except declarations outside a function in C/C++. So this code before main

1
2
3
4
5
6
7
8
9
10
11
12
13
 if (Myfile.is_open()); //If file is open, proceeds with output
 {
 while(Myfile.good());
 {
 getline (Myfile,line);
 stringstream convert(line);

 if (!(convert >> Points[n])) //uses integers in string to give value to points[n]
 {
 Points[n]=0; //If not set Points[n]=0
 }
 cout << Points[n] << endl; // output the points
 }


is invalid.
Last edited on
Topic archived. No new replies allowed.