1
votes

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.

3
Why would scanf() return -1. ( not a rhetorical question, I'm really interested ) ?Transcendental
return value of the scanf function is number of successful conversions, ie. arguments , input conversion. upon failure it returns -1Fawzan
@IlanAizelmanWS Yes, but why would it fail if you pressed Ctrl + Z?Transcendental
You mention scanf in your question, but your code uses scanf_s, which is not a standard function (I think it's a Microsoft thing?). I think you're also assuming that EOF is -1, which is not guaranteed to be true in general (though it may be in the MS case, I don't know and don't have a compiler handy to check!). The scanf function returns the number of fields assigned - not characters written - or EOF on error, or if when reading the first character it gets end-of-file. I think to force an end-of-file condition you have to press Enter-Ctrl+Z-Enter, so that might be the issue?psmears
@Transcendental Because scanf should get 2 floats. if it gets (Ctrl+Z) it is not a float. which means it will return -1Ilan Aizelman WS

3 Answers

1
votes

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.

0
votes

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");
0
votes

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");