You are using a version without Ads of this website. Please, consider donating:

### 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..

Sorry! This is my first encounter with cplusplus!

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include #include 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:
 ``12345678910111213141516171819202122`` ``````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 } ``````

 ``1234567891011121314151617181920212223`` ``````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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````using namespace std; #include #include #include #include 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/