scanf 在 C 中读取整数后跳过,在 while 循环中 [英] scanf skipped after reading in integer in C, in while loop

查看:51
本文介绍了scanf 在 C 中读取整数后跳过,在 while 循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,用于检查 scanf 的输入是否有效.即如果 scanf 返回一个非零正数.这是我的代码的一部分:

I have a section of code where I check the input to scanf is valid. i.e. if scanf returns a non zero positive number. This is part of my code:

while(scanf(" %d",&choice)<=0){
        printf("Incorrect value entered, please re-enter\n");
    }

其中选择"是一个整数.

Where "choice" is an integer.

每次我运行这段代码时,编译器都会在 while 循环执行后跳过 scanf.我得到这样的输出:

Every time I run this code, the compiler skips past the scanf after the while loops is executed. I get an output like this:

欢迎使用捕食者/猎物计算器

Welcome to the Predator/Prey Calculator

请输入您的姓名

丹嗨丹

请选择以下选项之一:1. 计算一个典型的捕食者和猎物系统的进化2. 计算特定捕食者和猎物系统的进化3. 计算自定义捕食者和猎物系统的进化0. 退出一种输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值不正确,请重新输入输入的值有误,请重新输入

Please choose one of the following options: 1. Calculate the evolution of a TYPICAL predator and prey system 2. Calculate the evolution of a SPECIFIC predator and prey system 3. Calculate the evolution of a CUSTOM predator and prey system 0. Quit a Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter Incorrect value entered, please re-enter

你能解释一下为什么会发生这种情况吗!我似乎无法在互联网上找到任何特定于整数阅读的答案.

Could you explain why this happens! I can’t seem to find any answers on the internet specific to reading in integers.

非常感谢,

推荐答案

您的代码的问题在于,如果用户不输入数字,您的程序将永远循环.这是因为 scanf 会反复尝试解析相同的字符串并不断失败.您需要做的是匹配用户写的任何内容,然后再次询问一个数字:

The problem with your code is that in case the user does not input a number your program will loop forever. This is because scanf will repeatedly try to parse the same string and keep failing. What you have to do is to match whatever the user has written and then ask again for a number:

#include<stdio.h>

int main(){
    int choice;
    while(scanf("%d",&choice) <= 0){
        scanf("%*s"); // this will parse anything the user has written
        printf("Incorrect value entered, please re-enter\n");
    }
    return 0;
}

scanf 格式字符串中的 * 是赋值抑制字符,来自 scanf 手册页:

The * in the scanf format string is the assignment suppression character, from scanf man page:

'*' 赋值抑制字符:scanf() 读取输入按照转换规范的指示,但丢弃输入.不需要相应的指针参数,并且这个规范不包括在计数中成功的scanf() 返回的赋值.

'*' assignment-suppression character: scanf() reads input as directed by the conversion specification, but discards the input. No corresponding pointer argument is required, and this specification is not included in the count of successful assignments returned by scanf().

这篇关于scanf 在 C 中读取整数后跳过,在 while 循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆