Ending program when Newline is entered

Viewed 97

I am looking to understand how to make the program break from a while loop when the user has entered nothing into the command line. The program should only continue looping when the user has entered elements into the command line:

#include <stdio.h>
#include <ctype.h>

int main() {
    int ch;

while ((ch == getchar()) != '\n')  // read one char:  quit?
    putchar(toupper(ch));        // upper-case character and print

return 123 ;                     // Unix: check with: echo $?
3 Answers

With this code you're not assigning the ch variable but checking if it's equal to the value returned by getchar().
This way your while loop never stops iterating, since (ch == getchar()) is always 0 (false) and 0 != '\n' ('\n' value is 10).

You probably want to change (ch == getchar()) to (ch = getchar()), so you can update ch with the value entered by input.

I suggest you to turn on your compiler warnings, so it will tell you stuff like that and help you understand and fix it. I think you might find the following thread extremely useful: What compiler options are recommended for beginners learning C?

Example gcc with flags -Wall -Wextra (https://godbolt.org/z/n6vG6f7Gv):

![enter image description here

<source>:7:26: warning: comparison of constant '10' with boolean expression is always true [-Wbool-compare]
    7 | while ((ch == getchar()) != '\n')  // read one char:  quit?
      |                          ^~
<source>:8:5: warning: 'ch' is used uninitialized [-Wuninitialized]
    8 |     putchar(toupper(ch));        // upper-case character and print
      |     ^~~~~~~~~~~~~~~~~~~~

Moreover, it's a good practice to return 0 at the end of main function, which indicates that the program ended correctly, and eventually make it return something else in case errors occur.

You need to make sure that '\n' is at the first position of the line. Using fgets() is a simpler solution as it returns the entire null terminated read line with the '\n' at the end:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
  char line[256];
  size_t l;
  size_t i;

  while(fgets(line, sizeof(line), stdin)) {

    l = strlen(line);

    // '\n' is the last char in line[]

    // If empty line (only '\n')
    if (l == 1) {

      break;

    }

    for (i = 0; i < l; i ++) {
      putchar(toupper(line[i]));
    }

  }

  return 0;

}

Examples of executions:

$ ./readl
$ ./readl 
azerty is not qwerty
AZERTY IS NOT QWERTY
     word preceded by spaces
     WORD PRECEDED BY SPACES

Store the last value of ch.
If the last value of ch and the current value of ch are both newline, then break out of the loop.
For example:
command<enter>command<enter><enter>
The consecutive newlines, indicate a blank command.

#include <stdio.h>
#include <ctype.h>

int main() {
    int ch = 0;
    int last = 0;

    while ( ( ch = getchar()) != EOF) { // get value for ch
        if ( last == '\n' && ch == '\n') { // both equal to newline
            break;
        }
        putchar ( toupper ( (unsigned char)ch)); // print letters as uppercase
        last = ch; // store last value of ch
    }

    return 0;
}
Related