Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Question on ifstream!

Reply
Thread Tools

C++ Question on ifstream!

 
 
Jupiter5F
Guest
Posts: n/a
 
      11-16-2003
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();
}



 
Reply With Quote
 
 
 
 
Russell Hanneken
Guest
Posts: n/a
 
      11-16-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove the 'g' from my address to send me mail.



 
Reply With Quote
 
 
 
 
Jupiter5F
Guest
Posts: n/a
 
      11-16-2003
Thank you Russell! Your solution has cleared the problem!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments