read data from file into structure ?

Discussion in 'C++' started by tvn007@hotmail.com, Nov 19, 2005.

  1. Guest

    I am using the following code to read data from input file into
    structure.
    Not sure it is the best way. Please comment or you have better way.
    please let me kmow.
    //////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;

    struct Record_info {
    string name;
    double midterm;
    double quiz;
    double final;
    }student ;
    int main (void){

    double grade;

    ifstream in ("test2.txt");
    string line,word;
    while (getline(in,line)){
    istringstream anyname(line);

    anyname>>student.name>>student.midterm>>student.quiz>>student.final;
    grade = student.quiz+ 10;
    cout << student.name <<endl;
    cout << student.midterm <<endl;
    cout << student.quiz <<endl;
    cout << student.final <<endl;
    cout << "GRADE: "<<grade <<endl;
    }
    return 0;
    }
    /////////////////////// test2.txt
    ///////////////////////////////////////
    Tony 90.0 -15.2 98.2
    Michael 95.0 17.2 92.2
     
    , Nov 19, 2005
    #1
    1. Advertising

  2. vire Guest

    use a class or a function to do it
     
    vire, Nov 19, 2005
    #2
    1. Advertising

  3. wrote:
    > I am using the following code to read data from input file into
    > structure.
    > Not sure it is the best way. Please comment or you have better way.
    > please let me kmow.
    > //////////////////////////////////////////////////////////////////////////
    > #include <iostream>
    > #include <string>
    > #include <fstream>
    > #include <sstream>
    > using namespace std;
    >
    > struct Record_info {
    > string name;
    > double midterm;
    > double quiz;
    > double final;
    > }student ;
    > int main (void){
    >
    > double grade;
    >
    > ifstream in ("test2.txt");
    > string line,word;
    > while (getline(in,line)){
    > istringstream anyname(line);
    >
    > anyname>>student.name>>student.midterm>>student.quiz>>student.final;
    > grade = student.quiz+ 10;
    > cout << student.name <<endl;
    > cout << student.midterm <<endl;
    > cout << student.quiz <<endl;
    > cout << student.final <<endl;
    > cout << "GRADE: "<<grade <<endl;
    > }
    > return 0;
    > }
    > /////////////////////// test2.txt
    > ///////////////////////////////////////
    > Tony 90.0 -15.2 98.2
    > Michael 95.0 17.2 92.2
    >


    Looks fine to me, apart from the gratuitous use of a global variable.
    Try this

    struct Record_info {
    string name;
    double midterm;
    double quiz;
    double final;
    };

    int main()
    {
    ...
    while (getline(in,line))
    {
    istringstream anyname(line);
    Record_info student;
    anyname >> student.name >> student.midterm >>
    student.quiz >> student.final;
    ...
    }
    ...
    }

    Declare variables locally where you need them, not globally.

    john
     
    John Harrison, Nov 20, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joan
    Replies:
    11
    Views:
    1,297
  2. Vasilis Serghi

    How do I read a csv file into a structure?

    Vasilis Serghi, Jan 19, 2004, in forum: C Programming
    Replies:
    5
    Views:
    1,096
    Vasilis Serghi
    Jan 23, 2004
  3. Replies:
    3
    Views:
    1,574
  4. meisterbartsch
    Replies:
    2
    Views:
    793
    meisterbartsch
    Jun 12, 2007
  5. A
    Replies:
    27
    Views:
    1,646
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page