Thread: Program will not exit on EOF

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    Program will not exit on EOF

    Hi my program is functioning properly and doing all it is supposed to do, but i just cant get the bloody thing to exist with the while loop

    can anyone tell me why this is happening? or how to fix it?

    Code:
    
    #include <stdio.h>
    
    int rotate_right(int);
    int rotate_left(int);
    int encode(int, int);
    
    int main(void){
    
      int shift, ch;
    
      printf("Enter a number for amount of rotation: ");
      scanf("%d",&shift);
    
      printf("Enter text to scramble: ");
    
    
      while((ch = getchar())!=EOF){
    
        ch = encode(ch,shift);
        putchar(ch);
    
      }
    
    
            return 0;
    
    }
    
    int encode(int ch, int shift){
    
      while(shift!=0){
    
        if(shift>0){
    
          ch = rotate_right(ch);
    
          shift--;
    
        }
        else if(shift<0){
    
          ch = rotate_left(ch);
          shift++;
        }
      }
    
      return ch;
    }
    
    int rotate_right(int ch){
    
      if((ch >= 'A'&& ch < 'Z')||(ch >= 'a'&& ch < 'z'))
        ch++;
      else if(ch == 'Z')
        ch = 'A';
      else if(ch == 'z')
        ch = 'a';
    
      return ch;
    }
    
    int rotate_left(int ch){
    
      if((ch > 'A'&& ch <= 'Z')||(ch > 'a'&& ch <= 'z'))
        ch--;
      else if(ch == 'A')
        ch = 'Z';
      else if(ch == 'a')
        ch = 'z';
    
      return ch;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What OS and how are you entering EOF?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    48
    i do this on Netbeans and Cygwin, but what i am confused about further(based on the question you just asked) what is EOF? i thought it was end of file? so after i enter the text for the program to scramble, shouldnt the loop automatically exist when it reaches the enter key from stream?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    what is EOF? i thought it was end of file?
    That's right EOF is end of file.
    so after i enter the text for the program to scramble, shouldnt the loop automatically exist when it reaches the enter key from stream?
    The enter key doesn't generate EOF it generates the end of line character. EOF is generated by a control key combination either CTRL Z for Windows or CTRL D for Linux.

    Jim

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    48
    ahh thank you for that clarification

    so if i wanted to exit on end of line, how would i go about implementing this?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Jim wants to say that CTRL Z is equivalent to EOF.So on you code check if EOF,then input CTRL Z and see what happens

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    so if i wanted to exit on end of line, how would i go about implementing this?
    Then you would need to check your character to see if it contains this character (0x0a).

    Jim

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    949
    Quote Originally Posted by jimblumberg View Post
    Then you would need to check your character to see if it contains this character (0x0a).

    Jim
    Or use '\n' to make the intention clear and to make the program portable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program won't exit
    By tkd_aj in forum C Programming
    Replies: 7
    Last Post: 04-10-2012, 11:59 PM
  2. exit program
    By Teardrop3903 in forum C Programming
    Replies: 7
    Last Post: 03-11-2011, 01:06 AM
  3. -1 to exit program ?
    By sharris in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2010, 09:14 PM
  4. How to exit program???
    By spottedzebra in forum C Programming
    Replies: 10
    Last Post: 06-21-2010, 04:43 PM
  5. exit program
    By spentdome in forum C Programming
    Replies: 4
    Last Post: 04-22-2002, 02:08 PM