qq_45735316 2020-05-20 18:37 采纳率: 94.1%
浏览 288
已采纳

这段代码真的能将开头为非数字的整行输入丢弃吗?为什么跑出来的结果还是不对的?

图片说明图片说明图片说明

#include <stdio.h>
#include <stdlib.h>
#define N 5
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    long input;
    char ch;
    while(scanf("%ld",&input)!=1)
    {
        while((ch=getchar())!='\n')
             putchar(ch);
    }
    printf("%ld",input); 
    return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-21 00:04
    关注

    可以啊

    #include <stdio.h>
    #include <stdlib.h>
    #define N 5
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char *argv[]) {
        long input;
        char ch;
        while(scanf("%ld",&input)!=1)
        {
            while((ch=getchar())!='\n')
                 ;//putchar(ch); //这里的输出只是告诉你你输入了什么,实际使用应该去掉
        }
        printf("%ld",input); 
        return 0;
    }
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog