Thread: User pressing int or char....

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    15

    User pressing int or char....

    Hi all!

    Well im kinda noobish at the c-programming language, not a full blown reatard tho. Lets say that i wanna write a simple program that wants to calculate the value of x in function:

    y = kx + m

    We want the user to enter the value of y, k and m and doing this by:

    float y, k, m;

    printf("Enter value for K: ");
    scanf("%f", &k);

    (i just stop here, dont need to write more code)

    Here is my problem....... when i run the program and the user types the a char instead of an int my program crasch. How can i prevent this? Is there any test for keypress or something else i can do?

    Ty.
    Last edited by ingenting; 11-08-2008 at 06:00 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > (i just stop here, dont need to write more code)
    If it crashes you do.

    Check the return result of scanf().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Normally, I would suggest you separate input from conversion. It's not that the simple approach is hard, but it can be very tricky to make robust.

    Code:
    char buff[BUFSIZ];
    if ( fgets( buff, BUFSIZ, stdin ) ) {
      if ( sscanf( buff, "%f", &k ) == 1 ) {
        // success, do stuff with k
      } else {
        // some error message
      }
    }
    Even sscanf has problems, in that the numeric conversions do not detect numeric overflow, and the string conversions are tricky to get right to prevent buffer overflow.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    Salem my man, tyvm for ya help!!!!

    Now the program works as it should. One question tho...... about the size of buff[] in ya code.

    If i want to use a macro as size for an array[] i usual do it like this:

    #define SIZE 25

    and then i can use it as index/size with me array like:

    char arr[SIZE];

    But i dont see any #define BUFSIZ, how come?

    Again tyvm for help!!!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    BUFSIZ is usually a pre-defined macro somewhere, but it is not like it was not defined but rather that Salem left it out to simplify the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    OFC LOL sometimes me noobish glow just shines hehehe

    ty for quick reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM