0

The problem is only in the last scanf of the program

The following program works perfectly the 1st time, but as soon as I use the again (from the restart/quit goto program in the last), the whole program just starts to run infinitely without even stopping for the rightly written scanf() commands. NOTE that in the given program, BinToDec and dgts functions that are working perfectly.

int main()
{
    printf("WELCOME TO THIS AMAZING BINARY TO DECIMAL NUMBER CONVERTER\n");
start:;
    int bin, dgt, rev;
    printf("\nEner the binary number to convert it into decimal number:     ");
    scanf("%d", &bin);
    printf("\n\nYour binary number = %d\n", bin);
    int nib = bin;
    dgt = dgts(bin);
    char str[dgt + 1];
    sprintf(str, "%d", bin);
    int dec = BinToDec(dgt, str);
    printf("\n\n*   *   *   *   *   *   *\ndecimal no. is:     %d\n*   *   *   *   *   *   *\n\n", dec);
    char qor;
    printf("If you want to quit, enter q;\n If you want to use this tool again, enter any other character");
    scanf("\t% c", &qor);
    if (qor == 'q')
    {
        goto end;
    }
    else
    {
        goto start;
    }
end:
    return 0;
}

Ps: I do know the problem, and I've fixed it in my final code, but I wanted to know the reason of how just a single space in between the format specfier is leading to that weir result.

Tauqeer
  • 15
  • 3
  • Classic mistake; [never use any of the `scanf` functions for _anything_](https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf), _especially_ not to read interactive input. – zwol Sep 02 '22 at 20:01

1 Answers1

0

The format string in the call of scanf

scanf("\t% c", &qor);

is incorrect. For example there is no format specifier due to the space between the characters % and c. The function does not allow spaces in conversion specifications after the sign %. So this call enters nothing.

Instead write

scanf( " %c", &qor);

Pay attention to the leading space in the format string. It allows to skip white space characters as for example the new line character '\n' that can be stored in the input buffer of preceding calls of scanf.

Also it will be much better if instead of the goto statement you will use a loop as for example do-while loop.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Don't tell people how to fix their `scanf` format strings; tell them not to use it at all. – zwol Sep 02 '22 at 20:02
  • 1
    @zwol May I will not follow this silly advice? – Vlad from Moscow Sep 02 '22 at 20:03
  • I am entirely serious. – zwol Sep 02 '22 at 20:07
  • 1
    @zwol We have all kind of strange approaches here on SO, that's why I blocked several tags for me. Even if `scanf()` is a hard to use function, it has its merits, and used carefully, it works. Just do not expect too much from it. _You_ are free to avoid it, but please let others use it. – the busybee Sep 02 '22 at 20:08
  • @thebusybee Sha'n't. It is broken as designed and it is never appropriate to use it. – zwol Sep 03 '22 at 01:36