0

Hey guys im trying to write a small program where the user has to put in a number between 1-9, anything else is an error, but I'm having trouble validating the input because if you put 12 it only reads the 1 and it goes in the loop. It has to be done using getchar() this is what have so far:

 printf(%s,"please enter a number between 1 - 9);
 int c;
 c = getchar();
    while(c != '\n') {

    int count = 1;
    count ++;

    if ((c >= '0' && c <= '9') || count > 1) {
 printf(%s, "Congrats!);
 }
 else
 {
 print(%s, "ERROR);
    }

 }

I'm also having problems validating the char into an int after it goes in. If i put in 5 i get 53.

4 Answers4

1

Try changing count > 1 to count == 1, and initialize it to 0 rather than 1. That way you can keep count of the number of digits you have. Also, note that because you initialize count to 1 and then immediately increment it, count > 1 will always evaluate to true, so if you gave it any char it will always say it's correct.

Tyler H
  • 413
  • 3
  • 11
1

getchar() will return the next character typed. If you want more than the first character you will need a call getchar() again within the while loop.

//Somewhere to store the result
//initialized with an invalid result value
int digitchar = 0;

//Get the first char
int c = getchar();
while (c != '\n')
{
    //Check if we already have a digit
    //and that the new char is a digit
    if (digitchar == 0 && c >= '1' && c <= '9')
    {
        digitchar = c;
    }


    //Get the next char
    c = getchar();
}

//Check if we have stored a result
if (digitchar != 0)
{
    //Success
}

Note this doesn't handle if a non-digit or newline character is entered. You would need to handle that as an error as well as if more than one digit is entered.

pticawr
  • 551
  • 5
  • 8
0

This is not working with 12 because getchar() takes one character per time.The following example is one way to solve it.

printf("please enter a number between 1 - 9");
int c[10], count=1;
//Declare an array because user may insert a bigger number
char number;
//This loop allow the user to enter an input
for(i=0;i<10;i++){
    number = getchar();
    if (number != ' '){
        c[i] = atoi(number);
        sum = sum + c[i];
    }
    else if(number == ' '){
        //Terminate the loop when user stop typing
        break;
    }
    else if( sum > 9 || sum < 0){
        //Start loop again until user enter valid input
        printf("You entered a number out of field try again\n");
        continue;
    }

}
while(c != '\n') {

        count ++;

    if ((c >= '0' && c <= '9') || count > 1) {
        printf("%d Congrats!",c);
    }
    else
    {
        printf(" %d ERROR", c);
    }

}
Man H
  • 77
  • 1
  • 11
0

Remember that getchar() returns the ascii value of the char, thus when you pass the value to the function you must subtract char '0' to pass the actual decimal value into the function. Another point is that you must clear the input buffer. If your user enters wrong input, you have to make sure that there is nothing left on the input buffer before you try to read input again. Hope this helps.

int main(void) {

    int input = 0; // 0 is the sentinel value to close program   

    printf("\n%s\n", "Enter value between 1-9 .\nEnter [0] to finish.");

    do {
        input = getchar();

        if (((input>= '1') && (input <= '9') || input == '0') && getchar() == '\n') {

            if ((input >= '1') && (input <= '9')) {
                callYourOwnFuntionAndPassValue(input - '0');
                printf("\n%s\n", "Enter value between 1-9 .\nEnter [0] to finish.");
            }
        } 
        else {
            while (getchar() != '\n') {} // clear input buffer
            printf("\n%s\n", "Please enter a valid number");
        }

    } while (input != END_PROGRAM);


    return NO_ERROR; // NO_ERROR  = 0
}
jats2k9
  • 1
  • 2