简体   繁体   中英

Check if the input is a whole number in C++ in simple way using isdigit

This is what I have tried
It would always act as false no matter the input
How could I do it right in a simple way if possible and why doesn't it work?
I am trying to create a condition that would ask if the scanned user input is one of the numbers and if not it asks them to only write one of the shown numbers and shows what the options are.

void konc()
{
    exit (0);
}


void scan()
{
    int ch = 0;
    printf("0 - \n");
    printf("1 - \n");
    printf("2 - \n");
    printf("3 - \n");
    printf("4 - \n");
    printf("5 - \n");
    printf("6 - \n");
    printf("7 - \n");
    scanf("%d", &ch);
    
    while (isdigit(int(ch)) == false || ch < 0 || ch > 7){
        printf("Must be one of the numbers\n");
        fflush(stdin);
        scan();
    }
    if (ch == 0){
        konc();
    }



    printf("%d", ch);
    scan();
}

int main()
{

    scan();

    return 0;
}

The problem is in the checking logic.

The number 0 is not the same thing as the character '0' (which is mapped to a value of 48 ).

Change the 0 to '0' . (and 7 also)

You have a few issues:

  1.  int ch = 0; ... scanf("%d", ch);

    scanf needs to be given a pointer to the data it's reading into, so this should be scanf("%d, &ch);

  2.  while (isdigit(int(ch)) == false || ch < 0 || ch > 7){

    ch is already an int, so using isdigit doesn't make sense. It's already a number. Just check the range ( while (ch < 0 || ch > 7) { )

  3.  fflush(stdin);

    Using fflush on an input stream is undefined behavior. Get rid of this.

  4. Instead of calling scan recursively, you should use a loop to jump back to the top:

     int number; while(1) { printf("0 - \n"); printf("1 - \n"); printf("2 - \n"); printf("3 - \n"); printf("4 - \n"); printf("5 - \n"); printf("6 - \n"); printf("7 - \n"); scanf("%d", &number); if(number < 0 || number > 7) { printf("Must be one of the numbers\n"); continue; // Go back to the top of the loop } break; } // Now number is between 0 and 7

your logic is wrong. Make a loop which will continue until the user chooses a right number.

#include <conio.h>
#include <stdio.h>
int main(void)
{
    scan();
}

void scan()
{
  printf("Choose any of the following numbers:\n");
  do
  {
    printf("0 - \n");
    printf("1 - \n");
    printf("2 - \n");
    printf("3 - \n");
    printf("4 - \n");
    printf("5 - \n");
    printf("6 - \n");
    printf("7 - \n");
    char ch;  // now it is not undefined
    scanf("%c",&ch);
    fflush(stdin);
    if(ch<48||ch>55)  //ascii of '0' is 48 and ascii of '7' is 55.
      printf("Must be one of the numbers\n");
    else
      break;
  }while(1);
}

I have tested it.It will surely work.Also no need of isDigit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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