Hi all im stuck with a problem im ask to enter 8 student marks and calculate the average mark and store them into a text file but i cant seem to storm the 8 student marks but got no problem with the average this is what i type in my codeblock C++
Code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int i,sum, marks[8];
double average;
ofstream outfile("outfile1.txt");
for (i=0;i<8;i++)
{
cout << "Enter Next Student: " << endl;
cin >> marks[i];
}
outfile<<"Student"<<i+1<<
marks[8]<<endl;
sum=0;
for (i=0;i<8;i++)
{
sum=sum+marks[i];
}
average=sum/(i);
cout<<"The Result is printed in outfile1.txt";
outfile<<"The average score is : "<<
average<<endl;
outfile.close();
return(0);
}
and this is wat i get from my outfile1.txt
Student92009291924
The average score is : 2
i need help thx
or do i have to do the stupid and long way which is
Code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a,b,c,d,e,f,g,h;
double average;
ofstream outfile("outfile1.txt");
{
cout << "Enter Next Student: " << endl;
cin >> a;
}
outfile<<"Student: "<<1<<" "<<
a<<endl;
{
cout << "Enter Next Student: " << endl;
cin >> b;
}
outfile<<"Student: "<<2<<" "<<
b<<endl;
{
cout << "Enter Next Student: " << endl;
cin >> c;
}
outfile<<"Student: "<<3<<" "<<
c<<endl;
{
cout << "Enter Next Student: " << endl;
cin >> d;
}
outfile<<"Student: "<<4<<" "<<
d<<endl;
{
cout << "Enter Next Student: " << endl;
cin >> e;
}
outfile<<"Student: "<<5<<" "<<
e<<endl;
{
cout << "Enter Next Student: " << endl;
cin >> f;
}
outfile<<"Student: "<<6<<" "<<
f<<endl;
{
cout << "Enter Next Student: " << endl;
cin >> g;
}
outfile<<"Student: "<<7<<" "<<
g<<endl;
{
cout << "Enter Next Student: " << endl;
cin >> h;
}
outfile<<"Student: "<<8<<" "<<
h<<endl;
average=(a+b+c+d+e+f+g+h)/8;
cout<<"The Result is printed in outfile1.txt";
outfile<<"The average score is : "<<
average<<endl;
outfile.close();
return(0);
}