Thread: Somthing weired with C !!

  1. #1
    Registered User wasem21's Avatar
    Join Date
    Dec 2010
    Location
    israel
    Posts
    5

    Somthing weired with C !!

    so i wrote this code fragment and when scan a input not a integer the code never stop printing
    "illegal input please try again" !!!!! nothing wrong with code :S any help
    Code:
    int x,y;
       int deminsions_check()
    {
        int f;
        f=scanf("%d%d",&x,&y);
        if(f==0)
        {
        printf("illegal input please try again\n");
        return 1;
        }
        else if(x>9|| y>9 || x<3 || y<3)
        {
           printf("illegal board deminsions please try again\n");
           return 1;
        }
        else
        return 0;
    
    }
    
    int main()
    {
        int u;
        printf("hello to the tic tac toe game !! ;)\n");
        printf("please enter demensions as rows and columns\n");
        u=deminsions_check();
        while(u==1)
        {
            
          u=deminsions_check();
    
        }
    
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you searched for your problem on the boards? We had two of those (as I recall) this week, let alone how many over the last several years. Or you can look at the faq.cprogramming.com.

  3. #3
    Registered User wasem21's Avatar
    Join Date
    Dec 2010
    Location
    israel
    Posts
    5
    im not finding similiar problem , and can't find explanation for this in the faq !!

    can you see what the problem with this code ?

    thanks any way..

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Check the FAQ again, this time looking for scanf().
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The "f" in "scanf" means formatted. If you cannot or do not trust your user to type the input in the proper format, then scanf is the wrong function to use. You would have to use fgets (or something similar) to get the non-numeric input out of the way, and then try reading again.

    Also note that checking f==0 is bad, since what if they type "4a6"? f would equal 1, since one number was successfully read, but that's not going to help you.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Try using two scanf statements, with a couple printf ones.

    Code:
    ...
    int f, g;
    printf("Enter X dimension: ");
    f = scanf("%d", &x);
    if (f == 0) { ... }
    printf("Enter Y dimension: ");
    g = scanf("%d", &y);
    if (g == 0) { ... }
    else { ... }
    ...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to use two. You just need to check for the right return value.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User wasem21's Avatar
    Join Date
    Dec 2010
    Location
    israel
    Posts
    5
    Quote Originally Posted by tabstop View Post
    The "f" in "scanf" means formatted. If you cannot or do not trust your user to type the input in the proper format, then scanf is the wrong function to use. You would have to use fgets (or something similar) to get the non-numeric input out of the way, and then try reading again.

    Also note that checking f==0 is bad, since what if they type "4a6"? f would equal 1, since one number was successfully read, but that's not going to help you.
    if type 4a6 it equal to 0 since the scan doesn't succeed , the code work perfectly without
    Code:
      while(u==1)
        {
    
          u=deminsions_check();
    
        }
    Code:
    You don't need to use two. You just need to check for the right return value.
    
    
    Quzah.
    and what the riight value ??? either 0 or 1 have any ideas ?
    thanks ..

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Read the Return Value section of man scanf.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by wasem21 View Post
    if type 4a6 it equal to 0
    You should try again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cant understand weired problem...
    By (Slith++) in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2006, 06:08 AM
  2. Somthing Wrong With My Array
    By ypramesh in forum C Programming
    Replies: 4
    Last Post: 04-01-2006, 10:35 PM
  3. Weired serial port problem
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2003, 05:31 AM
  4. MSDN and somthing else
    By Granger9 in forum C Programming
    Replies: 1
    Last Post: 09-05-2002, 01:56 PM
  5. forgot how to do somthing...
    By ackman in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2002, 04:57 PM