0

I was writing the following C code on Centos 6.5 using gcc-4.7 but program does not waiting for Type Y/N: statement it immediately exits after result.Either of the codes are not working.

#include <stdio.h>

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

   do
    {
            printf("Enter Number 1:\n");
            scanf("%d",&a);
            printf("Enter Number 2:\n");
            scanf("%d",&b);
            printf("Result is:%d\n",a+b);
            printf("Type y/N:\n");
            scanf("%c",&ch);
    }while(ch=='y' || ch=='Y');
    }
    return 0;
 }

OR

    #include<stdio.h>

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

       while(ch=='y' || ch=='Y')
       {
            printf("Enter Number 1:\n");
            scanf("%d",&a);
            printf("Enter Number 2:\n");
            scanf("%d",&b);
            printf("Result is:%d\n",a+b);
            printf("Type y/N:\n");
            scanf("%c",&ch);

       }
         return 0;
}
Lennart
  • 9,657
  • 16
  • 68
  • 84
Abhijatya Singh
  • 642
  • 6
  • 23
  • You need to read the documentation of every used function, notably [scanf(3)](http://man7.org/linux/man-pages/man3/scanf.3.html). You should test the result of `scanf`. You should compile with all warnings & debug info (`gcc -Wall -Wextra -g`) and **use the debugger** (`gdb`) (when coding with pointers, you'll want also to use [valgrind](http://valgrind.org/)) – Basile Starynkevitch Nov 11 '14 at 08:23

2 Answers2

5

When you execute:

scanf ("%d", &b);

and enter, for example, 42<enter>, the 42 is read and the <enter> is left in the input buffer.

Then it's picked up by the scanf("%c",&ch); which is one of the format strings that doesn't skip whitespace before attempting to read. From C99 7.19.6.2 /8, though largely unchanged in C11:

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.

Many input specifiers tend to skip white space before they start reading the real data, c is one of the exceptions.

For simple code like this, you could possibly get away with using a space in the format string since (from that same section /5):

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read.

So, you would use something like:

scanf (" %c", &ch);

Though that's not going to help if you enter a number like 47b for example :-)

At some point, every C developer realises the inadequacies of the standard input mechanisms, and writes their own.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

Your issue is: %c won't skip leading whitespace, so if there's a newline in the input stream (i.e. from your previous scanf entry i.e. number 2 say in your case) then scanf call will consume it immediately.

One way to solve the issue is to put a blank space before the specifier in the format string:

scanf(" %c", &c);

The blank in the format string tells scanf to skip leading whitespace.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
SMA
  • 36,381
  • 8
  • 49
  • 73