i have been having problems inputting some data from a file and i am clueless as to why i keep getting the output i do.

This is a partial part of my code but as you can see from my output for some unkown reason I am not inputting an entire line when i use the getline function

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

string title;
string author;
string call_number;  
string status;
char C;

ifstream inFile;
inFile.open("FileName.txt");

while(!inFile.eof())
	{
	getline(inFile, title);
	getline(inFile, author);
	inFile >> C;
	inFile >> call_number;
	inFile >> status;
	inFile.ignore('\n');  //used to skip a line
	
	
	cout << title << endl;
	cout << author << endl;
	cout << call_number << endl;
	cout << status << endl;
                }

here is the file i am reading from

Absolute C++ (3rd Edition) 
Walter Savitch 
C1
1 

Bello, Topics In Contemporary Math Ninth Edition 
Ignacio Bello, Jack R. Britton, and Anton Kaul 
C2
1 

C++ Primer Plus (5th Edition) 
Stephen Prata 
C3
0 

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 
D.S. Malik 
C4
1

and the output i get is excluding the letters from only the first line.

Absolute C++ (3rd Edition)
Walter Savitch
1
1

Topics In Contemporary Math Ninth Edition
Ignacio Bello, Jack R. Britton, and Anton Kaul
2
1

mer Plust (5th Edition)  //notice the missing letters :(
Stephen Prata
3
0

gramming: From Problem Analysis to Program Design, Fourth Edition
D.S. Malik
4
1

Recommended Answers

All 3 Replies

>>while(!inFile.eof())
wrong way to code that loop while( getline(inFile, title) )

>>while(!inFile.eof())
wrong way to code that loop while( getline(inFile, title) )

can someone plz explain what that condition means. Also simply the condition of that loop does not completly solve my problem. I am still not getting the correct output

getline() returns *this or NULL if end-of-file.

This seems to work. I changed it to use all std::string and getline(). See this thread for the code to flush the '\n'

while(getline(inFile, title))
{
    getline(inFile, author);
    getline(inFile,call_number);
    call_number = call_number.substr(1);
    getline( inFile, status);
    inFile.ignore ( std::numeric_limits<std::streamsize>::max(), inFile.widen ( '\n' ) );
	
    cout << title << endl;
    cout << author << endl;
    cout << call_number << endl;
    cout << status << endl << "\n";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.