Thread: Help with a small program!!

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Unhappy Help with a small program!!

    Hi ,
    I am new to C programming and have a question on the following. I have to write a program which must accept a single character as input and echo that character before the enter key is pressed. The programming environment is GNU. The execution must end once ctrl D is pressed.I was not able to use getchar putchar to solve this. Can somebody help me out??
    tnexpress

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Thanks Hammer for ur post. I tried out the programs but they require the user to press the enter key before echoing the character. Can you please tell me how to go about this?
    tnexpress

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well it helps if you actually READ what you're given. The first link tells you what you say you can't find.


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

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    hi!
    But I am not able to use conio.h as im using linux platform!! I tries using curses.h but tht doesnt seem to work either!!
    tnexpress

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    From the second link:
    Code:
    #include <stdio.h> 
    #include <termios.h> 
    #include <unistd.h> 
    
    int mygetch(void) 
    {
      struct termios oldt,
                     newt;
      int            ch;
      
      tcgetattr( STDIN_FILENO, &oldt );
      newt = oldt;
      newt.c_lflag &= ~( ICANON | ECHO );
      tcsetattr( STDIN_FILENO, TCSANOW, &newt );
      ch = getchar();
      tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
      
      return ch;
    }
    The first link has a broken link at the end of the page, where it says "this FAQ entry".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Here's a tip - post what you tried instead of whining "it doesn't work"

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I tries using curses.h but tht doesnt seem to work either!!
    You may have to link to the library.

    [edit]
    1000th post!!!
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tnexpress
    I tries using curses.
    Yeah, I do that a lot.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with small program
    By cjohnman in forum C Programming
    Replies: 11
    Last Post: 04-14-2008, 09:59 AM
  2. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  3. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  4. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 06:15 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 09:09 PM