0
#include<stdio.h>
#define LINESIZE 1024

int n, sum =0;
char line[LINESIZE];

int main() {
    while(1) {
        printf("enter an integer: ");
        if(!fgets(line, LINESIZE, stdin)) {
            clearerr(stdin);
            break;
        }

        if (sscanf(line, "%d", &n) == 1)
            sum += n;
    }
    printf("%d \n",sum);
}

When I run this in Cygwin, the output seems infinite and I don't know how to return sum? Am I missing something?

enter an integer: 1
enter an integer: 2
enter an integer: 3
enter an integer: 4
enter an integer: 5
enter an integer: 6
3
  • 4
    ctrl-d indicates end-of-file Commented Feb 11, 2017 at 3:56
  • when are you expecting fgets to return 0? It will only return the null pointer when it encounters the end of file marker, i.e. CTRL-D Commented Feb 11, 2017 at 6:53
  • the call to fgets() will block, waiting for input. When reading from stdin, the is only one way to handle the problem. I.E. pass the program in End Of File indication. for cygwin, that is a <ctrl-d> Commented Feb 11, 2017 at 6:56

2 Answers 2

3

Your while loop is fine, the program loops until it hits the end of file for stdin. From the terminal, you can signal an end of file by pressing Ctrl-D under Unix and Ctrl-Z Enter on Windows.

Alternately, you could exit the loop when you read some specific input, such as a blank line, a line without a number, a line with the word quit...

Some remarks about the program:

  • There is no reason to make your variables global, nor to clear the error condition on stdin.
  • The idiomatic statement for an infinite loop is for (;;) { ... }.
  • main() should return 0.

Here is a modified version of your program:

#include <stdio.h>

#define LINESIZE 1024

int main(void) {
    char line[LINESIZE];
    int n, sum = 0;

    for (;;) {
        printf("enter an integer: ");
        if (!fgets(line, sizeof line, stdin)) {
            break;
        }
        if (*line == '\n') {
            /* stop on empty line */
            break;
        }
        if (!strcmp(line, "quit\n")) {
            /* stop if the user types quit */
            break;
        }
        if (sscanf(line, "%d", &n) == 1) {
            sum += n;
        }
    }
    printf("%d\n", sum);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the comments mentioned in the question, I would like to suggest a slightly different approach. You can modify the if statement which calculates the sum such that when the input from the user is not an integer, you choose to exit the while loop.

    if (sscanf(line, "%d", &n) == 1) {
        sum += n;
    }
    else {
        printf("Could not get integer\n");
        break;
    }

Sample output 1:

enter an integer:
3
enter an integer:
4
enter an integer:
2
enter an integer:
r
Could not get integer

9

Sample output 2: sscanf successfully extracted 5 from 5gf

enter an integer:
3
enter an integer:
4
enter an integer:
5gf
enter an integer:
t
Could not get integer

12

Sample output 3: sscanf could not extract 5 from r5f and rightly so

enter an integer:
5
enter an integer:
3
enter an integer:
r5f
Could not get integer

8

6 Comments

Would appreciate if the downvoter left a comment. Thank you.
I'm not the downvoter, but I suppose you've been downvoted because your post does not answer the question, it only adds something not-related.
@linuxfan, I believe I have provided an answer to the question and not added anything un-related to the question.
Sorry, you are really wrong. The title (and content) of the question is "how to exit while loop in C?" You don't say anything about it. Instead you propose an improvement, for which the OP didn't ask. Such things can be comments, not answers.
@linuxfan, perhaps you have not read my answer correctly. I have suggested adding a break statement when we establish that the user has not provided any integer input and in such a case, display the current sum and exit. If you read chqrlie's answer, the suggestion is on similar lines of using an explicit "quit" OR "\n" to get out of the loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.