URGENT!! fstream

Hi, I am facing problem in designing a new program which its requirements are:
1)Program with file output operation:
User to enter a group of students’ names and
marks for a few subjects they have taken.
2)Program with file input operation:
Retrieve the students’ names and all subject
marks. Display all details and calculate average or
number of failures / passes etc.
I suppose to use 'fstream' but I don't really know how exactly it works. Can somebody help?

Last edited on
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
string name;
int num, i, j, marks, sum=0;
double average;
cout << "Please enter number of students: " ;
cin >> num;
for(i=0; i < num; i++)
{
cout<< "\nEnter student "<< i+1 << " name: ";
std::getline (std::cin, name);
cin.ignore();
//getline(cin, name);
{
for (j=0; j<5; j++)
{
cout << "Please enter subject " << j+1 << " marks: " ;
cin >> marks;
sum+= marks;
average= sum /5;

}
if (average<50)
{
cout << name << "has failed. " << endl;
}
}
}


return 0;
}

well actually I did this at first, but it seems like it got some error so I redo another one..
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

int main()
{
ofstream fout ("marks.txt");
int num;
cout << "Enter the number of student: " ;
cin >> num;
int marks[num][5];
string name;
char names [num][50];
int i,j;
for(i=0; i < num; i++)
{
cout<< "\nEnter student "<< i+1 << " name: ";
if(i==0)
cin.ignore();

getline(cin, name);
strcpy(names[i],name.c_str());
}

for (i=0;i<num;i++)
{
for (j=0;j<5;j++)
{
cout<< "Enter marks for student "<< names[i];
cout<< ", subject "<< j+1<< ":";
cin>> marks[i][j];
}
}
cin.ignore();
fout << "\n\t\t\t\tSub 1\tSub 2\tSub 3\tSub 4\tSub 5" <<endl;
for(i=0;i<num;i++)
{
fout << "Mark for student " <<

names[i];
fout<< "\t";
for(j=0;j<5;j++)
{
fout << " " << marks[i][j]<< "\t";
}
fout<<"\n";
}

fout.close();

ifstream fin;
fin.open("marks.txt");
string line;
while(!fin.eof())
{
getline(fin,line);
cout << line << endl;
}
fin.close();

return 0;
}
Firstly, please post errors and use code tags and secondly, why the c strings and c string methods, why not just use <string>?
what is the difference between cstring and string?
http://stackoverflow.com/questions/12824595/difference-between-cstring-and-string
In my experience there's basically no reason to use c strings unless you are interfacing with C code rather than C++ as std::strings are the improved C++ version.
Last edited on
Topic archived. No new replies allowed.