struct ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tvn007

    #1

    struct ?

    I am working on the data struc. below:
    is there way I can abbrev. the expression below to avoid long
    expression.
    Thanks......
    ///////////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;
    const int MAX=50;
    struct Record_info {
    string bypassed;
    string name;
    int midterm;
    double quiz;
    double final;
    string testname;
    }student[MAX],*ptrstudent;

    int main (void){
    ptrstudent = student;
    ifstream in ("test2.txt" );
    string line;
    string line2;
    char buffer[40];
    while (getline(in,lin e)){
    if (line.find("#") !=string::npos) continue ;
    istringstream anyname(line);

    /////////// HOW CAN I ABBREV. EXPRESSION BELOW ?
    ////////////////////////////////////////

    anyname>>ptrstu dent->bypassed>>ptrs tudent->name>>ptrstude nt->midterm;

    anyname>>ptrstu dent->quiz>>ptrstude nt->final>>ptrstud ent->testname;
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ptrstudent++;
    }
    }

  • Ron Natalie

    #2
    Re: struct ?

    tvn007 wrote:
    [color=blue]
    > struct Record_info {
    > string bypassed;
    > string name;
    > int midterm;
    > double quiz;
    > double final;
    > string testname;[/color]

    istream& operator>> (istream& is) (
    is >> bypassed;
    is >> name
    is >> midterm;
    is >> quiz
    is >> final;
    is >> testname;
    return is;
    }
    [color=blue]
    > }student[MAX],*ptrstudent;
    >[/color]

    [color=blue]
    > int main (void){
    > ptrstudent = student;
    > ifstream in ("test2.txt" );
    > string line;
    > string line2;
    > char buffer[40];
    > while (getline(in,lin e)){
    > if (line.find("#") !=string::npos) continue ;
    > istringstream anyname(line);
    >
    > /////////// HOW CAN I ABBREV. EXPRESSION BELOW ?
    > ////////////////////////////////////////
    >[/color]

    [color=blue]
    > anyname>>ptrstu dent->bypassed>>ptrs tudent->name>>ptrstude nt->midterm;
    >
    > anyname>>ptrstu dent->quiz>>ptrstude nt->final>>ptrstud ent->testname;[/color]

    anyname >> *ptrstudent;
    [color=blue]
    > //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    > ptrstudent++;
    > }
    > }
    >[/color]

    Comment

    • Richard Herring

      #3
      Re: struct ?

      In message <4385ded1$0$284 83$9a6e19ea@new s.newshosting.c om>, Ron Natalie
      <ron@spamcop.ne t> writes[color=blue]
      >tvn007 wrote:
      >[color=green]
      >> struct Record_info {
      >> string bypassed;
      >> string name;
      >> int midterm;
      >> double quiz;
      >> double final;
      >> string testname;[/color]
      >
      >istream& operator>> (istream& is) ([/color]

      If its _left_ operand is to be an istream, this can't be a member
      function of Record_info. It has to be a non-member function with two
      arguments.

      friend istream & operator>>(istr eam & is, Record_info & rec) {
      [color=blue]
      > is >> bypassed;[/color]
      is >> rec.bypassed; // etc.[color=blue]
      > is >> name
      > is >> midterm;
      > is >> quiz
      > is >> final;
      > is >> testname;
      > return is;
      >}
      >[color=green]
      >> }student[MAX],*ptrstudent;
      >>[/color]
      >
      >[color=green]
      >> int main (void){
      >> ptrstudent = student;
      >> ifstream in ("test2.txt" );
      >> string line;
      >> string line2;
      >> char buffer[40];
      >> while (getline(in,lin e)){
      >> if (line.find("#") !=string::npos) continue ;
      >> istringstream anyname(line);
      >> /////////// HOW CAN I ABBREV. EXPRESSION BELOW ?
      >> ////////////////////////////////////////
      >>[/color]
      >
      >[color=green]
      >> anyname>>ptrstu dent->bypassed>>ptrs tudent->name>>ptrstude nt->midterm;
      >>
      >> anyname>>ptrstu dent->quiz>>ptrstude nt->final>>ptrstud ent->testname;[/color]
      >
      > anyname >> *ptrstudent;
      >[color=green]
      >>
      >>///////////////////////////////////////////////////////////////////////
      >>///////////////////////////////////////////////////////
      >> ptrstudent++;
      >> }
      >> }
      >>[/color][/color]

      --
      Richard Herring

      Comment

      • tvn007

        #4
        Re: struct ?

        I am not sure if it will work since I am using struct array and
        pointer.

        struct Record_info {
        string bypassed;
        string name;
        int midterm;
        double quiz;
        double final;
        string testname;
        }student[MAX],*ptrstudent;
        ptrstudent = student;

        anyname>>ptrstu dent->bypassed>>ptrs tudent->name>>ptrstude nt->midterm;


        anyname>>ptrstu dent->quiz>>ptrstude nt->final>>ptrstud ent->testname;

        Comment

        • Jakob Bieling

          #5
          Re: struct ?

          tvn007 <tvn007@hotmail .com> wrote:[color=blue]
          > I am not sure if it will work [..][/color]

          Have you tried it?
          --
          jb

          (reply address in rot13, unscramble first)


          Comment

          • Richard Herring

            #6
            Re: struct ?

            In message <1132850220.211 488.114170@g43g 2000cwa.googleg roups.com>,
            tvn007 <tvn007@hotmail .com> writes[color=blue]
            >I am not sure if it will work[/color]

            Not sure if _what_ will work? Please quote some context.
            [color=blue]
            >since I am using struct array and
            >pointer.[/color]

            I don't think that's relevant. Fundamentally you want to implement
            operator>>(istr eam&, Record_info &). If the Record_info happens to be an
            array element or the target of a pointer it makes no difference.[color=blue]
            >
            >struct Record_info {
            > string bypassed;
            > string name;
            > int midterm;
            > double quiz;
            > double final;
            > string testname;
            > }student[MAX],*ptrstudent;
            > ptrstudent = student;
            >
            >anyname>>ptrst udent->bypassed>>ptrs tudent->name>>ptrstude nt->midterm;
            >
            >
            >anyname>>ptrst udent->quiz>>ptrstude nt->final>>ptrstud ent->testname;
            >[/color]

            anyname >> *ptrstudent;

            --
            Richard Herring

            Comment

            Working...