1

The problem is, basically, that if user enters a:5, b:f, everything works fine. But if it's the other way around and enters a letter to the 'a' variable, the program ends saying "Incorrect input", not letting the user to finish typing in rest of the variables. Why? Is it because of how I dealt with checking if the input is correct in the first place? How to "delay" the message and make it show after user finishes entering variables?

Here's the code:

#include <stdio.h>

int main(void) {

  short int l1=0, l2=0, l=0;
  int a=0, b=0;

  printf("Is number 'a' divisible by number 'b'?\n");
  printf("Number a: ");
  l1 = scanf("%d", &a);
  printf("Number b: ");
  l2 = scanf("%d", &b);

  l=l1+l2;

  if (l<2)
  {
  printf("Incorrect input");
  return 1;
  }
  else if (b==0)
    {
      printf("Operation not permitted");
      return 1;
    } 
    else if (a%b)
      {
        printf("%d is not divisible by %d", a, b);
      }
      else printf("%d is divisible by %d", a, b);

  return 0;
}
Andy J
  • 1,479
  • 6
  • 23
  • 40
Nettle
  • 31
  • 1
  • 5
  • 3
    That is because the incorrect input remains in the input buffer. So if you enter a letter for the first input, the second input already has data waiting for it. So both inputs are refusing the same letter.It won't matter how many time you try to scan that as a number, it will stay there. – Weather Vane Oct 14 '18 at 09:18
  • Oh, okay, thank you! So is it possible to somehow overcome this? Code something that lets you finish enetring the variables, even if it won't change the overcome (obviously)? I'm sorry that it seems like a very stubborn question, but being able to enter every variable in every case is one of the requirements I have to meet with this one exercise from what I'm seeing. – Nettle Oct 14 '18 at 09:57
  • There are two answers to [this previous question](https://stackoverflow.com/questions/52795781/program-to-detect-whether-only-integer-has-been-given-or-not-goes-into-infinite/52796054) which show two different approaches to the problem. One way cleans out the input. The other inputs a string first, and forgets the string when there was an error. – Weather Vane Oct 14 '18 at 10:06
  • I'm new to coding enough to not always have an idea what solution is actually possible and what is not, needed a tip what to look for. Thank you very much! I really appreciate it. – Nettle Oct 14 '18 at 10:09

2 Answers2

1

As Weather Vane already pointed out, the reason the program exits is, that when you enter a character (%c) and the scanf function is waiting for a integer (%d) it ignores the char, doesn't find an int but ends its' search on the '\n' (enter), so your variable l1 stays 0. This happens for all of your scanf calls, as it doesn't clear the buffer from characters that don't match.

Solving this


You can clear the input buffer, so that all the other scanf calls can get an actual input, though, you are still going to get an "Incorrect input" at the end.

printf("Number a: ");
l1 = scanf("%d", &a);
while (getchar() != '\n');

printf("Number b: ");
l2 = scanf("%d", &b);
while (getchar() != '\n');

If you want to repeat the input process until a user enters the numbers correctly, you have to check the return value of the scanf in a while loop, something like this:

do {
printf("Number a: ");
    l1 = scanf("%d", &a);
    while (getchar() != '\n');
} while (l1 != 1 || l1 != EOF);
niraami
  • 646
  • 8
  • 18
  • 1
    You should be checking for `EOF` too! – Weather Vane Oct 14 '18 at 10:13
  • ... but really, you shouldn't clear off the future input unless you *know* it has to be done. In the normal run of things, the newline will be cleared out by the the next `scanf` (except for `%c` specifier and some string scan sets). – Weather Vane Oct 14 '18 at 10:16
  • @WeatherVane What would be the proper way of clearing the unmatched input in this case though? – niraami Oct 14 '18 at 10:19
  • So, if I understand this correctly, on the first `scanf()` call, the input buffer has a `\n`, then on the next call to `scanf()`, it sees a `\n` already in the input buffer and doesn't attempt to read anything from `stdin`? – Andy J Oct 14 '18 at 23:00
  • @AndyJ Pretty much, but the second scanf call does read the input buffer, but sees the previous input as the first scanf didn't clear it. That's why you need the getchar() in either mine or Kanji's implementation. – niraami Oct 15 '18 at 04:33
0

Try this:

#include <stdio.h>

int main(void) {

  short int l1=0, l2=0, l=0;
  int a=0, b=0;

  printf("Is number 'a' divisible by number 'b'?\n");
  printf("Number a: ");
  l1 = scanf("%d", &a);
  getchar();
  printf("Number b: ");
  l2 = scanf("%d", &b);

  l=l1+l2;

  if (l<2)
  {
  printf("Incorrect input");
  return 1;
  }
  else if (b==0)
    {
      printf("Operation not permitted");
      return 1;
    } 
    else if (a%b)
      {
        printf("%d is not divisible by %d", a, b);
      }
      else printf("%d is divisible by %d", a, b);

  return 0;
}

Reference : scanf() leaves the new line char in the buffer

Kanji Viroja
  • 493
  • 1
  • 7
  • 17