getchar() error?

This is a discussion on getchar() error? within the C Programming forums, part of the General Programming Boards category; This code is pretty self-explanatory. its supposed to read in a character and write the next character as its output. ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    getchar() error?

    This code is pretty self-explanatory. its supposed to read in a character and write the next character as its output. However i seem to be getting NOT that effect. The code from the book compiles and runs fine and i swear mine is the same but it works differently.
    Code:
    /* ch7listing7.2. getchar() */
    
    #include <stdio.h>
    #define space ' '
    int main(void){
      char ch;
    
      while ((ch=getchar()) !='\n'){
         if (ch == space){
            putchar(ch);
         }
    
         else
            putchar(ch + 1);
         ch = getchar();
      }
    
      putchar(ch);
      return 0;
     }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    try this
    Code:
    #include <stdio.h>
    
    #define space ' '
    
    int main(void)
    {
        char ch = 0;
    
        while ((ch = getchar()) != EOF) {
    	if (ch == space) {
    	    putchar(ch);
    	}
    
    	else
    	    putchar(ch + 1);
        }
    
        putchar(ch);
        return 0;
    }
    ~/

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    So: what does your code do? What do you expect it to do?

    It does pretty much what I expect it to do, but I don't have your book, so my expectations weren't tainted by high hopes.

    Dave

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by Dave Evans
    So: what does your code do? What do you expect it to do?

    It does pretty much what I expect it to do, but I don't have your book, so my expectations weren't tainted by high hopes.

    Dave
    The code is supposed to output ch + 1 for whatever character you enter ex:

    input: abcde
    ouptut:bcdef

    it outputs the first character correctly and subsequent charactes seemingly random.


    kermit: your code works up to a the last character, where it ouputs a characer I don't know. Try it and see.
    ex: abcdef
    out: bcdefg^
    Last edited by caroundw5h; 03-20-2004 at 06:33 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by caroundw5h
    The code is supposed to output ch + 1 for whatever character you enter ex:

    input: abcde
    ouptut:bcdef


    The original code had two getchar() calls in the loop, and, therefore skipped every other character. (One getchar() in the while() part, and one getchar() at the bottom of the loop. Get rid of the second one. It seems to work as advertised now, at least for the test string you supplied.

    Dave

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by Dave Evans
    The original code had two getchar() calls in the loop, and, therefore skipped every other character. (One getchar() in the while() part, and one getchar() at the bottom of the loop. Get rid of the second one. It seems to work as advertised now, at least for the test string you supplied.

    Dave
    This is the books code
    Code:
    /* cypher1.c -- alters input, preserving spaces */
    #include <stdio.h>
    #define SPACE ' '             /* that's quote-space-quote */
    int main(void)
    {
       char ch;
    
       ch = getchar();            /* read a character         */
       while (ch != '\n')         /* while not end of line    */
       {
           if (ch == SPACE)       /* leave the space          */
               putchar(ch);       /* character unchanged      */
           else
               putchar(ch + 1);   /* change other characters  */
           ch = getchar();        /* get next character       */
       }
       putchar(ch);
       getchar();              /* print the newline        */
       return 0;
    }
    and it works fine. I'm wondering if there is something i'm missing because i've been looking at it too long.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    your code (from first post):
    Code:
     while ((ch=getchar()) !='\n'){
         if (ch == space){
            putchar(ch);
         }
    
         else
            putchar(ch + 1);
         ch = getchar();
      }
    Two getchar() per loop

    book's code (from your last post)
    Code:
     ch = getchar();            /* read a character         */
       while (ch != '\n')         /* while not end of line    */
       {
           if (ch == SPACE)       /* leave the space          */
               putchar(ch);       /* character unchanged      */
           else
               putchar(ch + 1);   /* change other characters  */
           ch = getchar();        /* get next character       */
       }
    one getchar() per loop

    You don't see the difference?

    Dave

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Yeah, i'm a dumbass, I changed the code and not properly. Trying to make it more succint.

    [edit]
    Code:
    #include <stdio.h>
    #define space ' '
    int main(void){
       char ch;
      while ((ch=getchar()) != '\n'){
         ch = (ch == space) ? putchar(ch) : putchar (ch + 1);
      }
    
      putchar(ch);
      getchar();
      return 0;
     }[/edit]
    Last edited by caroundw5h; 03-20-2004 at 09:12 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

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