-1

i wrote the following code....

#include< iostream>
#include< fstream>
using namespace std;  
int main()  
{   
ifstream in("text1.dat",ios::in);    
enum choice{zero=1, credit, debit, exit};  
choice your;  
int balance;  
char name[50];  
int option;  
while(cin>>option)  
{
if(option==exit)  
 break;

switch(option)  
 {case zero:
     while(!in.eof())
     {in>>balance>>name;
      if(balance==0)
      cout<<balance<<" "<<name<<endl;
      cout<<in.tellg()<<endl;
     }   
     in.clear(); 
     in.seekg(0);
     break;}

// likewise there are cases for debit and credit

system("pause");
return 0;   
}    

In text1.dat the entry was:

10 avinash  
-57 derek  
0 fatima  
-98 gorn  
20 aditya

and the output was:

1 //i input this  
16  
27  
0 fatima  
36  
45  
55  
-1  //(a)  
3 //i input this  
10 avinash  
16  
27  
36  
45  
20 aditya  
55  
20 aditya //(b) 
-1  

my questions are:

  1. the output marked 'a' is -1...what does -1 mean as an output of tellg()?
  2. the output marked 'b' is repeated...why so?
Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
AvinashK
  • 3,106
  • 5
  • 38
  • 85
  • It is some failure. http://www.cplusplus.com/reference/iostream/istream/tellg/ – balki Jun 28 '11 at 14:39
  • 2
    Code + Output + Why? != Good SO Question – AJG85 Jun 28 '11 at 14:58
  • @balki that is too short an explaination...i know that failure returns -1, but i have put the condition that when EOF is confronted loop shall terminate...but u can see even after 55 is output(that is after aditya containing statement is input into the stream) the loop doesn't terminate but gives one more output -1...and the same is repeated when i enter 3...and above all i asked two ques...u should have answered both satisfactorily before downvoting me – AvinashK Jun 28 '11 at 14:59

1 Answers1

2

You are observing the same behavior as many other novice C++ programmers. Read for example this question.

What happens is that in.eof() is set to true after you've tried to read something from in and the operation failed because there was no more data. When a read operation fails due to end-of-file, it sets both, eofbit and failbit. When a stream is in fail state, the tellg function is documented to return -1.

To solve the problem, test for eof after you perform a read operation and before you do anything else. Even better, check that the operation just 'failed', since you don't want to distinguish between an end-of-file and an incorrect input (e.g. if a string is fed instead of an number for the balance, your code enters an infinite loop):

for(;;)
{
  in>>balance>>name;
  if(!in)
    break;
  if(balance==0)
    cout<<balance<<" "<<name<<endl;
  cout<<in.tellg()<<endl;
}

The !in condition checks that either failbit or badbit are set. You can simplify this by rewriting as:

while(in>>balance>>name)
{
  if(balance==0)
    cout<<balance<<" "<<name<<endl;
  cout<<in.tellg()<<endl;
}
Community
  • 1
  • 1
Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
  • @avinash: your second question is impossible to answer because we don't have the code for `debit`. – Yakov Galka Jul 03 '11 at 13:58
  • the code for debit and credit are exactly the same as zero...the only difference being that for debit we check if(balance>0) and for credit we check if(balance<0)....i have made an observation which can help you....aditya was repeated when i put aditya at last in the text file...initially i had put gorn at last and then when i pressed 2(credit=negative balance) derek was output once and then gorn got repeated 2 times – AvinashK Jul 03 '11 at 18:54
  • @avinash: at least format your questions properly, otherwise it's impossible to read. The answer to your second question is the same. You print the values even when the operation fails due to EOF. But when the operation fails the old values are unchanged, therefore the last line read is repeated. – Yakov Galka Jul 04 '11 at 06:34