Problem accepting a character

This is a discussion on Problem accepting a character within the C Programming forums, part of the General Programming Boards category; Hi, My environment is Visual C++ 6.0. On executing the below code, its not waiting for me to enter a ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14

    Unhappy Problem accepting a character

    Hi,

    My environment is Visual C++ 6.0. On executing the below code, its not waiting for me to enter a character. After I enter a number, it goes directly to the printf statements. Please help...
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
    	char c;
    	int d;
    	printf("Enter a no:\n");
    	scanf("%d",&d);
    	printf("Enter a character\n");
    	scanf("%c",&c);
    	printf("The number you entered is %d",d);
    	printf("The character you entered is %c",c);
            return 0;
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Try this:
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
    	char c;
    	int d;
    	printf("Enter a no:\n");
    	scanf("%d",&d);
    	printf("Enter a character\n");
    	getchar();
        scanf("%c",&c);
    	printf("The number you entered is %d",d);
    	printf("The character you entered is %c",c);
        getchar();
        return 0;
    }
    I stuck a couple of getchars in it. Basically your first scanf takes a number but leaves the '\n' character from when you hit enter in the input buffer. The second scanf looks at the input buffer, sees the '\n' and takes it. The getchar basically just flushes it.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    As described in the FAQ, scanf() has some hidden traps.

    Use fgets() to read a whole line into a buffer, then use sscanf() (or anything else) to parse the buffer for the information you require.

    This will minimise the number of "what happened to my input" questions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    34
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
    	char c;
    	int d;
    	printf("Enter a no:\n");
    	scanf("&#37;d",&d);
    	printf("Enter a character\n");
                    fflush(stdin);
                    scanf("%c",&c);
    	printf("The number you entered is %d",d);
    	printf("The character you entered is %c",c);
            return 0;
    }
    use fflush(stdin);
    before scanf("%c",&c)
    ur problem will be solved

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    34
    hey can u tell me why fflush is wrong??
    still it clears your problem??

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Did you try clicking on the link I posted?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > hey can u tell me why fflush is wrong??
    See the FAQ.

    > still it clears your problem??
    There is a big difference between the "works for me" behaviour you're observing with your compiler, and the behaviour guaranteed by the standard which every compiler will implement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  3. printing strings (was similar problem)
    By weirdbeardmt in forum C Programming
    Replies: 5
    Last Post: 06-01-2004, 01:12 PM
  4. problem comparing character
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-19-2004, 11:15 AM
  5. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 02:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21