Thread: problem with scanf

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    problem with scanf

    Hi everyone!
    The problem is,the col val is not getting printed.I am using turbo c compiler.The second part of the code is the exact copy of the 1st part.No statements after scanf("%d",&col); is getting printed.Please help me understand,how to solve it.Thanks in advance.

    insert
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    
    void player_x()
    {
         int row,col;
         printf("enter the row no.\n");
         fflush(stdin);
         scanf("%d",&row);
         printf("the row val is :%d\n",row);    //test
    
    
         printf("enter the col no.\n");
          fflush(stdin);
         scanf("%d",&col);
    
    
        printf("the col val is :%d\n",col);  //test
    }
    int main()
    {
         player_x();
         return 0;
    }

  2. #2
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14
    I've looked at the code, and could not find anything wrong. When I ran the code through Code::Blocks, it printed everything just fine... Is there more code than what you published? The problem may be there, or with your compiler.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Get rid of the fflush statements. With %d, scanf() will ignore leading whitespace left in the buffer.

    See FAQ > Why fflush(stdin) is wrong - Cprogramming.com

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And while you're at it get rid of Turbo C... really, it's a freaking dinosaur that's just too stupid to lay down and become extinct.

    A far more up to date compiler with most of Turbo C's features is Pelles C
    It's C-99 standard with extensions that allow updating turbo C code... it's fully windows capable and it's free.

  5. #5
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Which compiler are you using?

  6. #6
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Took out fflush,still it's not working,any alternative for this code?

  7. #7
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14
    If you are no longer using fflush, you don't need the #include<conio.h> either. If your compiler does not have the conio header file, it may be screwing your system up. I ran it again after taking out fflush and the conio.h header file and again, it worked just fine. Again, no errors and no warnings.

  8. #8
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by VASHtheCHIBI View Post
    If you are no longer using fflush, you don't need the #include<conio.h> either. If your compiler does not have the conio header file, it may be screwing your system up. I ran it again after taking out fflush and the conio.h header file and again, it worked just fine. Again, no errors and no warnings.
    I took out conio.h too. Could you tell which other compiler i can go for? And which compiler are you using?

  9. #9
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14
    I am using Code::Blocks... It's got a bunch of compilers, but I'm using the GNU GCC compiler that comes along with it. Here is a page that tells you how to get it, and how to get set up with it.

  10. #10
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by VASHtheCHIBI View Post
    I am using Code::Blocks... It's got a bunch of compilers, but I'm using the GNU GCC compiler that comes along with it. Here is a page that tells you how to get it, and how to get set up with it.
    Thanks a lot for your help! The gcc compiler works on linux right,not for windows?

  11. #11
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Yes! the code is working in code::blocks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf problem
    By transgalactic2 in forum C Programming
    Replies: 11
    Last Post: 06-09-2009, 09:43 PM
  2. Problem with scanf
    By yoavagami in forum C Programming
    Replies: 2
    Last Post: 01-02-2008, 08:14 AM
  3. scanf problem
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-29-2005, 01:52 AM
  4. scanf problem
    By a1dutch in forum C Programming
    Replies: 8
    Last Post: 04-19-2005, 04:14 AM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM