Help on istream& operator ?

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

    #1

    Help on istream& operator ?

    Please help why this code not compile
    I would like it to be something like this:
    istringstream stringstreams(l ine);
    stringstreams>> ptrstudent->bypassed>>ptrs tudent->name>>ptrstude nt->midterm;
    stringstreams>> ptrstudent->quiz>>ptrstude nt->final>>ptrstud ent->testname;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    #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 ;
    istream& operator >>(istream & is,prtstudent & r){
    is>>r.bypassed> >r.name>>r.midt erm,r.quiz,r.fi nal,r.testname; }
    ptrstudent++;
    }
    return 0;
    }

  • Michiel.Salters@tomtom.com

    #2
    Re: Help on istream&amp; operator ?

    tvn007 wrote:[color=blue]
    > Please help why this code not compile[/color]

    Well, the compiler told you, in the error message. Please post it,
    there are good reasons why the FAQ says you must do that.
    [color=blue]
    > ///////////////////////////////////////////////////////////////////////////////////////////////////////
    > #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 ;
    > istream& operator >>(istream & is,prtstudent & r){
    > is>>r.bypassed> >r.name>>r.midt erm,r.quiz,r.fi nal,r.testname; }
    > ptrstudent++;
    > }
    > return 0;
    > }[/color]

    Functions should be defined and used separately. It looks like you
    don't
    understand the difference, and you tried to put something that's
    neither
    a definition nor a call of operator>> inside main.

    Try a simpler program first, defining and calling a function void
    print_hello_wor ld( )

    HTH,
    Michiel Salters

    Comment

    • Peter_Julian

      #3
      Re: Help on istream&amp; operator ?


      "tvn007" <tvn007@hotmail .com> wrote in message
      news:1133421143 .893522.93260@z 14g2000cwz.goog legroups.com...
      | Please help why this code not compile
      | I would like it to be something like this:
      | istringstream stringstreams(l ine);
      |
      stringstreams>> ptrstudent->bypassed>>ptrs tudent->name>>ptrstude nt->midte
      rm;
      |
      stringstreams>> ptrstudent->quiz>>ptrstude nt->final>>ptrstud ent->testname
      ;
      |
      |
      ////////////////////////////////////////////////////////////////////////
      ///////////////////////////////
      | #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 ;
      | istream& operator >>(istream & is,prtstudent & r){
      | is>>r.bypassed> >r.name>>r.midt erm,r.quiz,r.fi nal,r.testname; }
      | ptrstudent++;
      | }
      | return 0;
      | }
      |

      Start by getting a simplified skeleton to work first. Appropriate
      formatting goes a long way to help you identify your individual
      declarations.

      // test2.txt
      name_1
      name_2
      name_3

      So your test.cpp file should look like this:

      #include <iostream>
      #include <ostream>
      #include <string>
      #include <fstream>

      // Record type
      struct Record_info
      {
      void set(std::string & s) { name = s; }
      private:
      std::string name;
      /* Friend op>> */
      friend std::ostream&
      operator<<(std: :ostream&, const Record_info&);
      };

      // global op<<
      std::ostream& operator<<(std: :ostream& os, const Record_info& r)
      {
      os << "name: " << r.name << "\n";
      return os;
      }

      int main()
      {
      Record_info students[10];

      // open file
      std::ifstream in("test2.txt") ;
      if( !in )
      {
      std::cout << "error while opening file.\n";
      return 0;
      }

      // parse file
      int count = 0;
      std::string s;
      while ( std::getline(in , s) )
      {
      students[count++].set(s);
      }
      if( !in.eof() ) // paranoia check, was the error *not* an eof
      marker?
      {
      std::cout << "error while parsing file.\n";
      return 0;
      }

      // display contents
      std::cout << "number of students: " << count << "\n";
      for (int i = 0; i < count; ++i)
      {
      std::cout << students[i];
      }

      return 0;
      } // main()

      /*
      number of students: 3
      name: name_1
      name: name_2
      name: name_3

      */



      Comment

      Working...