相关疑难解决方法(0)

如果用户输入非数字字符,如何仅扫描整数并重复读取?

这是一个年轻的tyro问题,C代码试图阻止用户输入一个小于0或大于23的字符或整数.

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

int main(void)
{
    const char *input;
    char *iPtr;
    int count = 0;
    int rows;

    printf("Enter an integer: ");
    scanf("%s", input);
    rows = strtol(input, &iPtr, 0);
    while( *iPtr != '\0') // Check if any character has been inserted
    {
        printf("Enter an integer between 1 and 23: ");
        scanf("%s", input);
    }
    while(0 < rows && rows < 24) // check if the user input is within the boundaries
    {
        printf("Select an integer from 1 to 23: "); …
Run Code Online (Sandbox Code Playgroud)

c validation user-input

13
推荐指数
1
解决办法
10万
查看次数

标签 统计

c ×1

user-input ×1

validation ×1