Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:07:49+00:00 2026-05-23T13:07:49+00:00

i wrote the following code…. #include< iostream> #include< fstream> using namespace std; int main()

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:07:50+00:00Added an answer on May 23, 2026 at 1:07 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following code in a JSP file: <c:catch var=e> <% int accountNumber
In C++Builder, I wrote the following code (in Button1Click handler), When I run in
I needed some simple string encryption, so I wrote the following code (with a
I've got the following bit of code (that another developer wrote) that I need
i wrote following code to do some of a specifice column of gridview.. but
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code to make my toolbar transparent. [mtoolbar setBackgroundColor:[UIColor clearColor]]; How
I wrote the following code to set the site as the homepage: document.body.style.behavior='url(#default#homepage)'; document.body.setHomePage('http://www.abc.com');
How can I write the following code quickly in emacs? \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.