在C / C ++中清除输入缓冲区

笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。

CSDN视频网址:http://edu.csdn.net/lecturer/144

     什么是缓冲区?

临时存储区称为缓冲区,所有标准输入和输出设备都包含输入和输出缓冲器。在标准C / C ++中,流是缓冲的,例如在标准输入的情况下,

当我们按键盘上的键时,它不会发送到您的程序,而是由操作系统缓冲,直到时间被分配给程序。

它如何影响编程?

在各种情况下,您可能需要清除不需要的缓冲区,以便获得所需容器中的下一个输入,而不是在前一个变量的缓冲区中。例如,
在遇到“scanf()”之后的C的情况下,如果我们需要输入一个字符数组或字符,并且在C ++的情况下,遇到“cin”语句之后,我们需要输入一
个字符数组或一个字符串,我们需要清除输入缓冲区,否则所需的输入由前一个变量的缓冲区占用,而不是所需的容器。在第一次输入后按
输出屏幕上的“回车”(回车),前一个变量的缓冲区为新容器的空间(因为我们没有清除它),程序跳过容器的以下输入。

C编程的情况下

#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 not print character 'a'
    printf("%c", ch);
     
    return 0;
}

输入:

TestGame
一个

输出:

TestGame

   在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 from 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;
}

输入:

4
TestGame

输出:

4
在上述两种代码中,都不会根据需要打印输出。 原因是被占用的缓冲区。 “\ n”字符保留在缓冲区中,并作为下一个输入读取。

如何解决?

在C:

1、使用“while((getchar())!='\ n'); (键入)while((getchar())!='\ n');”读缓冲区字符直到结束并丢弃它们(包括换行符),

并在“scanf()”语句清除输入缓冲区之后使用它允许输入到所需的容器中。

#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 will print 'a' this time
    printf("%c", ch);
 
    return 0;
}

输入:

TestGame
一个

输出:

TestGame
一个
2、 使用“fflush(stdin)” :在“scanf()”语句之后键入“fflush(stdin)”也会清除输入缓冲区,但是避免使用它,并且根据C ++被称为输入流“未定义” 11标准。

在C ++的情况下:

1、使用“cin.ignore(numeric_limits :: max(),'\ n'); : - 在”cin“语句之后输入”cin.ignore(numeric_limits :: max(),'\ n');“后,放弃包含换行符的输入流中的所有内容。
#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, 80);
     
    // Prints 4
    cout << a << endl;
     
    // Printing string : This will print string now
    cout << str << endl;
 
    return 0;
}

输入:

4
TestGame

输出:

4
TestGame
2、 使用“cin.sync()”: 在“cin”语句之后键入“cin.sync()”将放弃缓冲区中的所有内容。虽然“cin.sync()”  不工作 在所有实施(根据C ++ 11和上述标准)。
#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, 80);
     
    // Prints 4
    cout << a << endl;
     
    // Printing string - this will print string now
    cout << str << endl;
 
    return 0;
}

输入:

4
TestGame

输出:

4
TestGame
使用“cin >> ws”: 在“cin”语句之后键入“cin >> ws”,告诉编译器忽略缓冲区,并且在字符串或字符数组的实际内容之前丢弃所有的空格。


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海洋_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值