繁体   English   中英

如何从用户c获取输入整数

[英]How to get input integer from user c

我想从用户输入中获取矩阵值,行和列。 因此,我正在执行“边do while操作:这仅适用于行:

do {
        printf ("Inserisci il numero di righe ( intero-positivo-diverso da 0): ");
        scanf ("%d",&righe);
    } while (righe<=0);

我想检查用户仅插入整数。 我能做什么?

scanf返回一个整数,指示成功读取了多少个“事物”。 您可以有条件地对此进行测试,以查看它是否满足您的要求。 例:

const int result = scanf ("%d",&righe);
if (1 != result) {
  /* didn't get the 1 input we were looking for, do something about it! */
}

您将要区分EOF,尽管不是整数。

检查scanf()的返回值,它应该返回转换后的字符串数,如果它是!= 1,则无法将输入转换为整数。

参考: scanf

您正在寻找此ctype.h

您的代码是正确的。 是您想要做的正确方法...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM