Help with using getline function

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
}
Post the error messages so that we can take a look at whats wrong.
Thanks for trying to use the code tags..

If you edit your post and add the closing code tag ("[/code]"), it may be easier for us to read your code.
Sorry! This is my first encounter with cplusplus!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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
} 
alx101 suggested that you post your error messages. That would be helpful.

But in the interim, lines 7 - 20 need to be in a function somewhere. I'm sure this is giving you errors. Actually, lines 29 to the end are also orphaned.

It looks like you are not trying to write functions (that's fine--you'll add that when you are ready). So you need to understand how the program runs so you know how to structure your code.

Program execution begins at the opening brace ({) of main and runs until the closing brace (}) of main. If you are not writing functions, all of your code must be between the braces1. Your opening brace is at line 23, and your closing brace is at line 28. If all of the erroneous code were ignored, your program merely declares some variables, initializes one of them, and exits.

You need to move lines 22 - 27 to before line 7. And I think you need to get rid of line 28.

It will also help you to indent your code. Inside every set of braces you might want to add a tab or a few spaces so you can see the structure of your code. Compare:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
} 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main ()
{
    //declaring variables
    int n;
    float sum, avg, vari, sd;
    int variables=100;
}  // This is an extraneous closing brace.
   // It doesn't line up properly with the intended structure of the program
    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
} 

Of course, this could be a relic of trying to paste your code. If you click the code tags button ("<>" ) in the format menu and then cut and paste your code from your editor, the indentation is usually preserved.

1 There are possible exceptions like global variables, macro definitions, etc., but for this simple example we are ignoring those things.
Thank you for your advice. Here is my revised code, however 'XCode' is telling me that my 'while' and 'if' statements have no body.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using namespace std;
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>

int main ()
{
    //declaring variables
    int n;
    float sum, avg, vari, sd;
    int numPoints;
    int Points[n];
    string line;
    
    ifstream Myfile ("StatNum.txt.");
    if (Myfile.is_open()); //if the file is open, proceed with output
    {
        while (! Myfile.eof());
        {
            getline (Myfile,line);
            stringstream convert(line);
            
            if (!(convert >> Points[n])) //uses integers in string to give values to points[n]
            {
                Points[n]=0; //if not set points[n]=0
            }
            cout << Points[n] << endl;
        }
        numPoints=n;
    }
    for (int n=1;n<=100;n++) {
        sum=sum+Points[n];
    }
    avg=sum/n;
    for (int n=1;n<=100;n++)
    {
        vari = ((Points[n]-avg)*2)/n;
    }
    sd=sqrt(vari);
    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
}
Remove the semi-colons in lines 17 and 19.

For additional detail, see the following tutorial topic.

http://cplusplus.com/doc/tutorial/control/

The tutorial on this site is very good. The table of contents is here.

http://cplusplus.com/doc/tutorial/
Last edited on
Topic archived. No new replies allowed.