Asked By
Maryam
20 points
N/A
Posted on - 07/19/2011
I want to develop a program that will input records of ten students and write these into a sequential file. The record of a student contains the following fields:
Name, address, Marks of three subjects (s1, s2, s3)
Then calculate total marks and also find out the grade of each student. The issue is that whenever I create a file in the system drive and want to write records on it, the compiler gives error that file no exists. Guide me why this error occurs and what will be the solution? Waiting.
Answered By
Fahar
10 points
N/A
#91660
Use of Files in C++ Programming
Include header file in start.
#include<fstream.h>
#include<stdlib.h>
And use this code for entering record into the file.
ofstream addrec("File Name Here");
Try this.
Answered By
Maryam
20 points
N/A
#91661
Use of Files in C++ Programming
Still the same error message. Where i need to make the file first? In My documents folder or in Program Files? And also what loop structure will be best for this situation to take ten student's records?
Answered By
Fahar
10 points
N/A
#91662
Use of Files in C++ Programming
Make the file in the same folder in which you are saving your Program Code. And call with inverted commas on both sides. Use Do While structure in this case. You can use For loop also but Do while will be the best working loop with files.
Just define one string type variable, takes its value from the user in Yes or No.
If yes then enter another record else exit. It will take the student's record till you want to enter. try now.
Answered By
Fahar
10 points
N/A
#91663
Use of Files in C++ Programming
An Idea solution for this program is here. Try it.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char name[15], address[30],grade[5],newRecord;
int s1,s2,s3,total,i=1;
ofstream addrec(“students.dat”);
if(!addrec)
{
cerr<<”File Opening Error”<<endl;
exit(1);
}
}
Do
{ clrscr();
Cout<<”Enter Name “ ;
Cin>>name;
Cout<<”Enter Address “ ;
Cin>>address;
Cout<<”Enter Marks of 1st subject “ ;
Cin>>s1;
Cout<<”Enter Marks of 2nd subject “ ;
Cin>>s2;
Cout<<”Enter Marks of 3rd subject “ ;
Cin>>s3;
Total=s1+s2+s3;
If(total/3>=90) strcpy (grade,”A+”);
Else If(total/3>=80) strcpy (grade,”A”);
Else If(total/3>=70) strcpy (grade,”B”);
Else If(total/3>=50) strcpy (grade,”C”);
Else strcpy (grade,”F”);
Addrec<<name<<”/t”<<address<<”/t”<<s1<<”/t”<<s2<<”/t”<<s3<<”/t”<<total<<”/t”<<grade<<endl;
Cout<<”More records [Y/N]?”;
Cin>>newRecord
}
while(newRecord==’Y’ || newRecord==’y’)
}
Answered By
Maryam
20 points
N/A
#91664
Use of Files in C++ Programming
Thanks a lot for your consideration and solution.