C++ Question on ifstream!

Discussion in 'C++' started by Jupiter5F, Nov 16, 2003.

  1. Jupiter5F

    Jupiter5F Guest

    This problem from one of the message boards has got my curiosity going. The
    task is to read in lines of data from a file where each line contains a name
    and some integers. We want to output the name and the result of adding the
    integers. i.e.
    ~~~~~~~~~~~~~~~~

    Frank 3 5 1 9
    Jeff 4 9 0
    Nancy 2
    Ron 1
    Tess 4 9 2 1 4
    ~~~~~~~~~~~~~~~~~
    output from above text file should output:

    Frank 18
    Jeff 13
    Nancy 2
    Ron 1
    Tess 20

    My code outputs the first line only. In previous things I've done the input
    files were consistent, this is a little different. here's my code:

    #include <fstream>
    #include <conio>
    #include <string>

    using namespace std;

    int main()
    {
    string name;
    string inputFile = "mylist.txt";
    int total = 0;
    int number = 0;
    ifstream inFile;

    inFile.open(inputFile.c_str());
    if(!inFile)
    {
    cout << "Could not open file: " << inputFile << endl;
    return 1;
    }
    inFile >> name;
    while(inFile)
    {
    while(inFile >> number)
    total += number;
    cout << name << " " << total << endl;
    total = 0;
    number = 0;
    inFile >> name ;
    }

    getch();
    }
    Jupiter5F, Nov 16, 2003
    #1
    1. Advertising

  2. Jupiter5F wrote:
    > ~~~~~~~~~~~~~~~~
    >
    > Frank 3 5 1 9
    > Jeff 4 9 0
    > Nancy 2
    > Ron 1
    > Tess 4 9 2 1 4
    > ~~~~~~~~~~~~~~~~~

    [. . .]
    > while(inFile)
    > {
    > while(inFile >> number)
    > total += number;


    The first time this loop is run, it will stop after it attempts to read an
    integer from the second line, and runs into the word "Jeff." The loop stops
    because the failbit will be set on inFile.

    > cout << name << " " << total << endl;
    > total = 0;
    > number = 0;
    > inFile >> name ;


    This read operation will fail, because the failbit is still set. Then the
    outer loop will end, because the expression (inFile) will evaluate to
    false--again, because the failbit is set.

    The simplest fix is the clear the failbit after you exit the inner loop.
    Add this line after "total += number;":

    inFile.clear();

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
    Russell Hanneken, Nov 16, 2003
    #2
    1. Advertising

  3. Jupiter5F

    Jupiter5F Guest

    Thank you Russell! Your solution has cleared the problem!
    Jupiter5F, Nov 16, 2003
    #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. flips

    Simple ifstream question

    flips, Nov 23, 2003, in forum: C++
    Replies:
    11
    Views:
    518
    Mike Wahler
    Nov 23, 2003
  2. Anton Ishmurzin
    Replies:
    7
    Views:
    7,673
    Anton Ishmurzin
    Nov 26, 2003
  3. tlmseven
    Replies:
    7
    Views:
    613
    Old Wolf
    Nov 9, 2004
  4. Kitty

    ifstream question

    Kitty, Dec 22, 2004, in forum: C++
    Replies:
    2
    Views:
    405
    Mike Wahler
    Dec 22, 2004
  5. phoenix
    Replies:
    1
    Views:
    335
    Victor Bazarov
    Jan 13, 2005
Loading...

Share This Page