I/O into array

This is a discussion on I/O into array within the C++ Programming forums, part of the General Programming Boards category; ive got this Code: struct student { int mark[MAX]; long id[MAX]; }; which is also part of Code: class subject ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    18

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

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    just the third one is in ReadFile..
    the others are b4 main..

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    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..
    Last edited by chizzy; 10-23-2002 at 08:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21