Reading from a data text file with multiple values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apedosmil8
    New Member
    • Apr 2007
    • 15

    Reading from a data text file with multiple values

    Hello, I am trying to read from a text file and this is what I have so far.

    The text file looks like this but longer.

    0 111111112 15 155
    1 1111 4 A
    1 1112 4 B
    1 1113 3 C
    0 111111111 30 90
    1 1111 4 A
    1 1112 3 A

    0 stands for student followed by its ssn and 2 other variables that I must do math with.

    1 stands for class followed by the class number, credit hours, and the grade in the course. The grade must be converted into a number A = 4, B = 3, C = 2, D = 1, F = 0.

    I keep getting a infinate loop even though I used the &&1 in my while loop. Any ideas?

    Code:
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    int main()
    { //startMain
    
    ifstream indata;
    
    int ssn, grdLvl, cre, pts, gpa, impr;
    
    indata.open ("/home/lx/abyrnes/input/cs241/fall07/pgm1.txt");
    if (indata.fail())
            {
            cout <<"\nFILE IS MISSING!";
            exit(1);
            }
    
            indata >> ssn >> grdLvl >> cre >> pts >> gpa >> impr;
    
            while ( !indata.eof() && 1 )
            {
            cout << "Test: " <<ssn << " " << grdLvl << " " << cre << " "
            << pts << " " << gpa << " " << impr;
    
            indata >> ssn >> grdLvl >> cre >> pts >> gpa >> impr;
            }
    
    
                    indata.close();
    
    } //endMain
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by apedosmil8
    Hello, I am trying to read from a text file and this is what I have so far.

    The text file looks like this but longer.

    0 111111112 15 155
    1 1111 4 A
    1 1112 4 B
    1 1113 3 C
    0 111111111 30 90
    1 1111 4 A
    1 1112 3 A

    0 stands for student followed by its ssn and 2 other variables that I must do math with.

    1 stands for class followed by the class number, credit hours, and the grade in the course. The grade must be converted into a number A = 4, B = 3, C = 2, D = 1, F = 0.

    I keep getting a infinate loop even though I used the &&1 in my while loop. Any ideas?

    [CODE=cpp]


    #include <iostream>
    #include <iomanip>
    #include <fstream>

    using namespace std;

    int main()
    { //startMain

    ifstream indata;

    int ssn, grdLvl, cre, pts, gpa, impr;

    indata.open ("/home/lx/abyrnes/input/cs241/fall07/pgm1.txt");
    if (indata.fail())
    {
    cout <<"\nFILE IS MISSING!";
    exit(1);
    }
    // dont need to do it twice
    while ( !indata.eof() && 1 )
    {
    indata >> ssn >> grdLvl >> cre >> pts >> gpa >> impr;
    cout << "Test: " <<ssn << " " << grdLvl << " " << cre << " "
    << pts << " " << gpa << " " << impr;
    }
    indata.close();
    } //endMain
    [/CODE]
    There is no point in adding "&& 1", 1 will always be true so it's the same thing without:
    [code=cpp]
    int x = 5;
    if (x == 5 && 1)
    // same as
    if (x==5)
    [/code]
    It might be a good idea to use getline to process each line. But why are you even ">> ing" six variables if each one of your lines contains four? That's probably why your program gets stuck, you ask for too much input.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by ilikepython
      It might be a good idea to use getline to process each line. But why are you even ">> ing" six variables if each one of your lines contains four? That's probably why your program gets stuck, you ask for too much input.
      getline is the wrong thing to use here. An >> into four variables should do the trick. If you use getline, then you have to parse the line.

      Your observation about >> into six variables instead of four is on target.

      Comment

      Working...