简体   繁体   English

如果在C语言中使用scanf输入了字符,如何获取错误以进行打印

[英]How to get errors to print if chars are entered using scanf in C

I'm experimenting with c at the moment and this program is suppose to get the user to input a number between ranges 10-100 if he enters anything that doesn't meet these conditions, the program will exit with an error code 1. anything that meets conditions, program will exit with 0. Down below is the code so far, but refuses to print correct error on char detection. 我目前正在尝试使用c,并且该程序假设用户输入的任何不满足这些条件的数值都会在10-100范围内输入数字,该程序将以错误代码1退出。如果满足条件,程序将以0退出。到目前为止,下面的代码是向下的,但拒绝在char检测时打印正确的错误。

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


int intGet(int ,int);
int error();
int userIn;
char x;

int main(void) {
    printf("Enter a number in between [10 -­100]: \n");
    x = scanf("%d", &userIn);
    intGet(x, userIn);

    return EXIT_SUCCESS;
}

int intGet(int min,int max ) {
    min = 10;
    max = 100;

    x = userIn;
    if ((x < min) || (x > max)) {
        printf("Input out of Range\n");
        exit(EXIT_FAILURE);
    } else if (x == 0) {
        printf("Incorrect input format\n");
        exit(EXIT_FAILURE);
    } else {
        printf("Read %d\n", userIn);
        exit(EXIT_SUCCESS);
    }
}

int error() {

}

It is not clear what your problem is. 目前尚不清楚您的问题是什么。 What error do you expect? 您期望什么错误?

You need to tidy up your code. 您需要整理代码。 There are few things. 没什么 scanf returns int value - the number of input items assigned. scanf返回int值-分配的输入项数。 You assign the return value to char x . 您将返回值分配给char x Then you assign the input value to x . 然后将输入值分配给x What is the logic behind? 背后的逻辑是什么? What do you expect? 你能指望什么? I guess your problem is logical. 我想你的问题是合乎逻辑的。 I suggest you: 我建议你:

  1. Handle the return and the input values separately 分别处理返回值和输入值
  2. Remove exit() statement, use return value instead. 删除exit()语句,改用返回值。 exit() terminates the program. exit()终止程序。
  3. Remove globals 删除全局
  4. If the above doesn't help use printf to see what is being processed 如果以上方法不能帮助您使用printf查看正在处理的内容

Example: 例:

int main(void) {
    printf("Enter a number in between [10 -­100]:\n");
    int userIn;
    int x = scanf("%d", &userIn);

    // for debug
    printf("Scanned: %d, returned: %d", x, userIn);

    int result = intGet(x, userIn);
    // analyse the result here
    switch (result) {
        case RES_SUCCESS:
           return SUCCESS;
        case ...
    }
}


int intGet(int x, int value) {
    int const min = 10;
    int const max = 100;

    if (x != 1) {
        return RES_ERROR_NO_INPUT;
    }

    if (value < min || value > max) {
        return RES_ERROR_OUT_OF_RANGE;
    }

    return RES_SUCCESS;
}

The root cause of the issue is ASCII codes for most of the characters are in between 10 - 100. 造成此问题的根本原因是大多数字符的ASCII码在10-100之间。

To solve your issue I have a complicated solution but it will help: 为了解决您的问题,我有一个复杂的解决方案,但可以帮助您:

steps: 脚步:

1.declare your variable "UserIn" as char * OR array of characters 1.将变量“ UserIn”声明为char *或字符数组

2.In scanf function use %s instead of %d 2.在scanf函数中使用%s代替%d

3.calculate length of entered string using strlen(UserIn) 3.使用strlen(UserIn)计算输入字符串的长度

4.If length is not equal to 2 OR 3 through error 4.如果长度不等于2或3(由于错误)

5.now check if UserIn[0] > 9 else through error 5. UserIn[0] > 9通过错误检查UserIn[0] > 9 else

6.now check if UserIn[1] > 9 else through error 6. UserIn[1] > 9通过错误检查UserIn[1] > 9 else

7.now check if length is 3 and UserIn[2] > 9 else through error 7.现在检查长度是否为3且UserIn[2] > 9否则出现错误

8.Now you have to convert this string into decimal value using: 8,现在您必须使用以下命令将该字符串转换为十进制值:

decimal_UserIn = ((10^2)*UserIn[2])+((10^1)*UserIn[1])+((10^0)*UserIn[0])

9.Now you can check whether it fits in your range(10-100) or not. 9.现在您可以检查它是否适合您的范围(10-100)。

Here I have assumed that you are needing the input-ed data in decimal format for further processing,otherwise you can directly check for strings within 0-9 at steps 5,6 and 7. 在这里,我假设您需要输入的数据以十进制格式进行进一步处理,否则,您可以在步骤5,6和7中直接检查0-9内的字符串。

PS I can help you providing whole code but I am running out of time. 附言:我可以为您提供完整的代码,但是我已经没时间了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM