1

I had this problem for a while. when I try to run my program, It will run but it will skip the 2nd scanf which is the nudoor, the part where my program ask for number of doors.

If anyone could help. Here's the screenshot of my program running DOOR SKIP


int main ()
{
    float length, height, width;
    height = 7.5;   
    
    int nudoor, nuwin, gall, door, win, doorsol, winsol, gallsol, gallsolu;
    gall = 450;
    door = 18;
    win = 9;
    
    printf("Width of Room: ");
    scanf("%.2f",&width);
    printf("Number of doors: ");
    scanf("%d",&nudoor);
    printf("Number of windows: ");
    scanf("%d",&nuwin);
    printf("Length of room: ");
    scanf("%.2f",&length);

    doorsol = nudoor * door;
    winsol = nuwin * win;
    gallsol = doorsol + winsol + length + width + height;
    gallsolu = gallsol/gall;

if (gall >= gallsol)
    printf("Gallons of Paint needed: 0");
else
    printf("Gallons of Paint needed:%d",gallsolu); 

return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Meowtyx
  • 13
  • 2
  • Compile the code with warnings enabled, and treat warnings as errors. And check the return value of your `scanf` calls. They should all return 1 in your case, if they manage to parse the input correctly. – Cheatah Oct 03 '21 at 19:12
  • `%.2f` is not a valid conversion specification. The behavior of your program is undefined after it calls `scanf` with that. Change it to `%f`. Also, in `gallsolu = gallsol/gall;`, you may want `-` rather than `/`. – Eric Postpischil Oct 03 '21 at 19:12
  • I want to divide the gallsol and gall not subtract them. also thanks %f worked. – Meowtyx Oct 03 '21 at 19:21

1 Answers1

0

Such a format string as shown below

scanf("%.2f",&width);

is incorrect. There is no precision part .2 of a format string for scanf (opposite to printf). So remove it everywhere where it is used to read float numbers.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335