Home > database >  Input are not able to calculate and print out in C
Input are not able to calculate and print out in C

Time:10-18

I've tried to build a simple calculator for physics force experiments.

//
//  main.c

#include <stdio.h>

int main() {

    char name[20];
    int  month,date;
    int difference_percentage1, difference_percentage2, difference_percentage3;
    double force1, force2, force3;
    scanf("%s", name);
    scanf("%d %d", &month, &date);
    scanf("%lf %lf %lf", &force1, &force2, &force3);
    double Favg=(force1   force2   force3)/3.000;
    puts(name);
    difference_percentage1=100*(force1-Favg)/Favg;
    difference_percentage2=100*(force2-Favg)/Favg;
    difference_percentage3=100*(force3-Favg)/Favg;

    printf("%d %d %d\n", difference_percentage1, difference_percentage2, difference_percentage3);
    getchar();

    return 0;
}

The calculate doesn't match what I've typed for scanf().

CodePudding user response:

The o's of the % mark for the %f look a bit larger to me than for the %d, so my guess is that you used the wrong % for the %f format specifier which isn't recognized by the compiler; it interprets the double value as int.

Edit: one more reason to post text instead of a screenshot of the code! (o;

Edit2: Yup, your % mark in %f is actually a "EF BC 85" in hex but should be "25"

  •  Tags:  
  • c
  • Related