How to test input is sane

后端 未结 6 1126
一个人的身影
一个人的身影 2020-12-11 11:38

Consider the following simple C program.

//C test

#include

int main()
{
   int a, b, c;

   printf(\"Enter two numbers to add\\n\");
   scan         


        
6条回答
  •  旧巷少年郎
    2020-12-11 12:21

    Also you can do this to prevent anything after second number

    int a,b;
    char c;
    if( scanf("%d%d%c", &a, &b, &c) == 3) {
        if (c == '\n') {
            puts("good");
        }
    } else {
        puts("bad");
    }
        return 0;
    }
    

提交回复
热议问题