Thread: getchar

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    getchar

    Hello

    im trying to write a program that is required to use the getchar method.

    the variables (there is two) is of type int and float.

    is the coding for it

    int a = (getchar() != '\n');

    will this read a number greater than one digit, like 15. if not who do i write it so it does read bigger numbers.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Use fgets() instead of getchar(). fgets() will read until it reaches a \n

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    can fgets() stop chars from being entered

    i need for only numbers to be entered

    Thats why i changed from scanf to get char

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by TyrioN
    Hello

    im trying to write a program that is required to use the getchar method.

    the variables (there is two) is of type int and float.

    is the coding for it

    int a = (getchar() != '\n');

    will this read a number greater than one digit, like 15. if not who do i write it so it does read bigger numbers.
    ::edit

    The following does not solve your problem - see Hammer's post below - I added a couple of comments on your use of getchar() for information purposes..

    You did ok to assign the value from getchar() to an int, but you should note that you need some extra brackets

    Code:
    /* != is of higher precendence than = , so what you really have 
    is this: */
    
    a = (getchar() != '\n')
    What the above code does is to test the return value of getchar() against '\n' and then sets the variable a to 1 or 0. Not exactly what you want.

    Try some extra brackets:
    Code:
    int a = 0;
    
    (a = getchar()) != '\n'
    ~/
    Last edited by kermit; 09-01-2004 at 06:58 PM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by TyrioN
    can fgets() stop chars from being entered

    i need for only numbers to be entered

    Thats why i changed from scanf to get char
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    Thanks

    Thanks Guys you are a lifesavers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM