Gender selection

Sep 5, 2012 at 8:07pm
Hey, im making a protject for school. The program is designed to calcualte a persons BAC.

In order to do so i have to choose if the person is either a Male or Female. (its the water % that changes. right now my solution is this:

cout <<"type 0.68 if you are male or 0.55 if you are a woman:\n";
cin>> a;

but i want it to be so that the user can just type Male or Female or M or F and then either 0.68 or 0.55.

Any ideas?

Regard Jonas Quist
Sep 5, 2012 at 8:10pm
It would be best if you could post the the entirety of your source code whilst also using "code" tags for best answers.
Sep 5, 2012 at 8:11pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using namespace std;

int main()
 {


 float a;
 float b;
 float c;
 float e;

 cout <<"type 0.68 if you are male or 0.55 if you are a woman:\n";
 cin>> a;

cout << "hours you've been drinking:\n";
cin >> b;

cout<<"number of beers:\n";
 cin >> c;

double d = c * 12.0; // grams of alchohol ex. 15 * 12 = 180g pure alchohol

cout <<"type your weigth:\n";
 cin>> e;

// pure alcohol in gram / weigth * waterpercent

 cin.ignore();
 float f = e / 10.0 * b; // the pr hour burn ex. 72 / 10 * 3 = 21,6
 float g = e * a;
 float h = d - f;
 double result = h / g;

 if (result>=0.5){
 cout<<"you are too drunk to drive, you BAC is:"<<" "<<result<<endl;
 cin.get();
 return (0);
 }

 if (result<=0.5){
 cout<<"you can drive, but be carefull:" << " "<<endl;
 return (0);
 }

return 0;

 }
Sep 5, 2012 at 8:12pm
i know it looks a little messy havent sorted everything out yet
Sep 5, 2012 at 8:18pm
alright, that isnt the worst code ive ever seen but it could use a decent amount of work. first of all if you want to select gender, declare a variable of type "string" (no quotes) and name it "gender". then prompt the user to input the string....
1
2
cout << "Please enter your gender.  M/F: ";
cin >> gender; 


then when changing the formula for each gender....
1
2
3
4
5
6
if (gender == "m" || gender == "M"){
//the formula for male BAC
}
else {
//the formula for female BAC
}
Sep 5, 2012 at 8:21pm
so ill creat a variable like float gender or int gender

and then ask the user to answere M or F and save it in the Gender.. and... ahh ofc an else statement..
Sep 5, 2012 at 8:27pm
Yes, the variable declaration should be similar to this string gender;
Sep 5, 2012 at 8:28pm
also you will need to add #include <string> to the first couple lines of code
Sep 5, 2012 at 8:29pm
the problem is still when i get to the final calculation. i dont know how to bring in what ever i saved. because what variable do i save it in?
Sep 5, 2012 at 8:32pm
something lie this then
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string gender;
 float a;
 float b;
 float c;
 float e;

 cout << "Please enter your gender.  M/F: ";
cin >> gender; 

if (gender == "M" || gender == "F"){
float a = 0.68;
}
else {
float a = 0.55;
}
Sep 5, 2012 at 8:42pm
I have one last question if i may

1
2
3
4
5
6
7
8
9
10
11
12
double result = h / g;

 if (result>0.5){
 cout<<"you are too drunk to drive, you BAC is:"<<" "<<result<<endl;
 cin.get();
 }
else{
 cout<<"you can drive, but be carefull:" << " "<<endl;
 cin.get();
 }

return 0;


this part always so matter what the answer is it prints cout<<"you can drive, but be carefull:" << " "<<endl;

can you find my mistake
Last edited on Sep 5, 2012 at 8:42pm
Sep 5, 2012 at 10:00pm
You really need to re-name your variables, its giving me a head ache trying to keep track of what everything does.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string gender;
 float a;
 float b;
 float c;
 float e;

 cout << "Please enter your gender.  M/F: ";
cin >> gender; 

if (gender == "M" || gender == "F"){
float a = 0.68;
}
else {
float a = 0.55;
}


This doesn't work. You're telling it that if the person says they are male or if theyre female, set a to equal 0.68.

It needs to be:

1
2
3
4
5
6
7
8
if (gender == "M" || gender == "m"){
float a = 0.68;
}

if (gender == "F" || gender == "f"){
float a = 0.55;
}

That will ask the user if he/she is a female or male and then set 'a' accordingly.
Last edited on Sep 5, 2012 at 10:01pm
Sep 6, 2012 at 6:19am
Ok thank you.. and i will rename my variables its giving me a head ache too

i fill try this
Topic archived. No new replies allowed.