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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:16:29+00:00 2026-06-15T10:16:29+00:00

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

  • 0

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

I was reading a group of characters via cin.get() and I noticed that my cin.get() was getting an exra character at the end of the input. Might anyone know how to fix this? Here’s my code:

unsigned char c;

while(!cin.eof())
{
   c = cin.get();
   cout << (int)c << endl;
}

My issue is that the character it gets is one of 255 ascii value. I simply don’t want it to get this extra character, but if the user enters in a ascii value of 255 without it being a garbage character at the end, then that should be fine. An example would be so for my output:

if I entered in abc\n in my output:

I get
97
98
99
10
255

but I want:
97
98
99
10

Any ideas on how to fix this? Thanks!

  • 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-15T10:16:30+00:00Added an answer on June 15, 2026 at 10:16 am

    Never use cin.eof() as a loop condition. It almost always produces buggy code, as it has here.

    Instead, try:

    int c;
    while ( (c=cin.get()) != EOF ) {
      cout << c << endl;
    }
    

    Or:

    char c;
    while (cin.get(c)) {
      cout << (int)c << endl;
    }
    
    • 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? I've got very
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: Program not waiting for cin I wrote the following code: #include <iostream>
Possible Duplicate: Same random numbers every loop iteration Here is my code: #include <iostream>
Possible Duplicate: Reading from text file until EOF repeats last line c++ EOF running
Possible Duplicate: Why is this cin reading jammed? I overloaded the istream operator (
Possible Duplicate: How is std::iostream buffered? This might sound ridiculous, but how can I
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class

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.