1

Here's my function and the problem is It never stops getting the input, in the command line.
I can't figure out where is the proper line to place a return 0.

int my_rot13(int c) {
    if ('a' <= tolower(c) && tolower(c) <= 'z')
        return tolower(c)+13 <= 'z' ? c+13 : c-13;
    return c;
}

int main() {

  int k, c;
  char *p;

  if (argc < 2) {
    while ((c = getc(stdin)) != EOF) {
      putchar(my_rot13(c));  
    }
    return 0; 
  }

  for (k = 1; k < argc; k++) {
    for (p = argv[k]; *p != '\0'; p++) {
      putchar(my_rot13(*p));
    }
    putchar(' ');
  }
  putchar('\n');

  return 0;
}

If I pass in the standard input like ./a.out "hey", it works exiting the program.

but when I get into user input mode I can not get out of this function.

3
  • 5
    Press CTRL-D to end the input. (or CTRL-Z if you're on windows) Commented Oct 30, 2013 at 22:23
  • 1
    Or check for (c = getc(stdin)) != '\n' to just accept one line. +1 for a well-formed question with proper code Commented Oct 30, 2013 at 22:25
  • Alternatively, check for something else than EOF Commented Oct 30, 2013 at 22:25

2 Answers 2

1

You need to send the EOF character to getc(), when you are typing the input in manually. You can do this on Linux with Ctrl-D, and on Windows with Ctrl-Z. Another option would be for you to test against a newline ('\n').

Sign up to request clarification or add additional context in comments.

Comments

0

If you're capturing from standard input, then use '\n' instead of EOF.

Comments