0

there are lots of similar question in forum but no one get me the right answer.

Hi every one

I'm really new in c programming and I have this problem.

I give you an simple code example of my problem:

#include <stdio.h>

int main ()
{
  char ch = 'y';
  int a = 0, b = 0, c = 0;

  while (ch == 'Y' || ch == 'y')
  {
    printf ("please enter the numbers: ");
    scanf ("%d", &a);
    scanf ("%d", &b);
    c = a + b;
    printf ("sum = %d \n", c);
    printf("do you want to calculate another numbers? N/Y\n");
    scanf('%c',&ch);
  }


  return 0;
}

in this code the program doesn't wait for last scanf (scanf('%c',&ch);)

I try to use getchar(); too, but the program doesn't respond to that too.

can any one give me a solution in this program and every program that I will do in the future?

B Ali
  • 11
  • 4
  • Please change `scanf('%c',&ch);` to `scanf(" %c", &ch);` (added a space, changed quote style) and see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Nov 12 '21 at 13:54
  • thanks for your help but it doesn't work and show this error in CMD Process returned -1073741819 (0xC0000005) – B Ali Nov 12 '21 at 13:56
  • Cannot reproduce (after correcting the program). Please read the link. – Weather Vane Nov 12 '21 at 13:59

0 Answers0