Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Clearing The Input Buffer In C/C++

// 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
", str);
     
    // This does not print
    // character 'a'
    printf("%c", ch);
     
    return 0;
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #Clearing #The #Input #Buffer #In
ADD COMMENT
Topic
Name
2+8 =