0

I'm doing 9th exercise of 6th chapter from C++ Primer Plus book, and, I've got some problem with reading from a file. It must be something wrong with operator >> and getline, but I don't know what, and how to deal with that. I would be really grateful if somebody could tell me what I should do in that case.

Here is the exercise:

Do Programming Exercise 6 but modify it to get information from a file.The first item in the file should be the number of contributors, and the rest of the file should consist of pairs of lines, with the first line of each pair being a contributor’s name and the second line being a contribution.That is, the file should look like this: 4 Sam Stone 2000 Freida Flass 100500 Tammy Tubbs 5000 Rich Raptor 55000

The file "Contribution.txt" contains the same names and values as in exercise. The output is, it displays 4x no names and $0.

Here is the code:

    #include <iostream>
#include <fstream>  //file I/O support
#include <cstdlib>  //support for exit()
#include <string>

std::string name;

double contr;

int content;
int i=0;

int main()
{
    std::ifstream inFile;    // object for handling file input
    inFile.open("Contribution.txt");
    if (!inFile.is_open()) // failed to open file
    {
        std::cout <<"Could not open the file \"Contribution.txt\""<<std::endl;
        std::cout <<"Programing terminating.\n";
        exit(EXIT_FAILURE);
    }
    inFile>>content;
    if(inFile.good())
    {
        while(i<content)
        {
            getline(inFile,name);   //saves the line in string name
            inFile>>contr;  //saves value in double contr

            std::cout<<name<<std::endl;
            std::cout<<"$"<<contr<<std::endl;
            i++;
        }
    }
    if(inFile.eof())
        std::cout<<"End of file reached.\n"<<std::endl;
    else if(inFile.fail())
        std::cout<<"Input terminated by data mismatch.\n";
    else
        std::cout<<"Input terminated for unknown reason.\n";

    inFile.close();


    return 0;
}

When it finishes it returns:

Input terminated by data mismatch.

  • 4
    Because of http://stackoverflow.com/q/5605125/560648. Where did you learn to read from a file like that? – Lightness Races in Orbit Feb 12 '15 at 11:53
  • 3
    And fyi, *Always* verify the success of your data IO operations. Assumption is the mother of all.... – WhozCraig Feb 12 '15 at 11:53
  • 8
    Notice that [StackOverflow: The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) discourages from reading this book. (And I am personally agreed with it) – Ivan Aksamentov - Drop Feb 12 '15 at 11:54
  • 1
    Add a debug statement to see if `inFile.good()` is executed at all....Also, what value are you saving in contr, because you have already saved the line in name? Do you know if you have anything left in the file to read? – ha9u63ar Feb 12 '15 at 11:55
  • 3
    What everybody else said is not the only problem - after you do `inFile >> contr;`, a `\n` is still left in the file, so the next call to `getline` will read a blank line. – Joseph Mansfield Feb 12 '15 at 11:56
  • Thanks guys for answers, that's why I like this forum. @LightnessRacesinOrbit I am learning from Primer Plus as you can see, and I also ask if I've got some questions. Maybe I should change the method, I don't know, but I'll read about that. Thanks. – Jerzy Tuszyński Feb 12 '15 at 11:59
  • @Drop Thanks for this comment, now I'm wondering if I won't stop learning from this book. – Jerzy Tuszyński Feb 12 '15 at 12:25
  • @JerzyTuszyński: It's not a forum :P – Lightness Races in Orbit Feb 12 '15 at 12:56

0 Answers0