homework help about ofstream/ifstream.. help PLZ

This is a discussion on homework help about ofstream/ifstream.. help PLZ within the C++ Programming forums, part of the General Programming Boards category; here is the given code. basically the program is supposed to open up the .txt file and save the data ...

  1. #1
    kv2
    kv2 is offline
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Red face homework help about ofstream/ifstream.. help PLZ

    here is the given code.
    basically the program is supposed to open up the .txt file and save the data from the .txt to an apvector.

    ---------------------------

    #include<fstream.h>
    #include"apstring.cpp"
    #include"apstring.h"
    #include"apvector.h"
    struct student
    {
    int id;
    apstring last;
    apstring first;
    int grade;
    char gender;
    apstring bd; // birthdate
    };
    apvector <student> getStudents( ifstream list );
    student getData( apvector <student>, int );

    void main()
    {
    ifstream idFile;
    idFile.open("idp4.txt");// means ID number period 4,
    // if that clarifies anything
    apvector <student> p4 ( getStudents ( idFile ) );
    int id1;
    cout << "Enter an id number: ";
    cin >> id1;
    student current = getData ( p4, id1 );
    if( current.last != "Not Found" )
    {
    cout << current.id << '\t'
    << current.first << '\t'
    << current.last << '\t'
    << current.grade <<'\t'
    << current.gender<< '\t'
    << current.bd<< endl;
    }
    else
    cout << "ID number "<< id1 << " not found.\n";

    idFile.close();

    }

    apvector <student> getStudents( ifstream list )
    /* pre: list is open and contains student data as follows
    id number
    last name
    first name
    grade level ( 9 10 11 12 )
    gender ( m f )
    birthday ( day/month )
    post: data in file is stored in an apvector of type student
    */
    {
    apvector<students>person;
    int i=0;
    while(list>>person[i].id)
    {





    student getData( apvector <student> thisClass, int thisID )
    /* pre: thisClass is initialized as is thisID
    post: returns student data for matching id if found
    if id is not found, returns a student with "Not Found"
    in the last name field
    */
    {


    -------------------------


    ok that's it... I have a few questions


    for... apvector<students>getStudents (ifstream list) ... I know I'm supposed to make something like:

    apvector<students>person;
    person[i].id=
    person[i].last=
    person[i].first=
    person[i].grade=
    person[i].gender=
    person[i].bd=

    with a loop or something.. but what I don't get is how the program giong to know where the first person's info starts/ends and second person's begins...

    How do you store a person's info in an apvector if the .txt is one big file with everyone's name/ID/birthdate/grade lvl... etc.? It doesn't know when the second person's info begins, so then I don't know when to save person[i].id... beacuse I think I'm gonna save the entire .txt file to "person[i].id" or something..

    kinda confusing here but help plz

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    You need to have a student class with the same number of arguments matching the types of data in the file. Such as: name, id , grade,gender and birthdate. Name will probably be a string object so you don't have two; it makes it simpler. When a class such as 'student' is made, it will store all of the info within it. The member functions for this class would probably be just a getdata function.
    SilasP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. Help with Homework plz!
    By mythassassin in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2007, 03:07 PM
  3. Need help with homework assignment plz..
    By RVDFan85 in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2006, 10:33 PM

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