修复BAC计划

时间:2014-09-30 04:09:23

标签: c

寻找一些帮助来修复计算BAC的程序并说明你是否可以开车。我在最后3个scanf语句没有运行时遇到了一些麻烦,并且声明的无限打印说明你是否可以开车。

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

int main() 
{

float drinks;
float shot = 1.5;
int beer = 12;
int wine = 5;
float floz;
float alcohol;
float weight;
float hours;
float bac;




printf("\nEnter beer if you drank beer, wine if you drank wine, and shot if you drank shots: ");
scanf(" %f.", &drinks);

printf("\nEnter how many fluid ounces of alcohol your drank: ");
scanf(" %f.", &floz);

printf("\nEnter your weight in pounds: ");
scanf(" %f.", &weight);

printf("\nEnter how long you've been drinking in hours: ");
scanf(" %f.", &hours);



do {
    alcohol = shot * floz * .4;
} while (drinks == shot);

do {
    alcohol = beer * floz * .05;
} while (drinks == beer);

do {
    alcohol = wine * floz * .12;
} while (drinks == wine);


bac = ((alcohol * 5.14) / (weight * .73)) - (.015 * hours);

printf("Your BAC is %f.\n", bac);


while (bac <= .03) {
    printf("You are ok to drive.\n");
}

while (.04 <= bac <= .08) {
printf("You may drive but it would be unsafe.\n");
}

while (bac >= .09) {
    printf("You are guaranteed a DUI if pulled over.\n");
}



return 0;

    }

1 个答案:

答案 0 :(得分:0)

修正程序:

#include <stdio.h>
#include <math.h>
#include <string.h>// for strcmp

int main() 
{

char drinks[5];// you need a string not a float
float shot = 1.5;
int beer = 12;
int wine = 5;
float floz;
float alcohol;
float weight;
float hours;
float bac;




printf("\nEnter beer if you drank beer, wine if you drank wine, and shot if you drank shots: ");
scanf("%s", drinks);// scanning string

printf("\nEnter how many fluid ounces of alcohol your drank: ");
scanf(" %f", &floz);

printf("\nEnter your weight in pounds: ");
scanf(" %f", &weight);

printf("\nEnter how long you've been drinking in hours: ");
scanf(" %f", &hours);


//compare the input and word using strcmp
if(strcmp(drinks,"shot")==0)        alcohol = shot * floz * .4;


else if(strcmp(drinks,"beer")==0)
    alcohol = beer * floz * .05;


else if(strcmp(drinks,"wine")==0)
    alcohol = wine * floz * .12;



bac = ((alcohol * 5.14) / (weight * .73)) - (.015 * hours);

printf("Your BAC is %f.\n", bac);

// no need to loop
if (bac <= .03) 
    printf("You are ok to drive.\n");
// your condition dosen't do what you think
else if (bac >= .04 && bac <= .08) 
printf("You may drive but it would be unsafe.\n");
// use else as it is sure that bac > .08
else
    printf("You are guaranteed a DUI if pulled over.\n");

return 0;
    }