EOF and getchar() problem

This is a discussion on EOF and getchar() problem within the C Programming forums, part of the General Programming Boards category; hi guys I'm challenged to write a code for the following: write a text up to 1000 characters, and get ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    EOF and getchar() problem

    hi guys

    I'm challenged to write a code for the following:

    write a text up to 1000 characters, and get it back. the end of the text is detected though EOF on a new line or reaching 1000 characters.(EOF=ctr-Z on a new line)

    I mainly have problem how to bring the EOF work probably. it doesn't react when I press ctrl-z directly . could you see the problem in the code?

    Code:
    int i;
        char Text[1000];
    
     printf("write the text:\n");
        gets(Text);
    
    for(i=0;i<=1000;i++){
    
     Text[i]=getchar();                  //read every sign
    
     if((Text[i]=getchar()== '26' || i==1000)){   //26 is the ASCII code for ctrl-z
    
        puts(Text);
    
        }
    }
    
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    check two things: operator precedence of '==' vs '=' vs '||', and what value is '26' (hint, its not 26 decimal). best to put parentheses around each term you want to isolate in your expression.
    run this snippet :
    Code:
    #include <stdio.h>
    
    int main(int argc,char *argv[])
    {
    	int a = '26';
    
    	printf("%d\n",a);
        return 0;
    }
    edit: read the second paragraph in this link :
    C syntax - Wikipedia, the free encyclopedia
    Last edited by dmh2000; 06-20-2013 at 03:49 PM.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Read twice the post of dmh and then take a look here, in order to see how one uses EOF. It was from the very first occurrences of EOF in our ip course, so I think it can help.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    Thanks guys!! it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with getchar()
    By blob84 in forum C Programming
    Replies: 2
    Last Post: 08-23-2010, 05:54 PM
  2. Problem with getchar()
    By SasDutta in forum C Programming
    Replies: 3
    Last Post: 03-31-2010, 11:33 AM
  3. Help getchar() problem
    By rob90 in forum C Programming
    Replies: 7
    Last Post: 01-28-2010, 11:26 AM
  4. Problem with getchar()
    By georgio777 in forum C Programming
    Replies: 6
    Last Post: 11-09-2009, 01:58 AM
  5. A getchar problem
    By ch4 in forum C Programming
    Replies: 2
    Last Post: 10-29-2007, 04:39 AM

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