0

This code is meant to ask for an integer input from the user, then confirm if it is the input, and then display whether it is positive or negative if the answer is 'yes'.

#include<stdio.h>
int main()
{
    int s;
    printf("Enter an integer:");
    scanf("%d", &s);
    printf("So, you entered %d\n", s);
    printf("Is that correct? (y/n)\n");
    char chr;
    scanf("%c", &chr);
    if(chr=='y')
    {
        if (s<0)
        {
            printf("\n%d is a negative number.\n", s);
        }
        else if(s==0)
        {
            printf("\nThe number which you entered is Zero\n");
        }
        else
        {
            printf("\n%d is a positive number\n", s);
        }
        
    }
    else if(chr=='n')
    {
        printf("\nSorry, for that. Please re-execute this program.\n");
    }
    else
    {
        printf("\nEnter the write keyword! Re-execute this program.\n");
    }
    return 0;
}

But this doesn't ask for any input during the char scanf. Syntax error, or something else?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
LordRishav
  • 33
  • 6
  • You don't even have to use scanf here, consider using [getc(FILE*)](https://en.cppreference.com/w/c/io/fgetc) or [getchar()](https://en.cppreference.com/w/c/io/getchar). – iTs.SL4y3R Oct 06 '20 at 04:18
  • `scanf("%c")` is reading the next character from `stdin` that `scanf("%d")` left behind. In this case, it's the newline. [Just don't use `scanf`.](https://stackoverflow.com/q/2430303/) It's incredibly error-prone. – jamesdlin Oct 06 '20 at 04:21
  • Re "*write keyword*", Is that supposed to be "right"? It would be best if you specified what the correct inputs are. – ikegami Oct 06 '20 at 04:21
  • @ikegami Yes, I saw 'write keyword' after I posted it. But it shouldn't cause any syntax errors. – LordRishav Oct 06 '20 at 04:32
  • huh? You're not getting any syntax errors – ikegami Oct 06 '20 at 04:33

1 Answers1

0

It's reading the line feed (ASCII 10) produced when you pressed ENTER. You could use

scanf(" %c", &chr);

The space causes whitespace (including LF) to be skipped.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • This indeed solved the problem, but why did this happen? It doesn't happen with ```int```s – LordRishav Oct 06 '20 at 04:27
  • `%c` doesn't clear leading whitespace automatically cause you might be trying to read those characters. A `%d` encountering whitespace would have two options: fail or ignore the whitespace. So it's designed to ignore whitespace – ikegami Oct 06 '20 at 04:32
  • That means if no whitespace is given before ```%c```, it reads the ASCII value of the character, and not the character itself? – LordRishav Oct 06 '20 at 04:34
  • Computer doesn't know anything about `y`. I pressed a key, and it put byte 79 hex or 121 decimal in the buffer. (I'll use hex henceforth.) It put byte 79 because my machine is set to UTF-8, and `y` encoded using UTF-8 is 79. I said ASCII because most people use ASCII-based encodings, and `y` will encode to 79 in all of them. /// Pressing `é` on my machine would result in C3 (195 or -61 decimal depending on whether `char` is signed or not in my compiler), since `é` encoded using UTF-8 is the two bytes C3 A9. – ikegami Oct 06 '20 at 04:42