10

如果用户为整数变量输入浮点数,我想打印无效输入。那可能吗?

 int a;
 scanf("%d",&a); // if user enters 4.35 print invalid input 

我试过这样的角色

  if(scanf("%d",&a)==1);
  else printf("invalid input");

但是如何处理浮点数。如果用户输入4.35它会截断4但我想要无效的输入。

4

9 回答 9

11

由于小数点前任何数字的浮点数的开头看起来像一个整数,因此无法单独检测到这一点%d

您可能会考虑阅读整行内容,fgets()然后使用以下内容进行分析sscanf()

int a;
int n;
char line[4096];
if (fgets(line, sizeof(line), stdin) != 0 && sscanf(line, "%d%n", &a, &n) == 1)
   ...analyze the character at line[n] for validity...

(是的,我的意思是与 1 进行比较;%n转换规范不计入sscanf()等人的返回值中。)

scanf()该代码没有做的一件事是在输入数字之前跳过空白行。如果这很重要,您必须编写一个循环来读取(非空)行,然后解析非空行。您还需要确定线路上可以容忍多少尾随垃圾(如果有)。允许空格吗?标签?字母字符?标点?

于 2013-08-20T15:46:32.403 回答
4

您必须将其读取为双精度,然后检查它是否为整数。检查它是否为整数的最佳方法是使用modf,它返回双精度的小数部分。如果有一个你有一个错误:

double d;
scanf("%lf", &d);

double temp;
if(modf(d, &temp)){
  // Handle error for invalid input
}

int a = (int)temp;

这将允许小数点后只有 s 的整数或浮点数,0例如54.00000. 如果您也想将其视为无效,则最好逐个字符地阅读并验证每个字符是否在0和之间9(ascii 48 到 57)。

于 2013-08-20T15:47:12.760 回答
3

如果不通过阅读int以查看是什么停止了扫描,则无法做到这一点。

经典成语

char buf[100];
if (fgets(buf, sizeo(buf), stdin) == NULL) {
  ; // deal with EOF or I/O error
}
int a;
char ch;
if (1 != sscanf(buf, "%d %c", &a, &ch)) {
  ; // Error: extra non-white space text
}
于 2013-08-20T15:56:51.327 回答
2

您可以使用strtol()strtod()比较结束指针来做到这一点,例如:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char buffer[100];
    char * endptr_n;
    char * endptr_d;
    long n;
    double d;

    fgets(buffer, 100, stdin);
    n = strtol(buffer, &endptr_n, 10);
    if ( endptr_n == buffer ) {
        fputs("You didn't enter a number.", stderr);
        return EXIT_FAILURE;
    }

    d = strtod(buffer, &endptr_d);

    if ( *endptr_d == '\0' || *endptr_d == '\n' ) {
        if ( endptr_d == endptr_n ) {
            puts("You entered just a plain integer.");
        } else {
            puts("You entered a floating point number - invalid.");
        }
    } else {
        puts("You entered garbage after the number - invalid.");
    }

    return EXIT_SUCCESS;
}

输出:

paul@local:~/src/c$ ./testint
2
You entered just a plain integer.
paul@local:~/src/c$ ./testint
2.3
You entered a floating point number - invalid.
paul@local:~/src/c$ ./testint
3e4
You entered a floating point number - invalid.
paul@local:~/src/c$ ./testint
4e-5
You entered a floating point number - invalid.
paul@local:~/src/c$ ./testint
423captainpicard
You entered garbage after the number - invalid.
paul@local:~/src/c$

它不使用scanf(),但这是一件好事,它避免了手动检查您读取的整数之后的输入的需要。

显然,如果线路上唯一的东西是数字,那么很多这些就变得不必要了,因为你可以立即打电话strtol()并检查*endptr_n,但如果线路上可能有其他东西,你可以这样做,例如,如果你想接受一个整数,后跟任何非数字,但不是浮点数,后跟相同的东西,你可以删除if ( *endptr_d == '\0' || *endptr_d == '\n' )逻辑。

编辑:更新代码以显示检查到*endptr.

于 2013-08-20T23:14:32.970 回答
1

This one is bit easier:

#include <stdio.h>

int main() 
{
    int a;
    long double b;

    scanf("%f",&b);
    a = (int) b;

    a == b ? printf("%d\n",a) : printf("Invalid input!");

    return 0;
} 

Input: 4
Output:

4

Input: 4.35
Output:

 Invalid input
于 2013-08-20T16:00:16.603 回答
1

这是一个简单的方法:

#include <stdio.h>

int main(int argc, char **argv) {
    int d;
    printf("Type something: ");

    // make sure you read %d and the next one is '\n'
    if( scanf("%d", &d) == 1 && getchar() == '\n' ) {
        printf("%d\n", d);
    }

    return 0;
}

.

$ a.exe
Type something: 312312.4214

$ a.exe
Type something: 2312312
2312312

$ a.exe
Type something: 4324.

$
于 2013-08-20T17:15:21.730 回答
1

首先,没有任何问题scanf。当用户输入一个浮点数时,他们实际上输入了一个数字点号。因此,编写一个 scanf 代码来检测该数据条目。

main()
  {
       char c1[2];
       int num1;

       int nr_nums;

       nr_nums = scanf("%d%1[.e0123456789]", &num1, &c1);

       if (nr_nums == 1) {printf("\ndata = %d", num1);}
       if (nr_nums == 2) {printf("\nInvalid");}

 }

按照注释的建议,按照另一种可能的数据输入格式 1. 或 3e-1 修改了此代码。

此代码可以满足您的基本要求。它接受整数数据输入并检测何时输入浮点数。

于 2013-08-20T20:47:01.247 回答
0

如果您将数字表示为字符串(当您使用 fgets 时),您可以通过它运行 for 循环并将每个字符与“.”进行比较。

于 2016-04-01T03:18:07.933 回答
-1

我可以看到的另一种选择如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{

 char mystring[31];
 scanf("%30s[0-9]\n", mystring);
 int mynumber = atoi(mystring);
 printf("here is your integer: %d", mynumber);
 getchar();
 getchar();
 return 0;











 }
于 2019-01-03T01:32:21.793 回答