-1

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;
}
Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
  • 1
    `0` is not the same as `'0'`. Is there some reason why you are not using any C++ in C++ code? – 463035818_is_not_an_ai Dec 05 '20 at 15:55
  • Does this answer your question? https://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c – 463035818_is_not_an_ai Dec 05 '20 at 15:56
  • i removed some non relevant tags, tag the ones that are actually related to the question. Your code has recursion, but thats not part of the problem, and `integral` tag is for calculating integrals. – 463035818_is_not_an_ai Dec 05 '20 at 15:58
  • Try `(ch - '0') > 7`, negative sign `'-'` and decimal point `'.'` must be handled as well. – πάντα ῥεῖ Dec 05 '20 at 15:58
  • for the first question our teachers are weird and want us to work with c++ libraries and code but dont want us to do any c++ code ourselves (for the most part) @largest_prime_is_463035818 also thanks im new here and will attemp to do better with tags next time i dont know what the difference between '0' and 0 is, what topic is that for me to catch up on – mistervoidnewb Dec 05 '20 at 16:11
  • read the link in my second comment. – 463035818_is_not_an_ai Dec 05 '20 at 16:12
  • btw on a second thought maybe the recursion tag wasnt that irrelevant. It is hard to tell because your code is incomplete. Eg You are missing the closing `}` of the function and of the `while` and I am not sure where to put it, looks like you call `scan` again no matter what. Please provide a [mcve] – 463035818_is_not_an_ai Dec 05 '20 at 16:15
  • @largest_prime_is_463035818 you are totaly right the scan is supposed to happen no matter what, the code is incomplete and the next thing i am working on is condition if ch is one of the numbers different functions will be called, they are ment to repressent a menu the user can chose from – mistervoidnewb Dec 05 '20 at 16:28
  • the code you posted does not compile. It is not your real code. You are still missing at least a `}`. Please post a [mcve]. Why call it again no matter what? At some point you need to return – 463035818_is_not_an_ai Dec 05 '20 at 16:31
  • `scanf("%d", ch);` is wrong.. you need to pass a pointer to the int, not the value. And it reads a decimal integer, not a character, so `isdigit(int(ch))` wouldn't be appropriate. You need to check `scanf`'s return to make sure it succeeded and handle failure appropriately too, though. And you may need to read off leading whitespace, or characters left in the stream after a failed read. – Dmitri Dec 05 '20 at 16:36
  • @Dmitri fixed that must have been an error that happened during copy pasting how would i check for failure – mistervoidnewb Dec 05 '20 at 16:43
  • @largest_prime_is_463035818 it should be fixed now – mistervoidnewb Dec 05 '20 at 16:43
  • If `scanf()` succeeds, it will return the number of fields it assigned, which should be 1. If it fails, the invalid characters are left in the stream so you need to read them off before trying again. Also, adding a space at the front of your `scanf()` format string will skip leading whitespace. – Dmitri Dec 05 '20 at 16:47
  • @largest_prime_is_463035818 Remember to _be nice_. – Asteroids With Wings Dec 05 '20 at 16:55
  • Have you seen what some askers are doing to their code? Is THAT nice? Animals! They are ANIMALS! But on a more serious note, be careful with `fflush(stdin);` The behaviour is undefined in C and as a result you can't count on what you're going to get. IN Linux-land it'll clear the stream, but that behaviour will not be consistent as you move the code around. You could get a plague of nasal demons. – user4581301 Dec 05 '20 at 17:03
  • Even though the man page makes it seem it would, `fflush(stdin)` generally does *not* clear the stream on linux (at least not with input from a terminal). It works on DOS/Windows with the usual C libraries there, but I don't know of anywhere else you can rely on it. It's better to just read off the invalid characters instead. – Dmitri Dec 05 '20 at 17:29
  • @AsteroidsWithWings please let me know what you refer to. There has been some agressive tone on this post, so I might have missed to pick the right words in the heat, but honestly I don't know what you mean. – 463035818_is_not_an_ai Dec 05 '20 at 18:09

3 Answers3

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)

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
0

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
    
Kevin
  • 6,993
  • 1
  • 15
  • 24
  • the problem is if i dont use flush and enter a character then my whole recursion breaks and it keeps constantly printing out without stopping for scan i dont really know why also flush stopped this from happening – mistervoidnewb Dec 05 '20 at 16:54
  • `fflush(stdin)` is a non-standard way to clear the input stream on some implementations. See [this answer](https://stackoverflow.com/a/18170425/5522303). – Kevin Dec 05 '20 at 16:55
-2

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

Aniket Bose
  • 55
  • 1
  • 8