gpt4 book ai didi

c - 当用户输入不正确的值以避免程序崩溃时,C 中的数据验证

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:23 29 4
gpt4 key购买 nike

如何确保用户只输入一个整数值以使程序不会崩溃?

如果用户输入的不是整数,我想:printf("请重新检查您的输入);

printf("How many values do you want to enter? \t");
int g;
scanf("%d", &g);

最佳答案

我会说,检查 scanf 的返回值:

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.

关于c - 当用户输入不正确的值以避免程序崩溃时,C 中的数据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25600370/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com