1

what is the problem here? scanf doesnt seems to working in while loop. i was trying to find out vowel & consonent until user wants.

Here's the code:

#include <stdio.h>
main()
{
    char x,c;
    do
    {
        printf("enter\n");
        scanf("%c",&x);
        if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')
            printf("vowel\n");
        else
            printf("consonent\n");

        printf("do u want to continue ?(y/n)\n");
        scanf("%d",&c);
        if(c=='n')
            printf("thnks\n");

    } while(c=='y');
    return 0;
}
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208

6 Answers6

6

You are trying to read a character using %d which is wrong. Use %c instead.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

Change code to scanf("%c",&c) your original code is getting the y/n entries as digits not characters

Edit:

Probably you are getting the carage return instead of the character try using getc or fgets instead and get the first character.

Zaid Amir
  • 4,727
  • 6
  • 52
  • 101
1

I think the problem might be here: scanf("%d",&c); It should be:

    scanf("%c",&c);
1

Here is the Correct code:

#include <stdio.h>

int main()
{
    char x,c;
    do
    {
        printf("enter\n");
        scanf("%c",&x);
        getchar(); //to remove the \n from the buffer
        if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')
            printf("vowel\n");
        else
            printf("consonent\n");
        printf("do u want to continue ?(y/n)\n");
        scanf("%c",&c); //Here you were using %d instead of %c
        getchar(); //to remove the \n from the buffer

        if(c=='n')
            printf("thnks\n");
    }while(c=='y');

    return 0;
}
Varaquilex
  • 3,447
  • 7
  • 40
  • 60
  • what if cr/lf is returned for key? – KevinDTimm Feb 27 '13 at 17:25
  • It is generally better to explain the problem / hint the solution rather than just give it away. Especially in case of simple questions like this which are generated by beginners. – Shahbaz Feb 27 '13 at 17:39
0

Both scanfs should be changed like this:

scanf(" %c",&x);

...

scanf(" %c",&c);

Note the space before the %, it's important: it consumes leading whitespace, which includes the endline characters left over in stdin after processing the input.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
-1

Please try with this code to run the loop multiple times.

EDIT: Different solution without fflush(stdin). Please define a string of 8 characters as

char str[8];

and modify the code in the loop as

fgets(str, 8, stdin); // To read the newline character
printf("do u want to continue ?(y/n)\n");

scanf("%c",&c);
fgets(str, 8, stdin); // To read the newline character
Ganesh
  • 5,880
  • 2
  • 36
  • 54
  • tried but it prints consonant and just then y/n. does not takes input 1st. – Kotha Ahsan Feb 27 '13 at 17:28
  • You would require 2 flushes for eliminating the 2 enter keys. I have tried this code and it seems to be functional. – Ganesh Feb 27 '13 at 17:29
  • @KothaAhsan did it work with the new code or still facing some issues? – Ganesh Feb 27 '13 at 17:33
  • 1
    `fflush(stdin)` is undefined. – Shahbaz Feb 27 '13 at 17:39
  • @Shahbaz `fflush` is a standard method to flush a given file pointer. `stdin` is the standard input file pointer. Hence, I am not sure if I got your comment correct. – Ganesh Feb 27 '13 at 17:42
  • @Ganesh See http://stackoverflow.com/questions/2979209/using-fflushstdin (it also doesn't work on any of the three platforms I use at work) – Cubbi Feb 27 '13 at 18:55
  • @Ganesh you probably wanted to write `stdout` but made mistake correct it. Cubbi's link is helpful. learn and update your answer better. – Grijesh Chauhan Feb 27 '13 at 19:34
  • @GrijeshChauhan The idea was to flush the input, specifically the new line character. I referred to Cubbi's link and have updated the answer. From my understanding, `fflush` on streams opened for `reading` depends on the specific library implementation. In either case, the solution is not dependent on `fflush` anymore. – Ganesh Feb 27 '13 at 23:43
  • @Cubbi Thanks for the link. I tested on MSDN and MingW and found it to be working. I have updated my answer not to use `fflush`. – Ganesh Feb 27 '13 at 23:44
  • Flushing the input is not a good idea. You _think_ you are removing the line already read, but that's not always the case. For example, if you redirect a file to your input, you throw _everything_ away. The proper way is something like `int c; do { c = fgetc(stdin); } while (c != '\n' && c != EOF);`. This is besides the point that formally, the C standard says that `fflush` on input streams is undefined behavior. – Shahbaz Feb 28 '13 at 09:33
  • @ganesh, by the way, your edit now is also not good. `gets` is a terribly unsafe function. It is deprecated by all standards (even removed from C11). You should use `fgets` instead. Nevertheless, you still need a loop, since `fgets` may not consume the whole line. The simple loop I wrote above is the easiest and safest solution I have come up with so far. – Shahbaz Feb 28 '13 at 09:48