Function that prompts user for integer value and checks for valid input

前端 未结 5 1756
一个人的身影
一个人的身影 2020-12-20 09:07

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

\"Call a function that prompts the user for

5条回答
  •  粉色の甜心
    2020-12-20 09:23

    I think the following code is you wanted:

    #include 
    #include 
    #include   // for isalpha
    
    void InputAndCheck(int * pValue, const char * pName)
    {
        do
        {
            printf("Enter a value for %s: ", pName);
            scanf("%d", pValue);
            if (isalpha(*pValue))
            {
                printf("INPUT ERROR!\n");
                continue;
            }
            else
            {
                break;
            }
        } while (1);
    
        // clear the input buffer
        fflush(stdin);
        return;
    }
    
    int main()
    {
        int a, b, c;
        InputAndCheck(&a, "a");
        InputAndCheck(&b, "b");
        InputAndCheck(&c, "c");
        printf("a=%d;b=%d;c=%d;\r\n",a,b,c);
        return 0;
    }
    

提交回复
热议问题