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 braces
1. 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.