Wanted to stop alphabet characters from getting inputted

0 john johnny · August 22, 2015
Hi I wanted to stop people from entering letter/s in my console. Below is what i wrote. I had tried "number != isdigit" "(isdigit(number) == 1)" (isalpha(number)) and many more. But all of them end up in the same result. The error message keep looping. As if it remembers the characters that I first entered. I also tried to change the data type of "number" and replace the %d with %c but still the same. This problem however does not occur if the condition and the number inputted are digits.

NOTE : number here is declared as int.

As of the time of writing I had made a temporary fix which will display an error message and make the console exit. But that's not what I want. What I really want is the console not to close and the user may enter numbers.

Sorry for my awful grammar.

I don't know what's going on but the image does not show so I decided to share the link.
https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-9/11928748_996167470414820_6421661611108937295_n.jpg?oh=ca4799ffc4ec3ed831f32b48dcd65a39&oe=563C61D1&__gda__=1450011367_b5daa3e32d6cd5871dc97f6c4241f261

Post a Reply

Replies

Oldest  Newest  Rating
+1 c student · August 22, 2015
have you checked the return value of scanf?
if (scanf ("%d", &number) == 1) {
   printf ("You have entered the number: %d\n", number);
} else {
   printf ("No number has been entered\n");
}

also, your isalpha usage is incorrect.  i dont know why your compiler didnt raise the error.

isalpha man page: http://linux.die.net/man/3/isalpha
scanf man page: http://linux.die.net/man/3/scanf
0 john johnny · August 22, 2015
Hi. Thank you for the response. Actually I'm trying to print errors so that only numbers within range can be entered.



#include
#include
#include
#include

main()
{
    int number;

    printf ("Enter a number below 30: ");
    scanf (" %d", &number);

    if( number != isdigit ){

        printf ("\n\nError!!! Number(s) only please");
        printf ("\nPress ENTER to start over again.");
        getch();
        system ("cls");
        return main();
    }else if (number>30){
        printf ("\n\nError! Please enter a number that is below 30");
        printf ("\nPress ENTER to start over again.");
        getch();
        system ("cls");
        return main();
    }else if (number<30){
        printf ("Congratulation you read the instruction");
    }
getch();
}

0 john johnny · August 23, 2015
Hi can I get a little bit of explanation why you write "== 1" in this line (scanf ("%d", &number) == 1)
like what does the 1 stands for? I'm really new in C and I want to understand details like this.
Thanks.
0 c student · August 23, 2015
"These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. "

scanf man page: http://linux.die.net/man/3/scanf
  • 1

C

106,947 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator