2

I want to know the reason why this code not run properly

#include<stdio.h>
main()
{
    char a;
    int n;
    do
    {
        printf("enter the number");
        scanf("%d",&n);
        printf("the squre is %d",n*n);
        printf("want any more so Y for yes N for no");
        scanf("%c%[^\n]",&a);
    }while(a=='Y');
}
taufique
  • 2,701
  • 1
  • 26
  • 40
  • 2
    you probably meant to ask on [so], not here. Also, please look at the formatting help or use the formatting toolbar. Also, please describe the error you're observing (input, expected output and why, actual output or error message (copy-pasted with the line number identified)). "This code doesn't run properly" is as non-descriptive as it can get. – John Dvorak Jan 01 '14 at 08:21
  • scanf("%c%[^\n]".... needs another parameter – mattnz Jan 01 '14 at 09:00
  • This question has been answered before, for example here: [C: Mutliple scanf's, when I enter in a value for one scanf it skips the second scanf.](http://stackoverflow.com/questions/9562218/c-mutliple-scanfs-when-i-enter-in-a-value-for-one-scanf-it-skips-the-second-s) – Thomas Padron-McCarthy Jan 01 '14 at 10:05

4 Answers4

3

Reasons are
1. scanf("%c%[^\n]",&a); needs two parameters. Remove %[^\n].
2. \n character left behind by the previous scanf on pressing Enter key. Next scanf will read this \n character in the buffer. You need to consume this \n. Use a space before %c specifier to eat up this \n.

Try this:

scanf(" %c",&a);  
       ↑ A space before %c specifier  

A space before %c specifier is able to eat ant number of newline characters.
Your code after modification:

#include<stdio.h>

int main(void)
{
    char a;
    int n;
    do
    {
         printf("enter the number\n");
         scanf("%d",&n);
         printf("the squre is %d\n",n*n);
         printf("want any more so Y for yes N for no\n");
         scanf(" %c",&a);
    }while(a=='Y');

    return 0;
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
haccks
  • 104,019
  • 25
  • 176
  • 264
2

Here is a solution to your problem.

#include<stdio.h>
main()
{
    char a;
    int n;
    do
    {
        printf("enter the number");
        scanf("%d",&n);
        a = getchar(); // <-- Here is a change.
        printf("the squre is %d",n*n);
        printf("want any more so Y for yes N for no");
        while(a == '\n') a = getchar();
    }while(a=='Y');
}

What actually is making you problem is this line.

scanf("%d",&n);

Suppose, you entered 10 then pressed 'Enter', now scanf() takes 10 and leaves a newline behind in the buffer. It is making problem. By getchar() you are eating up that new line each time after taking a input with scanf. Yes there are other solutions too with scanf() tricks but it seems simpler to me. So I shared it.

haccks
  • 104,019
  • 25
  • 176
  • 264
taufique
  • 2,701
  • 1
  • 26
  • 40
0

Wrong code: scanf("%c%[^\n]",&a);
Right code: scanf(" %c%*[^\n]",&a);

0

just use scanf(" %c",&a); and could be done.

haccks
  • 104,019
  • 25
  • 176
  • 264
vinayawsm
  • 845
  • 9
  • 28