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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:23:58+00:00 2026-06-14T05:23:58+00:00

Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? I’ve got very

  • 0

Possible Duplicate:
Why is iostream::eof inside a loop condition considered wrong?

I’ve got very simple question…

#include<iostream>
using namespace std;
int main()
{
    char x;
    while(!cin.eof())
    {
        cin>>x;
        cout<<"char: "<< x << endl;
    }
    return 0;
}

Why when i try to run this code on Linux:
./file_name < test_file.txt
The result is:
char: a
char: b
char: c
char: d
char: d
when test_file.txt is only:
abcd

  • 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-06-14T05:23:59+00:00Added an answer on June 14, 2026 at 5:23 am

    End of file is detected by failing an input operation.

    So, in

        cin>>x;
        cout<<"char: "<< x << endl;
    

    the output statement is executed even when the input operation fails.

    And when it fails, it doesn’t update x.

    Instead of testing .eof(), test .fail().

    You can do that by using the stream object directly as a condition, e.g.

    #include<iostream>
    using namespace std;
    int main()
    {
        char x;
        while( cin >> x )
        {
            cout<<"char: "<< x << endl;
        }
    }
    

    Here the expression cin >> x performs an input operation that might update x, and as its expression result returns a reference to the stream, i.e. to cin. So cin is being used directly as a condition. That invokes a conversion to boolean, which is defined such that it on its own is equivalent to !cin.fail() (i.e., the expression cin >> x as condition is equivalent to writing !(cin >> x).fail() or, as a comma expression, (cin >> x, !cin.fail())).

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

Sidebar

Related Questions

Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? So I've been
Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? im having a
Possible Duplicate: .eof() loop not working So I have a fairly simple C++ program
Possible Duplicate: Same random numbers every loop iteration Here is my code: #include <iostream>
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
Possible Duplicate: Parentheses at the end of a C++11 lambda expression #include <iostream> int
Possible Duplicate: Struct Padding The program is as below: #include <iostream> using namespace std;
Possible Duplicate: Can a local variable’s memory be accessed outside its scope? #include <iostream>
Possible Duplicate: Why do I need to include both the iostream and fstream headers
Possible Duplicate: Virtual Functions Object Slicing let's consider: #include <vector> #include <iostream> using namespace

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.