help with the getc function

This is a discussion on help with the getc function within the C Programming forums, part of the General Programming Boards category; Hi, im a newbie C programmer and i need to get a single character input from the keyboard and save ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    help with the getc function

    Hi, im a newbie C programmer and i need to get a single character input from the keyboard and save it to a variable.

    so i tried this (not actually what i tried but a good approximation)
    Code:
    char get_char()
    {
         char character = getc(stdin);
         if (character == "y")
         {
              do some stuff
         }
         return character;
    }
    anywho, thanks in advance

  2. #2
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    try this:

    Code:
    scanf("%c", &locationtosave)

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ.
    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.*

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    to DLR thank you for your constructive response. I was actually considering that as a fall back, but i don't really wanna have to press return after each input....but w/e

    to Dave...thanks that a great help...much better than actaully answering my question which would probably take less time than posting a link to a FAQ

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Garavix
    tto Dave...thanks that a great help...much better than actaully answering my question which would probably take less time than posting a link to a FAQ
    Well, why not try reading it and find that the answer to your question will give likely eventually give you more questions that will not be as sufficiently correct and lead to more questions which eventually lead you to the FAQ, I thought I'd save you a little middleman time. But by all means, go write many lines of code that eventually need to be changed.

    Abandon hope all ye who enter user input via scanf without being thoroughly versed in the C standard.

    [edit]
    (not actually what i tried but a good approximation)
    When you want actual help, post actual code.

    Oh, and "y" is not a character.
    Code:
    if (character == "y")
    (Neither is the value returned by getc.)
    Last edited by Dave_Sinkula; 04-20-2006 at 11:09 PM.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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