C 在不退出程序并重新启动的情况下检查输入类型

C 在不退出程序并重新启动的情况下检查输入类型,c,C,我需要得到两个整数,然后加上并打印增加的值。我写了一个工作程序。另一个要求是检查输入值是否不是整数,如果不是整数,在不关闭程序的情况下,它应该再次请求输入 C代码 #include <stdio.h> void main() { int a,b,c; printf("This is a Addition program"); printf("\n Enter value of a:"); scanf("%d",&a); print

我需要得到两个整数,然后加上并打印增加的值。我写了一个工作程序。另一个要求是检查输入值是否不是整数,如果不是整数,在不关闭程序的情况下,它应该再次请求输入

C代码

#include <stdio.h>

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

    printf("This is a Addition program");

    printf("\n Enter value of a:");
    scanf("%d",&a);
    printf("\n Enter value of b:");
    scanf("%d",&b);

    c=a+b;

    printf("\n The added value is %d",c);
}
#包括
void main()
{
INTA、b、c;
printf(“这是一个添加程序”);
printf(“\n输入a的值:”);
scanf(“%d”和“&a”);
printf(“\n输入b的值:”);
scanf(“%d”和“b”);
c=a+b;
printf(“\n附加值为%d”,c);
}

此程序将执行您想要的操作

#include<stdio.h>
int main()  //use int not void
{
    int a,b,c;

    printf("This is a Addition program");

    printf("\n Enter value of a:");
    while(scanf("%d",&a)==0)
    {
        printf("\n Invalid input.Try again:");
        getchar(); // clear the previous input
    }
    printf("\n Enter value of b:");
    while(scanf("%d",&b)==0)
    {
        printf("\n Invalid input.Try again:");
        getchar(); // clear the previous input
    }
    c=a+b;

    printf("\n The added value is %d",c);

    return 0;  // because main returns int
}
#包括
int main()//使用int not void
{
INTA、b、c;
printf(“这是一个添加程序”);
printf(“\n输入a的值:”);
而(scanf(“%d”,&a)==0)
{
printf(“\n无效输入。请重试:”;
getchar();//清除以前的输入
}
printf(“\n输入b的值:”);
而(scanf(“%d”,&b)==0)
{
printf(“\n无效输入。请重试:”;
getchar();//清除以前的输入
}
c=a+b;
printf(“\n附加值为%d”,c);
返回0;//因为main返回int
}

返回成功读取的项目数,因此如果输入了有效值,则在您的情况下将返回1。如果不是,则输入了一个无效的整数值,
scanf
将返回0。

以下是一些完全满足您要求的代码。昨天发布了一个相同的基本堆栈溢出

此代码基本上是David提供的:

#include <stdio.h>

static int getInt(const char *prompt)
{
    int value;
    char c;

    while(printf("%s",prompt) && scanf("%d", &value) !=1)
    {
     do { c = getchar(); } while ( c != '\n' && c != EOF );              // flush input
            printf ("Invalid Entry, Try Again...\n");
    }

    return value;
}

int sum(int a , int b) {  return ( a + b ); }

int main(){

    int a , b;

    a = getInt("Please enter a number");
    b = getInt("Please enter a number");

    printf("Sum is :%d" , sum(a,b));

}
#包括
静态int getInt(常量字符*提示符)
{
int值;
字符c;
while(printf(“%s”,提示符)和&scanf(“%d”,和值)!=1)
{
do{c=getchar();}而(c!='\n'&&c!=EOF);//刷新输入
printf(“无效条目,请重试…\n”);
}
返回值;
}
int和(inta,intb){返回(a+b);}
int main(){
INTA,b;
a=getInt(“请输入一个数字”);
b=getInt(“请输入一个数字”);
printf(“总和为:%d”,总和(a,b));
}
函数getInt检查输入并大声喊出错误的输入。

使用
fgets()/sscanf()/strto…()
而不是
scanf()

scanf()。使用
fgets()
进行用户输入,然后扫描/解析

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdio.h>

int getint(const char *prompt) {
  char buf[sizeof(int) * CHAR_BIT];
  while (1) {
    fputs(prompt, stdout);
    fflush(stdout);
    if (fgets(buf, sizeof buf, stdin) == NULL) {
      // sample handling of unexpected input issue.
      // Value to return of EOF or IO error
      return INT_MIN;
    }
    /// strtol() is another option
    int i, n;
    // " %n" notes offset of non-white-space after the numebr.
    if (1 == sscanf(buf, "%d %n", &i, &n) && buf[n] == '\0') {
      return i;
    }
  }
}

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

  printf("This is a Addition program");
  a = getint("\n Enter value of a:");
  b = getint("\n Enter value of b:");

  c = a + b;
  printf("\n The added value is %d", c);
  return 0;
}
#包括
#包括
#包括
#包括
int getint(常量字符*提示符){
字符buf[sizeof(int)*字符位];
而(1){
FPUT(提示、标准输出);
fflush(stdout);
if(fgets(buf,sizeof buf,stdin)=NULL){
//意外输入问题的示例处理。
//返回EOF或IO错误的值
返回INT_MIN;
}
///strtol()是另一个选项
inti,n;
//“%n”注释numebr后非空白的偏移量。
如果(1==sscanf(buf、%d%n、&i、&n)和&buf[n]='\0'){
返回i;
}
}
}
内部主(空){
INTA、b、c;
printf(“这是一个添加程序”);
a=getint(“\n输入a的值:”);
b=getint(“\n输入b的值:”);
c=a+b;
printf(“\n附加值为%d”,c);
返回0;
}

@KristerAndersson这确实有道理。检查它是否不是整数(double,float,string),如果它不是整数(double,float,string),do xi如果它是另一个整数,它不应该关闭程序,而是应该给出一个自定义消息并再次请求输入。有问题吗?将所有内容都包含在
main
中的a
中,而(1){…}
如果(b>a)中断就在
}
之前,而
@kristeranderson是的。我理解他们的要求我写了一个工作代码‘基本上我写的代码可以工作如果用户键入
a
而不是数字,程序将永远运行。您需要清除错误的输入。结果输出应以换行符终止。为安全起见,还应报告添加的两个数字:
printf(“%d+%d=%d\n的结果”,a、b、c)。我认为在获得有效输入时提供一个上限(比如10次尝试)是一个好主意。如果用户在10次尝试中都没有成功,他们可能不会成功。在过去,我更喜欢以下测试:
scanf(“%d%1s”,&a,&c)==1
(上面声明了
char c;
):它不仅测试输入是否以整数开头,而且还测试输入是否只包含整数…@JonathanLeffler,你说得对。现在它工作得很好!我不认为任何用户会错误输入超过10次…@SergeBallesta:if(scanf(“%d%1s”),&a,&c)==1)的
测试在几个方面似乎是可疑的。首先,
1s
需要两个字符,因此您需要
char c[2]
,因此您不需要
&c
中的
。接下来,
%1s
跳过空白,直到它读取除空白以外的内容;它会吃掉空格、制表符和换行符,直到你输入一个字母或数字。这不太可能是必要的行为;对于一个互动节目来说,这真是令人讨厌的行为。而且,除非用户指示EOF(例如键入control-D),否则字符串输入不会失败,因此您不会得到
scanf()
@JonathanLeffler返回的1:。。。但我确实在互动程序中使用了它,强制输入只能是数字。我不确定最后一次输入的
1 abc
是否正确。它将被
%d1s”
技巧扼住,但用一个简单的
%d”
就可以了。我已经复制了代码。。但是找不到链接,功能不错;它节省了不可忽略的代码重复量。
while(打印提示&&scanf()!=1)
循环(基本上)也很好。但是,如果函数获得EOF,它将运行很长时间。您可能需要从
scanf()捕获返回