My assignment is stated as:
The maximum allowable deflection of a beam depends on its function. For a floor, the typical maximum allowable deflection, in inches is: Dmax = L/240, while for a roof beam Dmax = L/180, where L is the length of the beam in inches. Using these formulas, write and test a function, maxdeflect, that accepts the length of a beam, in feet, and the type of beam (floor or roof) as a character code and returns the maximum allowable deflection.
Include the function in a working program. Make sure your function is called from main and correctly returns a value to main. Have main use a cout statement to display the value returned. Test the function by passing various data to it.
Note: Be sure the program has a function in it as described above. Be sure the function has a comment that tells what it does.
That's true but you shouldn't declare them global; that's bad practice. Make the deflection calculation function take whether the beam is floor or ceiling and it's length as parameters. And is there a reason you didn't simply use your old thread?
Anyway, to deal with passing the "type" of beam you have, you could use an enumeration or integers with set values to name two options.
Is there any way you can show me how to do that Zhuge. Since this is pretty new to me, interpreting c++ lingo is kind of difficult. And sorry if I didn't use the same thread. I made a new thread because I wrongly assumed that old updated threads don't get as many replies or none at all. But now I know, so thank you.
Like I said, it's laid out there, meaning that most, if not all of your problems can be solved by reading what people wrote. Also, if it's "outdated", comment on it, then it will be updated.
Most of the advice given by Zhuge is actually contained in your problems instructions:
write and test a function, maxdeflect, that accepts the length of a beam, in feet, and the type of beam (floor or roof) as a character code and returns the maximum allowable deflection.
I think that describes something like this:
1 2 3 4 5 6 7 8 9
double maxDeflect( double Lfeet, char beamType )
{
double Linches = Lfeet*12.0;// convert given length from feet to inches
if( beamType == 'f' )// single quotes for a char value are needed here
return Linches/240.0;
if( beamType == 'r' )// no need for else if because of the return above
return Linches/180.0;
return -1.0;// check return value for this to see if something went wrong (invalid beamType)
}
This should help. Post back if you still have trouble from here on.
so am I supposed to just make these changes to my prototype part of my program, and still leave everything the way it is under int main(). And would i also still need to to include, include <string>. And also how would i change my last cout that outputs the maxdeflect where i have the findcode(char beam)? Thank you for your help.
Sorry, you must be just starting out.
The above function replaces the findCode() so you can get rid of that.
You can either place the code I gave above the main function or below it. If you place the code below main() then you must have a "prototype" for the function before main(). double maxDeflect( double Lfeet, char beamType );. Your choice, but the compiler must be able to know what maxDeflect() is before it finds a call to this function.
Most of the variables you have in main() can be eliminated. You really only need two of them:
1 2
char beam;
double L;
You could keep double Dmax; too if you wish to check the return value from maxDeflect() before giving the output. Or you could output the return from the maxDeflect() directly.
I hope I haven't just confused you further with all this.
You will definitely want to get rid of the variable double maxDeflect; because that will cause a conflict with the function of the same name(I think).
Please have some new code to post if you need further help. That would reveal better where the continuing confusion lies.
cout << "Enter length of the beam in feet => ";
cin >> L;
cout << "Enter type of beam, (floor roof) => ";
cin >> beam;
cout << "The max deflect is " << maxDeflect << endl;
I see a single goof. You forgot to supply the arguments in the function call here: cout << "The max deflect is " << maxDeflect << endl;
I'm a bit surprised that doesn't cause a compile error!
You must pass the function the values it needs to work with. Try maxDeflect( L, beam ) where you call the function.
Make sure you are entering just f or b when prompted for type of beam. If you enter something else the function is supposed to return -1. Nevermind, I see you left that part out. Without that last return though a bad value for beamType will result in NO value returned from the function. I'm not sure what that could cause!
so do I put what you wrote down before the int main()? Is that where you call the function, or is it somewhere else? So do I just need to change where you call the function, or do I also need to make it where if you type anything else in it = -1? Thank you again and sorry that I don't fully understand C++ lingo.
Just change this: cout << "The max deflect is " << maxDeflect << endl;
To this: cout << "The max deflect is " << maxDeflect(L, beam) << endl;
Everything else looks good.
I was referring to the line you left out of the function I gave. Line 8 return -1.0;
Maybe a better version of the function would be:
1 2 3 4 5 6 7 8 9 10
double maxDeflect( double Lfeet, char beamType )
{
double Linches = Lfeet*12.0;// convert given length from feet to inches
double dMax = 0.0;
if( beamType == 'f' )// single quotes for a char value are needed here
dMax = Linches/240.0;
if( beamType == 'r' )// no need for else if because of the return above
dMax = Linches/180.0;
return dMax;
}