1

So I need the user input for an int to be greater than 2.

    printf("Number of triangles (must be greater than 2) = ");
    fflush(stdin);
    scanf("%d", &num_of_triangles);
    while (num_of_triangles < 3) // ?how to check using scanf?
    {
        printf("Number of triangles (must be greater than 2) = ");
        fflush(stdin);
        scanf("%d", &num_of_triangles);
    }

Is there any possibility to optimize this code regarding the repeating lines?

2
  • 5
    fflush(stdin) is undefined behaviour. Commented Oct 23, 2015 at 11:57
  • Please, post a complete example. Commented Oct 23, 2015 at 11:58

3 Answers 3

3

Instead of while, use do...while. That way, the loop will always run at least once.

num_of_triangles = 0;
do {
    printf("Number of triangles (must be greater than 2) = ");
    scanf("%d", &num_of_triangles);
    while (getchar() != '\n');    // this flushes the input buffer
} while (num_of_triangles < 3);

Also, don't fflush(stdin), as that's undefined behavior.

EDIT:

It seems that Visual Studio allows fflush(stdin). From MSDN:

The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.

In general however, this behavior shouldn't be depended on. Doing something more portable like the above code is preferable.

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

3 Comments

We work in Visual Studio and our teacher recommended using fflush(stdin) before scanf(); . How should I replace this?
@Lorant Not initially. Otherwise, getchar() will pick up the first value you enter instead of scanf.
The MSDN page linked to has this comment in the example code: // fflush on input stream is an extension to the C standard
1

fflush(stdin) is undefined behaviour, so you need to remove that entirely and substitute something else.

You should also check for the outputs of scanf, in case it fails.

If you want to improve readability, you can also move the scanf into your while loop directly, like this

while (scanf("%d",&num_of_triangles)==1 && num_of_triangles < 3)

1 Comment

The "undefined behaviour" claim needs some qualifications: en.cppreference.com/w/c/io/fflush (look for "Notes").
0

You can write your code as this -

printf("Number of triangles (must be greater than 2) = ");

while (scanf("%d",&num_of_triangles)==1 && num_of_triangles < 3) //check return of scanf as well as number
{
    printf("Number of triangles (must be greater than 2) = ");       
}

Loop will iterate until scanf is successful and num_of_triangles is less than 3.

6 Comments

Wouldn't while(scanf("%d", &num_of_triangles) != 1 || num_of_triangles < 3) be better?
@Lorant That will give you an infinite loop on character input.
@CoolGuy Sorry , but I don't get infinite loop with a character input .
Oh. I misread your code and got confused. It should be while(scanf("%d", &num_of_triangles) != 1 || num_of_triangles < 3) so that the error message is displayed on invalid input such as a character. You also need to clear the stdin in case of invalid input.
Why is it an infinite 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.