I'm working on some problems for my c++ class and this one is giving me a lot of trouble. According to the question (which is about calculating a person's height based on their gender and femur length) I'm supposed to write a program that has the following input:
• Gender of that adult: (char type)
• A height determined by foot bone: (float type)
• Length of thighbone of an adult: (float type)
And has an output of both the input height and the calculated height (float type). Then i am to determine if the height based on the thighbone is consistent with the height determined by the foot bone (differs by no more than 0.5 inches in absolute value), and indicate the answer as "consistent" or "not consistent".
int main ()
{
char gender; //THE PERSON'S GENDER
float w; //THE HEIGHT OF A MAN BASED ON THE LENGTH OF THEIR BONES
float x; //THE HEIGHT OF A WOMAN BASED ON THE LENGTH OF THEIR BONES
float y; //THE LENGTH OF THE PERSON'S THIGHBONE
float z; //THE DIFFERENCE BETWEEN THE TWO HEIGHT ESTIMATES
//THIS FUNCTION DISPLAYS THE INFORMATION ABOUT WHAT THIS PROGRAM DOES
//AND TAKES INPUT FROM THE USER IN A SPECIFIC FORM.
cout << "This program will determine the height of a person based on their gender and the length of their thighbone." <<endl;
cout << "It will also determine if this height is consistent with the height estimated by the bones in their feet." <<endl;
cout << "" <<endl;
cout << "What is the person's gender? Please type M, m, F, or f: " <<endl;
cin >> gender;
cout << "What is the person's estimated height based on the bones in their feet (in inches)?: " <<endl;
cin >> w;
cout << "What is the length of the person's thightbone (in inches)?: " <<endl;
cin >> y;
}
bool z (float z) //Determines whether the two measurements are close to each other.
{
if (z <=.5 && >=(-.5)) {
cout << "The two estimates are consistent with each other." <<endl;
else
cout << "The two estimates are not consistent with each other." <<endl;
}
bool gender (char gender) //Return an error message if the gender is input improperly.
{
if (gender != 'f' || 'F' || 'm' || 'M' )
return << "Error. Please restart program." << endl;
else
cin >> gender;
}
I'm at a loss for what to do next. Thanks very much for any suggestions anyone might have.
Eh... measure yourself and a few willing friends. People's ratios will be different, but try an average of the ratio of the length of their thigh bones to their actual height. Have two ratios, depending on sex.
Anyways, in your gender function, call another function known as (say) float estimate()
This function will return a value of type float, which you can then use for comparisons.
I recommend that you define your variables on a global scale, though (outside of any functions). That way, all your functions can use them without having to take them as arguments.
In this function, you'd multiply the fraction selected by an if-else clause (the fraction looks like height/length of bone) by the length of their bone to get your height estimate. I recommend that you return this value.
I'll let you figure out how to solve the deviation problem.
-Albatross
OH! EDIT: You should probably define your functions before your main function.
#include <iostream>
#include <cmath>
#include <string>
usingnamespace std;
//THIS FUNCTION DISPLAYS THE INFORMATION ABOUT WHAT THIS PROGRAM DOES
//AND TAKES INPUT FROM THE USER IN A SPECIFIC FORM.
void GetAndTakeInfo(double &tl, double &mh, double &fh, string &gender){
cout << "This program will determine the height of a person based on their gender and the length of their thighbone." <<endl;
cout << "It will also determine if this height is consistent with the height estimated by the bones in their feet." <<endl;
cout << "\n\n";
cout << "What is the person's gender? Please type \nM or m for male \nF or f for female " <<endl;
cin >> gender;
while(gender!="M" && gender!="m" && gender!="F" && gender!="f"){
cout << "You have entered an unknow comand, pleas type \nM or m for male \nF or f for female ";
cin.clear();
cin.ignore();
cin >> gender;
}
cout << "What is the person's estimated height based on the bones in their feet (in inches)?: " <<endl;
if(gender=="M" || gender=="m")
cin >> mh;
else
cin >> fh;
cout << "What is the length of the person's thightbone (in inches)?: " <<endl;
cin >> tl;
}
bool ConsistentOrNot(double dif){
if(dif<=(0.5) && dif>=(-0.5))
returntrue;
returnfalse;
}
int main (){
string gender; //THE PERSON'S GENDER
double mh; //THE HEIGHT OF A MAN BASED ON THE LENGTH OF THEIR BONES
double fh; //THE HEIGHT OF A WOMAN BASED ON THE LENGTH OF THEIR BONES
double tl; //THE LENGTH OF THE PERSON'S THIGHBONE
double dif; //THE DIFFERENCE BETWEEN THE TWO HEIGHT ESTIMATES
GetAndTakeInfo(tl,mh,fh,gender);
double MF(1);
double FF(1);
if(gender=="m" || gender=="M"){
//some formula for the height let us call it MF
dif=mh-MF;
}
else{
//some formula for the height let us call it FF
dif=fh-FF;
}
if(ConsistentOrNot(dif)==true)
cout << "A good result" << endl;
else
cout << "Not good" << endl;
return 0;
}
Hmm... odd... what GCC version are you using, or was this on Microsoft? Because when I run it through and when I put the data in the first time they ask me, (I swear, it's only m, f, M, or F) I get SEGFAULT.
i is not, but sometimes people just need to get it done and do not have time to do it, a few days ago i hade to write a "homework" for my course, but i had 4 other exams and very little time, i wolud apriciate if someone had writen me the code. and after all people can learn by looking some codes. and that is what i expect them to do.
I'm not here to be a teacher, or that someone could say:"Hey that dude knows it" i'm here because i love doing this stuff, and if i have done something i will give it to someone who needs it.
The vast majority of this board disagrees with that attitude. People will learn far more if they figure it out for themselves with some guidance than if the answer is given to them.
Also, I understand having very little time to do something, but honestly that's their responsibility, not yours.
maybe it is just me, but i need to se something in "practice" to understand it, from just reading books, tutorijals etc i don't get all, you get the most out of such things, but u just need to see it in some code to fully understand what a thing is doing.
some things just aren't intuitive...
after all didn't you sometimes wish to have a code done to look at it or to have someone writte it for u cauz you just didn't have the time for it?
I agree. That doesn't, however, mean that giving them all of the code is the most productive thing to do. If they ask a specific question, giving them specific code snippet can allow them to see code examples while still making them work a bit to understand the concept.
I did attempt to communicate to you on at least one occasion that at least the veterans and elite of this board don't give out full answers. I will try to explain why, as I might not have made myself clear about that.
Yes, if you have your code written for you, you might pass your class and get to hang out with your friends more. What will you have learned? Nothing. The human mind defaults to taking the easiest path out, and in his case it's changing the header so that it has his name, and correcting any misspellings that may have happened. You don't put enough comments in your code to explain what each line does for you to justify what you're doing.
I recall that I have released the full code to a problem only once and that was under a circumstance where I was 100% certain that the guy had no idea what to do, where to even begin, how to translate my abstract English into concrete C++. Afterward, I was sure to send him a PM explaining that if he wants to learn C++, he should ask me questions which I would answer, but not release any more full solutions. I admit, that wasn't the best solution. However, he did PM me with a question later. ^_^
If you want to help others on the forum, then releasing the full code isn't helping them. Selling LSD to a depressed person isn't help. It give a temporary escape, then fades away as the problems come back (okay, that was a very extreme example). Releasing the entire code that is intended to be copied and pasted and compiled and just run without teaching every little detail is much like that LSD. It solves one problem, but soon the student will get another. They will come back here, and ask for another one.
Those of us who realize this either explain what to do in English, or give a code snippet to work around (I give snippets of English). If it's a small syntax error, then I just tell them what's wrong with their code and be on my way. However, if there is something wrong or missing regarding their fundamental structure, then I slip them a hint about what to do, then increase the size of the hint, then add code snippets, and maybe even explain what to do in English. However, I never give out the entire code and leave some ambiguity in all my English explanations.