Help on struct ?

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

    #1

    Help on struct ?

    Please help, I thought the program save all data into struc
    *ptrstudent.
    however, when cout it only print the last piece of data.

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;

    struct Record_info {
    string name;
    double midterm;
    double quiz;
    double final;
    }student[50],*ptrstudent;
    int main (void){

    double grade;

    ifstream in ("test2.txt" );
    string line,word;
    while (getline(in,lin e)){
    ptrstudent = student;
    if (line.find("#") !=string::npos) continue ;
    istringstream anyname(line);

    anyname>>ptrstu dent->name>>studen t->midterm>>stude nt->quiz>>studen t->final;

    ptrstudent++;
    }

    for (ptrstudent=stu dent; ptrstudent->name[0];ptrstudent++){
    cout << ptrstudent->name<<endl;
    cout << ptrstudent->midterm<<end l;
    cout << ptrstudent->quiz<<endl;
    cout << ptrstudent->final<<endl;
    }

    return 0;
    }
    /////////// input file "test2.txt" ///////////////////////
    ##########3
    ##########3
    Tony 90.0 -15.2 98.2
    ##########3
    Michael 95.0 17.2 92.2
    /////////////////////////////////////////////

    //////////////// output from program /////////////
    Michael
    95.0
    17.2
    92.2

    ////////////////////// Want the output as follow /////////////
    Tonny
    90.0
    -15.2
    98.2
    Michael
    95.0
    17.2
    92.2

  • John Harrison

    #2
    Re: Help on struct ?

    tvn007@hotmail. com wrote:[color=blue]
    > Please help, I thought the program save all data into struc
    > *ptrstudent.
    > however, when cout it only print the last piece of data.
    >
    > #include <iostream>
    > #include <string>
    > #include <fstream>
    > #include <sstream>
    > using namespace std;
    >
    > struct Record_info {
    > string name;
    > double midterm;
    > double quiz;
    > double final;
    > }student[50],*ptrstudent;
    > int main (void){
    >
    > double grade;
    >
    > ifstream in ("test2.txt" );
    > string line,word;
    > while (getline(in,lin e)){
    > ptrstudent = student;[/color]

    Here is the error, you assign strudent to ptrstudent each time round the
    loop. It doesn't matter than you are incrementing it later on because
    you go and set it back to the beginning each time round the loop.

    You probably meant only to assign it once before the loop, like this

    ptrstudent = student;
    while (getline(in,lin e)){

    john

    Comment

    • tvn007@hotmail.com

      #3
      Re: Help on struct ?

      after the fix, the output still does not come out correct.

      #include <iostream>
      #include <string>
      #include <fstream>
      #include <sstream>
      using namespace std;

      struct Record_info {
      string name;
      double midterm;
      double quiz;
      double final;
      }student[50],*ptrstudent;
      int main (void){

      double grade;

      ifstream in ("test2.txt" );
      string line,word;
      ptrstudent = student;
      while (getline(in,lin e)){
      //ptrstudent = student;
      if (line.find("#") !=string::npos) continue ;
      istringstream anyname(line);

      anyname>>ptrstu dent->name>>studen t->midterm>>stude nt->quiz>>studen t->final;

      ptrstudent++;
      }

      for (ptrstudent=stu dent; ptrstudent->name[0];ptrstudent++){
      cout << ptrstudent->name<<endl;
      cout << ptrstudent->midterm <<endl;
      cout <<ptrstudent->quiz<< endl;
      cout << ptrstudent->final<<endl;
      }

      return 0;
      }
      /////////// input file "test2.txt" ///////////////////////
      ##########3
      ##########3
      Tony 90.0 -15.2 98.2
      ##########3
      Michael 95.0 17.2 92.2
      /////////////////////////////////////////////

      //////////////// output from program /////////////
      Tony
      95
      17.2
      92.2
      Michael
      0
      0
      0


      ////////////////////// Want the output as follow /////////////
      Tonny
      90.0
      -15.2
      98.2
      Michael
      95.0
      17.2
      92.2

      Comment

      • tvn007@hotmail.com

        #4
        Re: Help on struct ?

        never mind. I was a typo on my part.
        Thanks

        Comment

        Working...