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 4126664
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:03:43+00:00 2026-05-21T00:03:43+00:00

I have this text sample Ahmed 10 kmal 5 doola 6 And I am

  • 0

I have this text sample

Ahmed 10
kmal 5
doola 6

And I am using this code to read it

if (myfile.is_open())
{
  while ( myfile.good() )
{

    myfile >> name;
    myfile >> phone;
    cout << name <<" "<<phone<<endl;

}
myfile.close();

}

I get this output

Ahmed 10
kmal 5
doola 6
doola 6

Why does this code read the last line twice ?

  • 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-21T00:03:44+00:00Added an answer on May 21, 2026 at 12:03 am

    Try

    while( myfile >> name >> phone ) {
        // your code here
    }
    

    I believe the problem with the other approach is that eof isn’t signaled until you actually try to read more than you should. That is, when you attempt to myfile >> name on the last round. That fails, as does myfile >> phone and only then do you break out of the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sample text, which is retrieved from the class name on an
I have this sample model working with the admin class Author(models.Model): name = models.CharField(_('Text
I currently have this piece of code: NSString *strURL = [NSString stringWithFormat:@http://www.sample.com /phpFile.php?firstname=%@,txtfirstName.text]; NSString
I have this text read into an NSString *responce which I am trying to
I have this little text <div> Hello world<br> this is a good<br> text! <br><br>
I have this kinda template text : Hello {#Name#}, Thanks for coming blah on
I have this sample MVC project using jQuery grid. There is only one problem
I have this simple problem. I'll try to explain with a sample code An
I have this sample page: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8> <title>Ajax Page</title> <script
I have this below code(this is a sample, there are many more other conditions

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.