Domanda

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.

#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() {

}
È stato utile?

Soluzione

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. You assign the return value to char x. Then you assign the input value to 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() terminates the program.
  3. Remove globals
  4. If the above doesn't help use printf to see what is being processed

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;
}

Altri suggerimenti

The root cause of the issue is ASCII codes for most of the characters are in between 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

2.In scanf function use %s instead of %d

3.calculate length of entered string using strlen(UserIn)

4.If length is not equal to 2 OR 3 through error

5.now check if UserIn[0] > 9 else through error

6.now check if UserIn[1] > 9 else through error

7.now check if length is 3 and UserIn[2] > 9 else through error

8.Now you have to convert this string into decimal value using:

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.

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.

P.S. I can help you providing whole code but I am running out of time.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top