Home > Back-end >  Creating a Blood Alcohol Content Calculator but having errors? [closed]
Creating a Blood Alcohol Content Calculator but having errors? [closed]

Time:09-26

Hello, I am trying to create a blood alcohol content calculator, my program runs but whenever I input the values my if and else statements do not work, nor can I switch between male and female values. Below is the description of the question.

Many car accidents are a direct result of drunk driving. In order to reduce the number of such accidents, the government is desperately seeking cooperation from the public to monitor their amount of alcohol consumption.

Being a good citizen, you want to help saving lives by developing a program that can estimate the Blood Alcohol Content (BAC) of a person.

Any person can use your program to test if he/she is intoxicated after consuming alcoholic beverages. If, indeed, the person is consider intoxicated, he/she can rest until he/she is safe to get behind the wheel again.

Your program will rely upon the Widmark’s formula, which is state as follows: (x * y /z) -(h * 0.017)

where:

x is the amount of alcohol consumed, y is 8 ( if you know how to do if statements y is 8 for a man and 10 for a woman) z is the weight of the person in pounds, and h is the time duration (in hours) in which these alcohol was consumed.

In reality, this program can be used to calculate the BAC for anyone.

The basic program should calculate for a man (if you can work with a decision statement, it can adjust for a woman too.) Also, for this assignment, a 12 oz beer with 3.5% alcohol content is the main focus.

Thus, x is the number of beers times 0.42, where 0.42 is obtained by multiplying 12 by 0.035. For example, if only two beers are consumed, x is 2 × 0.42 = 0.84. For this project, you can assume that the user drinks only beer in 12oz cans with 3.5% alcohol content. .In order for your program to work, the user has to supply the following information:

a. number of beers consumed by the user b. weight of the user c. the duration (in hours) in which these beers are consumed by the user d. gender of the user ( bonus )

Example:

Example 1:

Enter the number of 12 oz beers: 3

Enter the weight (in pounds): 150

Enter the time duration (in hours): 2

Enter m for male and f for female: m

Since your BAC level is 0.03, you are safe to drive.

Code that I have

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    int beers;
    int weight;
    int time;
    int y;
    double first;
    double second;
    double third;
    string gender;

    cout << "Enter the number of 12 oz beers:" << endl;
    cin >> beers;
    cout << "Enter the weight (in pounds):" << endl;
    cin >> weight;
    cout << "Enter the time duration (in hours):" << endl;
    cin >> time;
    cout << "Enter m for male or f for female:" << endl;
    cin >> gender;
    y = 8;
    first = beers * y / weight;
    second = time * 0.017;
    third = second - first;
    if (third < 0.08)
    {
        cout << "Since your BAC level is " << third << " , you may drive!";
    }
    else
    {
        cout << "Since your BAC level is " << third << " , you may not drive!";
    }
    
    return 0;
}

CodePudding user response:

#include <iostream>

int main() {
  size_t numbeers = 0; // number of 12 oz beer cans
  const double ALC_BY_VOL = 0.035 * 12;
  double y = 0; // some gender constant
  double z = 0; // weight in pounds
  double h = 0; // time duration in hours
  char gender = 'f';
  double BAC = 0;

  // Get user's input
  std::cout << "Enter Number of Beer Cans(12 oz): ";
  std::cin >> numbeers;
  std::cout << "Enter Weight(pounds): ";
  std::cin >> z;
  std::cout << "Time passed since last drink(hours): ";
  std::cin >> h;
  std::cout << "Gender(m/f)[f]: ";
  std::cin >> gender;

  // Decide on the gender constant
  if (gender == 'f') {
    y = 10;
  } else if (gender == 'm') {
    y = 8;
  }

  // Compute the results and inform the user
  BAC = ((ALC_BY_VOL)*numbeers) * (y / z) - (h * 0.017);
  std::cout << BAC << '\n';
  if (BAC < 0.08) {
    std::cout << "Safe to drive!\n";
  } else {
    std::cout << "Please consider getting a cab\n";
  }

  return 0;
}

Not the best answer, but should give you an idea on what might be going wrong on your end. Please consider using doubles where you need floating point precision. Integer division such as 3/5 is just 0, and not 0.6.

CodePudding user response:

This is the issue:

first = beers * y / weight;

first is of type double. but beers, y, and weight are all of type int.

Hence, the expression, beers*y/weight is an integer expression itself. And decimal values like .16 simply get rounded down to 0 in integer expressions.

The easy fix is make one of those variables a double to make the entire expression a double. You can cast too.

first = (beers * (double)y)/ weight;
  •  Tags:  
  • c
  • Related