Inputting and outputting from structs

This is a discussion on Inputting and outputting from structs within the C++ Programming forums, part of the General Programming Boards category; OKAY here I go again ...doin some more homework.. The teacher gave us some of the code and told us ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Martin, Tn

    Inputting and outputting from structs

    OKAY here I go again ...doin some more homework.. The teacher gave us some of the code and told us what the code we needed to write should do. So inevitably I am lost. I have a lot of code written most of it was what I took as what he wanted me to write when I spoke to him. I think I have written WAY too much and have gotten lost.
    What I would like here is for you to simply tell me which of my function I do not need.

    The assignment is that in the end I have a text file that I will open and read info into some records (st#) of type "Studentrec" I will have to compare 2 random records with a bool operator. I havent made it this far yet, I am simply trying to get the struct to hold data and print that data part of which is an ENUM.

    I will appologize right now for the length of this.. Plz feel free to point and laugh at the noob. If you see a mistake I have made plz inform me in simple explanations. everything in RED is what the instructor gave us but if its wrong let me know plz

    #include <iostream>
    #include <cstring>
    #include <cassert>
    using namespace std;
    enum year_in_school {freshman = 1, sophomore, junior, senior, special};
    struct studentrec
           char name[20];
           int age;
           float gpa;
           year_in_school classification;
    }; //End Struct
    istream &operator >>(istream &is, year_in_school &yr);
    ostream &operator <<(ostream &os, year_in_school yr);
    istream &operator >>(istream &is, studentrec &r);
    void print ( /* I have not made this function work yet everything I put here errors*/);
    int main(void)
        studentrec st1 = {"bob dylan", 64, 3.6, senior};
        print (st1);
       /*this is just to test. the real prog will read from a text file I just wanted
              to see if everything was working*/
        system ("pause");
       return 0; 
    ostream &operator <<(ostream &os, year_in_school yr)
      case freshman:  os << "Freshman";  break;
      case sophomore: os << "Sophomore"; break;
      case junior:    os << "Junior";    break;
      case senior:    os << "Senior";    break;
      case special:   os << "Special";   break;
      default:        os << "Classification Unspecified" << endl; break;
     return  os;
    } //end year_in_school OS
    istream &operator >>(istream &is, year_in_school &yr)
     int year;
     is >> year;
     if (is.eof()) return is;
       yr = static_cast <year_in_school> (year);
    return is;
    } //end yr IS
    istream &operator >>(istream &is, studentrec &r)
    /* Function reads a student rec, allowing 20 characters including spacesin a name
       year_in_school is read with a user supplie operator >>. */
      int temp;
      char filler[80];
        /* Reads 19 chars or to an end of line whichever comes first
           does not remove end of line. Adds a null character */
           return is;
        is >> r.age >> r.gpa >> r.classification;
       /* To properly remove end of line char so next read of string works correctly
          reads 79 characters including end of line, adding null. End of line not stored. */
      return is;
    void print (/* I have not made this function work yet everything I put here errors*/)
         int i;
     for(i=0; i<20; i++)
        cout << name[i] << endl;    
    In the end his instructions are
    1. read the Students.txt file into an array and print the orig list
    EX from The text file: Maynard, Jill 22 3.15 3
    he wants the year_in_school (enum) to be input as an int. so far I havent been able to make it do this.
    2. sort the array using the < operation (if you need ==, >= or > define them also).
    I have no Idea what he is talking about here but I will ask him. or if you can offer a short simple explanation
    3. Print sorted array.
    I dont know if he only wants the names to be printed here or all the information printed and sorted by names again I have to talk to him again.

    That will all come later for now if you could just tell me what to cut out of this and maybe a description of what I need to write.. Again I DO NOT expect anyone to do my homework for me just to aid me allong. If there is something that I will absolutly need and you can give me a lil piece of code along with an explanation that would be Wonderful.

    Thank you.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Forget your Print function and use this instead ...

    ostream &operator <<(ostream &os, const studentrec &r);
    ostream &operator <<(ostream &os, const studentrec &r)
        os << "Name : " << << endl;
        os << "Age : " << r.age << endl;
        // Depends on how you want to format a GPA, as I'm English, not a septic, I don't know
        // how you format a GPA, but I'm guessing something like 3.1 ?
        os << "GPA : ";
        os  << r.gpa << endl;
        os << "Classification : " << r.classification << endl;
        os << endl;
        return( os );
    Also you might want to do something like this ...

    istream &operator >>(istream &is, year_in_school &yr)
        int year;
        is >> year;
        switch( year )
            case freshman:
                yr = freshman;
            case sophomore:
                yr = sophomore;
            case junior:
                yr = junior;
            case senior:
                yr = senior;
            case special:
                yr = special;
                 // What are you going to do if it isn't a valid value?
        return( is );
    Also someone ought to tell your lecturer that is quite a common convention to use upperacase for enum values. And that he ought to use "const" a bit more!

Popular pages Recent additions subscribe to a feed

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