提示用户输入整数值并检查有效输入的函数 [英] Function that prompts user for integer value and checks for valid input

查看:147
本文介绍了提示用户输入整数值并检查有效输入的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前只能完成我需要做的一小部分作业. 分配的一项要求是

I currently am stuck on a small part of an assignment I need to do. One requirement of the assignment is

调用一个函数,该函数提示用户输入二次方程的系数a,b和c的每个值,并返回输入的值,并通过错误检查有效输入(scanf返回一个值)."

"Call a function that prompts the user for each of the values of the coefficients a, b, and c for the quadratic equation and returns the value entered, with error checking for a valid input (scanf returned a value)."

,我不知道该怎么做.我可以轻松提示用户输入,也可以检查输入是否有效,但是我不知道如何将其转换为函数.我当前的代码是:

and I can't figure out how to do this. I can easily prompt the user for input and I can check if it is valid input, but I don't know how to turn this into a function. My current code is:

   {
   if (isalpha(a))
   {
      printf("INPUT ERROR!\n");
      printf("Enter a value for a: ");
      scanf("%d", &a);
   }
   } //this is how I would normally check the input

   int main(void) //start of main() function definition
   {
      int a, b, c, n, D; //declares integer variables a, b, c, n, and D
      float root1, root2; //declares float variables root1 and root2

      do //do while loop starts here
      {
         printf("Enter a value for a: "); //prompts user to input integer for variable 'a'
         scanf("%d", &a); //reads an integer from the keyboard and stores in the variable 'a' 
         printf("%d\n", a); //returns value of integer that was input for variable 'a'

         printf("Enter a value for b: "); //prompts user to input integer for variable 'b'
         scanf("%d", &b); //reads an integer from the keyboard and stores in the variable 'b'
         printf("%d\n", b); //returns value of integer that was input for variable 'b'

         printf("Enter a value for c: "); //prompts user to input integer for variable 'c'
         scanf("%d", &c); //reads an integer from the keyboard and stores in the variable 'c'
         printf("%d\n", c); //returns value of integer that was input for variable 'c'
         ...}

很抱歉出现任何格式错误,但这基本上是我坚持使用的程序的一部分.

Sorry for any formatting mistakes, but that is basically the part of the program I am stuck with.

我的问题是,如何将第一个函数与do/while循环中的所有内容组合在一起,以使一个大函数可以被调用三次?

My question is, how can I combine the first function with everything in the do/while loop to make one big function that I can call three times?

我不知道如何使用一个函数为a和b切换所有实例,因为我以前从未真正使用过这样的函数.

I don't know how I'd be able to switch out all the instances of a for b and c using a function, as I've never really had to use a function like this before.

推荐答案

scanf()已经根据格式说明符(%d)为您处理了输入,因此您只需要了解scanf的工作方式并使用它即可检查并构建您的功能:)

scanf() already processes the input for you according to the format specifier (%d) so you just need to understand how scanf works and use it to check and build your function :)

编写scanf("%d", &a);时,程序希望由于%d说明符而写一个整数,如果读取了整数,则程序会将其写入变量a.

When you write scanf("%d", &a); the program expects you write an integer because of the %d specifier, and if an integer is read, the program writes it into variable a.

但是函数scanf也具有返回值,即,您可以执行check = scanf("%d", &a);,并且在这种情况下check的值将为0或1.这是因为返回值记录了成功读取了多少个值.如果输入dsfugnodg,则没有数字,因此它将返回0.如果输入659 32,它将成功读取第一个值并返回1.

But the function scanf also has a return value, ie, you can do check = scanf("%d", &a); and check will have a value of 0 or 1 in this case. This is because the return value records how many values have been successfuly read. If you entered dsfugnodg there's no number so it would return 0. If you entered 659 32 it would read the 1st value successfully and return 1.

您的函数将类似于:

#include <stdio.h>

int getAndPrint(char label)
{
    int n = 0, val = 0;
    do {
        printf("Enter a value for %c: ", label);
        n = scanf("%d", &val);
        if (n == 0) {
            printf("Error, invalid value entered.\n");
            /* Consume whatever character leads the wrong input 
             * to prevent an infinite loop. See: 
             * https://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c */
            getchar();
        }
    } while (n == 0);
    printf("%c = %d\n", label, val);
    return val;
}

int main()
{
    int a, b, c;
    a = getAndPrint('a');
    b = getAndPrint('b');
    c = getAndPrint('c');
    printf("a=%d, b=%d, c=%d\n", a, b, c);
}

另请参阅: Scanf在C语言中的其他while循环都跳过

这篇关于提示用户输入整数值并检查有效输入的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆