Help with string in program

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.

I have a good portion of the program
As far as I know, none of the variables in main() will be reachable from findcode(out of scope).
Try declaring them globally and see if it helps
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

char floor, f;
char roof, r;
double length, L;
char beam;
double maxdeflect, Dmax;

main()
{
}
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.
http://cplusplus.com/forum/beginner/38362/ ... ...

It's laid out here.
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.
closed account (D80DSL3A)
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.
closed account (D80DSL3A)
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.

#include <iostream>
#include <string>
using namespace std;

double maxDeflect(double Lfeet, char beamType);
int main()
{
double length, L;
char beam;

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;

return 0;
}

double maxDeflect ( double Lfeet, char beamType )
{
double Linches = Lfeet * 12.0;
if(beamType == 'f' )
return Linches/240.0;
if( beamType == 'r' )
return Linches/180.0;
}

The compiler works, but something is still wrong with the program. It outputs 1 for both.
Also thank you for you help fun2code. It is very helpful.
closed account (D80DSL3A)
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.
closed account (D80DSL3A)
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;
}

This uses just a single return statement.
Last edited on
Thank you so much fun2code. The program works and I better understand how to do loops better. Hopefully you will answer my questions in the future.
Topic archived. No new replies allowed.