Clear The Input Buffer In Cpp

[Solved] Clear The Input Buffer In Cpp | C - Code Explorer | yomemimo.com
Question : clear the input buffer in cpp

Answered by : red-team

cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

Source : https://gist.github.com/leimao/418395bf920eb70b2b11fe89d7c1f738 | Last Update : Thu, 31 Mar 22

Question : Clearing The Input Buffer In C/C++

Answered by : travinth-dayalaeaswaran

// C Code to explain why not
// clearing the input buffer
// causes undesired outputs
#include<stdio.h>
int main()
{
    char str[80], ch;
     
    // Scan input from user -
    // GeeksforGeeks for example
    scanf("%s", str);
     
    // Scan character from user-
    // 'a' for example
    ch = getchar();
     
    // Printing character array,
    // prints “GeeksforGeeks”)
    printf("%s\n", str);
     
    // This does not print
    // character 'a'
    printf("%c", ch);
     
    return 0;
}

Source : https://www.geeksforgeeks.org/clearing-the-input-buffer-in-cc/?ref=lbp | Last Update : Fri, 15 Jul 22

Answers related to clear the input buffer in cpp

Code Explorer Popular Question For C