Thread: Something odd with putchar()...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    8

    Something odd with putchar()...

    I am writing a simple program with using getchar() and putchar() which outputs the next letter up in the alphabet from the input.

    Here is my code :
    Code:
    #include <stdio.h>
    
    int main(void) {
    	char c;
    	printf("Enter letters: ");
    	while((c = getchar()) != '0') {
    		putchar(++c);
    	}
    }
    If i input the letters abcde pressing enter in between each the output is -
    Enter letters: a
    b♂b
    c♂c
    d♂d
    e♂e
    f♂f
    g♂g
    h♂
    First of all, why are newline characters being outputted? Can this be stopped?
    Secondly, why does the '\n' character get incremented to '\v' aswell as the alphabet character?

    Thankyou

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, your code increments every character it comes across before printing it. Since getchar() returns every character you type (except for backspace etc), including <enter>, well, that's what you get. An incremented '\n'.

    The newline character is "printed" because you typed it. There's no standard way to avoid this happening.

    But if you're willing to be non-standard, you can use things like getche() (from <conio.h>, for Dev-C++) to get every character you could think of, including backspace and '\n'. I think it still echos the newline, though.
    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.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you're willing to go non-standard, learn the *nix or Windows methods for dealing with input. Please don't use conio.h. lol...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It should also be noted that typing a '\0' is not usually how you end input. You can type a '\0', usually; CTRL-SHIFT-2 on DOS systems. But EOF is the standard "character". You can type it with CTRL-Z, plus that's what happens automatically when you redirect a file for input to your program.

    EOF can only be stored in an int, not a char. So you should probably use an int in your code.
    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.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    An idea. I don't think I'm missing anything.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main( void )
    {
        int ch;
    
        while( ( ch = getchar() ) != EOF ) {
            if( isalpha( ch ) )
                putchar( ++ch );
            else
                putchar( ch );
        }
        return 0;
    }
    this is the 1st test!!
    uijt jt uif 1tu uftu!!
    does it work??
    epft ju xpsl??
    yes, goodbye
    zft, hppeczf
    ^Z

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Time to (ab)use the ternary operator.

    Code:
    putchar( isalpha(ch) ? ++ch : ch );

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or, since nothing actually depends on the computed result,
    Code:
    putchar( isalpha(ch) ? ch+1 : ch );
    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.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    8
    Thankyou all for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. array help
    By 1rwhites in forum C Programming
    Replies: 17
    Last Post: 11-09-2005, 04:10 PM
  3. crazy triangles
    By markg in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 12:50 PM
  4. Text based frame in win32 console
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 04-16-2002, 07:01 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM