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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:30:38+00:00 2026-05-22T19:30:38+00:00

Considering the following: char c; cin >> c; cin.unget(); Assuming the char input was

  • 0

Considering the following:

char c;
cin >> c;
cin.unget();

Assuming the char input was successful, is unget guaranteed to be able to back up at least that one character? If I ask for, and successfully get a string, can I be guaranteed to be allowed to call unget all the way to the beginning of that string?

  • 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-22T19:30:39+00:00Added an answer on May 22, 2026 at 7:30 pm

    You are guaranteed to be able to unget at least 1 character. Any more than one is up to the implementation and circumstances, so you shouldn’t assume that you can unget more than one.

    EDIT: Sorry I was thinking of libc’s int unget(int ch, FILE *stream). Which the standard says:

    One character of pushback is
    guaranteed. If the ungetc function is
    called too many times on the same
    stream without an intervening read or
    file positioning operation on that
    stream, the operation may fail.

    I will see if I can find exactly what is said about basic_istream<>& unget()

    EDIT: OK, so here’s what the c++ standard says about basic_istream<>& unget() (bolding added by me):

    Behaves as an unformatted input
    function (as described in 27.6.1.3,
    paragraph 1). After constructing a
    sentry object, if !good() calls
    setstate(failbit) which may throw an
    exception, and return. If rdbuf() is
    not null, calls rdbuf()->sungetc(). If
    rdbuf() is null, or if sungetc()
    returns traits::eof(), calls
    setstate(badbit) (which may throw
    ios_base::failure (27.4.4.3)).

    So the important bit is that it calls sungetc(), so let’s see what the standard says about that:

    If the input sequence putback position
    is not available, returns pbackfail().
    Otherwise, decrements the next pointer
    for the input sequence and returns
    traits::to_int_type(*gptr()).

    I see nothing that explicitly states a limitation, so it’s worth a try. If I understand this correctly, it will adjust pointers in the backing stream buffer. So as long as there is enough “history” in the buffer, it should continue to succeed.

    Unlike C, however, it seems that you are never guaranteed that it will work, but you will likely be able to putback more than one character.

    So my advice is to not depend on putting back more than one character, and always check for failure.

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

Sidebar

Related Questions

Considering the following line: char *p = malloc( sizeof(char) * ( len + 1
Considering the following two usage scenarios (exactly as you see them, that is, the
Considering following array and two series of assignments: char charArray[3]; charArray[0]='a'; charArray[1]='b'; charArray[2]='c'; char
Considering the following : http://jsfiddle.net/Johnny5/EC8kK/ I would like the input button to align right
Considering following code public class A { public static void main(String[] args) { new
Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and a
Considering the following table: someId INTEGER #PK ageStart TINYINT(3) ageEnd TINYINT(3) dateBegin INTEGER dateEnd
considering the following enum: public enum LeadStatus { Cold = 1, Warm = 2,
I'm considering the following: I have some data stream which I'd like to protect
Considering this code, can I be absolutely sure that the finally block always executes,

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.