-
I/O into array
ive got this
Code:
struct student
{
int mark[MAX];
long id[MAX];
};
which is also part of
Code:
class subject
{
private:
char code[MAX];
int students[MAX];
student data;
public:
void ReadFile();
void DisplayMenu();
};
how do i read from a file and store it into an array....
Code:
inFile>>code;
cout<< "Subject Code" << code;
inFile>>students;
cout << "\tNumber of Students" << students <<endl;
for(int i=0;i<students;i++)
{
inFile>>id;
cout << "Student Number" <<id;
inFile>>smark;
cout << "\tMark" << mark <<endl;
this doesnt work..
can anybody help me..
thankyou...
-
just the third one is in ReadFile..
the others are b4 main..
-
Code:
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <stdlib.h>
const int MAXSTUDENTS = 20;
const int MAXSUBJECTS = 10;
void ReadFile(void);
struct student
{
int student_mark[MAXSTUDENTS];
long student_num[MAXSTUDENTS];
};
class subject
{
private:
char subcode[MAXSUBJECTS];
int num_students[MAXSTUDENTS];
student data;
public:
void ReadFile();
};
int main()
{
clrscr();
ReadFile();
return 0;
}
void subject::ReadFile(void)
{
int ch;
student data;
//int num_students, student_mark;
//float mean=0, total=0, Grade=0;
//char num_students[MAXCHARS];
ifstream inFile;
inFile.open("a:\subjdata.dat");
if (inFile.fail())
{
cout << "\nError! File Not Found";
while(!kbhit());
exit(1);
}
cout << "Succesfull\n\n";
while((ch = inFile.peek()) != EOF)
{
inFile>>subcode;
cout<< "Subject Code" << subcode;
inFile>>num_students;
cout << "\tNumber of Students" << num_students <<endl;
for(int i=0;i<num_students;i++)
{
inFile>>student_num;
cout << "Student Number" <<student_num;
inFile>>student_mark;
cout << "\tMark" << tudent_mark <<endl;
}
}
while(!kbhit())
inFile.close();
return;
}
Does this help..