简体   繁体   中英

Program closes in Dev-C++

This is the code I have entered. I am a beginner at programming and can't understand why the computer ignores the command to return an A or B

  #include <stdio.h>
    int main (void){
        float grade;

        printf ("Enter grade");
        scanf  ("%d",&grade);

        if (grade >= 90) {
            printf ("A\n");
        }
        else {
            printf ("B\n");
        }
        return 0;
    }

You have to change your format specifier in your scanf statement. From this:

scanf("%d",&grade);

to this:

scanf("%f",&grade);

Because %d is for integer's and %f is for float .

For more infromation about scanf() see this: http://www.cplusplus.com/reference/cstdio/scanf/

The scanf format string should be "%f" ( float ) instead of "%d" ( int ). C++ will blindly store an int in the memory allocated for the float . The other code will then try to interpret those bits as a float, which will get strange results.

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