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

  • Home
  • SEARCH
  • 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 7974269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:19:33+00:00 2026-06-04T08:19:33+00:00

Given this code: #include <cstdio> #include <iostream> #include <string> using std::cin; using std::cout; using

  • 0

Given this code:

#include <cstdio>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;

int main() {
        int a;
        string b;

        cin >> a;
        cin >> b;

        return 0;
}

I tried compiling it with g++ and running it.
When assigning a char to a, at the first cin, the following instruction seems to be skipped.

Even if add two getchar() instructions between the last two lines, only the second getchar() seems to be executed.
Can somebody accurately explain what’s happening at low level, which seemingly results in an apparent non execution of those lines?

EDIT:

Using this debug code:

#include <cstdio>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
        int a;
        string b;

        cin >> a;
        cin >> b;
        cout << "a is "<< a << endl;
        cout << "b is "<< b << endl;
        getchar();

        return 0;
}

INPUT
1test

OUTPUT
a is 1
b is test
* No getchar executed *

INPUT
1
test

OUTPUT
a is 1
b is test

INPUT
ttest

OUTPUT
a is 0
b is

INPUT
t

// Skips the second cin

OUTPUT
a is 0
b is

NOTE:
getchar() was not executed even once.

  • 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-04T08:19:34+00:00Added an answer on June 4, 2026 at 8:19 am

    Two things, judging from your output. The first is when you enter
    "ttest", the cin >> a; fails. This puts cin in an error state,
    where it will remain until the error is cleared. And as long as it is
    in an error state, all other operations are no-ops. You really need to
    test the results of the input before trying to use the values:

    std::cin >> a;
    if ( !cin ) {
        std::cerr << "That wasn't an integer" << std::endl;
        std::cin.clear();
    }
    std::cin >> b;
    if ( !cin ) {
        std::cerr << "Where was the string" << std::endl;
        std::cin.clear();
    }
    

    (And don’t use a non-initialized variable, like a, until it has been
    successfully input.)

    The second is that the >> operator only extracts the characters
    necessary for its target: >> to an int will stop at the first
    non-numeric character, and >> to a std::string at the first white
    space (in both cases, after having skipped leading white space). This
    means that after something like "1test\n", there will still be a
    '\n' in the buffer. And while it’s generally a bad idea to mix
    FILE* (like getchar()) and iostream, if they’re correctly
    synchronized, getchar() will read this '\n' immediately and return.

    If you’re reading line oriented input, the best solution is to use
    getline(), and then put the line into a std::istringstream to parse
    it. So your code might end up looking like:

    std::string line:
    std::getline(std::cin, line);
    if ( ! std::cin ) {
        //  Something unexpected went wrong...
        std::cin.clear();
    } else {
        std::istringstream l( line );
        l >> a >> b;
        if ( !l ) {
            //  Format error in input...
        } else {
            //  use your data here...
        }
    }
    std::cin.get();  //  Wait for one more character...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code: #include <iostream> #include <cstdio> #include <fstream> #include <string> int main() { std::remove(test.txt);
Given this code: #include <iostream> using namespace std; class Foo { public: Foo ()
Why this code does not work ? #include <iostream> #include <cstdio> int main() {
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Given this C code compiled with gcc 4.3.3 #include <stdio.h> #include <stdlib.h> int main(int
Given this code: #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using
Given this code sample: complex.h : #ifndef COMPLEX_H #define COMPLEX_H #include <iostream> class Complex
I have a program that has this code : #include<stdio.h> main(){ int input; char
Given the following code: #include <iostream> struct implicit_t { implicit_t(int x) : x_m(x) {
I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);

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.