简体   繁体   中英

Why printf() is not used when scanf returns negative number?

I have written a code which checks when scanf() returns a negative number, for example if I press "Ctrl+Z" it should get out of the while loop and printf("Finish!") , but instead it is not printing the "finish!" can anybody take a look?

#include <stdio.h>
#include <conio.h>
#include <math.h>

#define G 9.81

float Height(float speed, float angle, float time);
float Horizontal(float speed, float angle, float time);


void main()
{
    float speed;
    float angle;
    float time = 0.1;
    float height = 0;
    float horizontal = 0;
    int res = 0;

    printf("Enter v <0.0 - 100.0 m/s> and a <0-90 degrees>: ");
    res = scanf_s("%f %f", &speed, &angle);
    while (res != -1)
    {
        for (time = 0.1; height >= 0; time += 0.1)
        {
            printf("Time: %.1f .... H = %.2f  S = %.2f \n", time, horizontal = Horizontal(speed, angle, time), height = Height(speed, angle, time));
        }
        height = 0;
        printf("Fallen!\n");
        printf("Enter v <0.0 - 100.0 m/s> and a <0-90 degrees>: ");
        res = scanf_s("%f %f", &speed, &angle);
    }
    printf("\nFinish!\n");
    getch();
}

float Height(float speed, float angle, float time)
{
    float height;
    angle = ((3.14 / 180) * angle);
    height = (speed * sin(angle) * time) - ((G*time*time) / 2);

    return height;
}

float Horizontal(float speed, float angle, float time)
{
    float horizontal;
    angle = ((3.14 / 180) * angle);
    horizontal = (speed * cos(angle)) * time;

    return horizontal;
}

I'm using Visual Studio (C language) on Windows.

Here the reference for scanf(): http://www.cplusplus.com/reference/cstdio/scanf/

When scanf() encounters end of file (that's the usual meaning of ctrl+z on the console), it is not a matching error: it's only that a part of the expected items could be read.

Make your while use >0 instead of >=0.

Be sure to press Enter after Ctrl+Z. By default, the terminal input is line-buffered, and it is passed to the program when newline is encountered.

Also, most probably you are running the program in a separated terminal window, which closes when the program exits. This would explain the need for getch(); at the end, to be able to see the output, and then close the window with a keypress.

By default, printf is buffered and the output is written to the terminal when newline \\n is reached. The buffer is also flushed when the program exits ( exit call or main return). In your case, it will be printed after you press a key, and you may not be able to see it, because the window closed.

To solve this, add a newline character at the end:

printf("\nFinish!\n");

I believe it should work. Even with >= in while. Definitely works on unix with ordinary scanf().

What IDE and compiler are you using? Are you running this directly through cmd.exe? I'd suggest checking Having troubles with EOF on Windows 7 Also as mentioned in other answers add:

printf("\nFinish!\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM