Clear the input buffer in C + +

Source: Internet
Author: User
Tags stdin

The author introduces: Jiang Xiewei, IT company technical partner, it senior lecturer, CSDN Community experts, invited editors, best-selling authors, published books: "Hands-on teaching you architecture 3D Game Engine" Electronics publishing house and "Unity3d actual combat core technical details" Electronic industry publishing house and so on.

CSDN Video URL: http://edu.csdn.net/lecturer/144 what is a buffer.

The temporary storage area is called a buffer, and all standard input and output devices contain input and output buffers. In standard C/s + +, the stream is buffered, for example, in the case of standard input,

When we press the key on the keyboard, it is not sent to your program, but is buffered by the operating system until the time is assigned to the program. how it affects programming. in various cases, you may need to clear the unwanted buffer to get the next input in the desired container, rather than in the buffer of the previous variable. For example, in the case of C after "scanf ()", if we need to enter a character array or character, and after the "CIN" statement in the case of C + +, we need to enter a character array or a string, we need to clear the input buffer, Otherwise, the required input is occupied by the buffer of the previous variable, not the desired container. After the first input, press RETURN (enter) on the output screen, the buffer of the previous variable is the space of the new container (because we did not clear it), the program skips the following input of the container. in the case of C programming

#include <stdio.h>
int main ()
{
    char str[80], ch;
     
    Scan input from User-testgame for example
    scanf ("%s", str);
     
    Scan character from user-' A ' for example
    ch = getchar ();
     
    Printing character array, prints "testgame")
    printf ("%s\n", str);
     
    This does is not print character ' a '
    printf ("%c", ch);
     
    return 0;
}

Input:

Testgame
One

Output:

Testgame

in the case of C + +

#include <iostream>
#include <vector>
using namespace std;
int main ()
{
    int A;
    Char ch[80];
     
    Enter input from User-4 for example
    cin >> A;
     
    Get input to User-"Testgame" for example
    cin.getline (ch,80);
     
    Prints 4
    cout << a << Endl;
     
    Printing String:this does not print string
    cout << ch << endl;
 
    return 0;
}

Input:

4
Testgame

Output:

4
In both of these codes, the output is not printed as needed. The reason is the occupied buffer. The "\ n" character is kept in the buffer and read as the next input.

how to solve.

in C:

1. Use "while" (GetChar ()). = ' n '); (type) while (GetChar ()). = ' n '); Read buffer characters until they are finished and discarded (including line breaks).

and use it to allow input to the desired container after the "scanf ()" statement clears the input buffer.

#include <stdio.h>
int main ()
{
    char str[80], ch;
     
    Scan input from User-testgame for example
    scanf ("%s", str);
     
    Flushes the standard input (clears the input buffer) while
    ((GetChar ())!= ' \ n ')
     ;
    Scan character from user-' a ' for example
    ch = getchar ();
     
    Printing character array, prints "testgame")
    printf ("%s\n", str);
     
    Printing character a:it'll print ' A ' This time
    printf ("%c", ch);
 
    return 0;
}

Input:

Testgame
A

Output:

Testgame
A
2. Use "Fflush (stdin)": Typing "fflush (stdin)" After the "scanf ()" statement also clears the input buffer, but avoids using it, and according to C + + is called the "undefined" 11 standard for the input stream. in the case of C + +:1, the use of "Cin.ignore (numeric_limits:: Max (), ' n '"); ":-Enter" Cin.ignore (numeric_limits:: Max (), ' \ n ') after the "CIN" statement; , discard all content in the input stream that contains the line feed.
#include <iostream>
#include <ios>     /for <streamsize>
#include <limits>  // For Numeric_limits
using namespace std;
int main ()
{
    int A;
    Char str[80];
     
    Enter input from User-4 for example
    cin >> A;
     
    Discards the input buffer
    Cin.ignore (Numeric_limits<streamsize>::max (), ' \ n ');
     
    Get input from User-testgame for example
    cin.getline (str,);
     
    Prints 4
    cout << a << Endl;
     
    Printing String:this'll print string now
    cout << str << endl;
 
    return 0;
}

Input:

4
Testgame

Output:

4
Testgame
2, use "Cin.sync ()": After the "CIN" statement type "Cin.sync ()" will discard all content in the buffer. Although "Cin.sync ()" does not work at all implementations (according to C + + 11 and above standards).
#include <iostream>
#include <ios>     
#include <limits>  
using namespace std;
int main ()
{
    int A;
    Char str[80];
     
    Enter input from User-4 for example
    cin >> A;
     
    Discards the input buffer
    cin.sync ();
     
    Get input from User-testgame for example
    cin.getline (str,);
     
    Prints 4
    cout << a << Endl;
     
    Printing String-this'll print string now
    cout << str << endl;
 
    return 0;
}

Input:

4
Testgame

Output:

4
Testgame
Use CIN >> WS: Type "cin >> ws" After the "CIN" statement, telling the compiler to ignore the buffer, and discard all spaces before the actual contents of the string or character array.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.