Help with Program

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.



The program that I have written so far, but somewhat confused on is this:


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

int main()
{
char floor, f;
char roof, r;
double length, L;
char beam;
double maxdeflect, Dmax;

cout << "Enter length of the beam in feet => ";
cin >> L;
cout << "Enter type of beam, (floor or roof) => ";
cin >> beam;

cout >> "The max deflect is " << findcode(char beam) << endl;

return 0;
}

string findcode(char beam)
{
if (beam == f)
Dmax = L/240
return Dmax;
else if (beam = r)
Dmax = L/180
return Dmax;
else
return "unknown";
}

I am kinda confused on how to use the character types as loop inputs and if I am even using the right setup for this. How are you supposed to input floor into the function and receive an answer for the floor equation, and the same for the roof. I am a beginner at this, so I am still learning all of the functions.
Thank you

Please use tags.

What is the file "stream"? Aren't you suposed to use "string" for strings or does "stream" define strings as well?

You have to declare any function BEFORE the main function, or you can use a function prototype.
1
2
3
4
5
6
7
8
9
10
11
int func (int, int);

int main ()
{
     //stuff
}

int func (int a, int b)
{
    /*Stuff*/
}

cout >> /*these >> should be <<*/ "The max deflect is " << findcode(char beam) /*you already declared char beam, remove the char here*/<< endl;

1
2
3
4
5
6
7
8
9
10
11
string findcode(char beam)
{
if (beam == f) // f is not declared in this function
Dmax = L/240 // Dmax is not declared in this function, also add a ;
return Dmax;
else if (beam = r) // else if beam == r, r is not declared in this function
Dmax = L/180 // ;
return Dmax;
else
return "unknown"; //is this even allowed?
}
The instruction is fairly clear, imo:
First find out what your function is supposed to return, so try this,
1
2
3
4
5
6
return_type function_name(function_parameters) 
{
    //function body
   type variable_name;//the variable you're supposed to return from your function, e.g the product of your calculations
   return variable_name;
}


now, you know from the question specs you have to return Dmax, a value you get from calculating L/240 or L/180, so the best bet is the type of Dmax is either a double or a float
thus, your function becomes, for example:
1
2
3
4
5
6
double function_name(function_parameters) 
{
    //function body
   double variable_name;//the variable you're supposed to return from your function, e.g the product of your calculations
   return variable_name;
}


next, from teh question you know your function must receive an input -your function parameters. which your specs state as: "accepts the length of a beam, in feet, and the type of beam (floor or roof) as a character code ", from this your function_parameters would be length_of_beam and type_of_beam:
1
2
3
4
5
6
double function_name(type_of_beam, length_of_beam) 
{
    //function body
   double variable_name;//the variable you're supposed to return from your function, e.g the product of your calculations
   return variable_name;
}

so, try to build up your function from these, as for this:
I am kinda confused on how to use the character types as loop inputs
it is for another 'lecture' :P

Your code has a lot of mistakes. Have you even tried to look over it yourself once?

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
#include <iostream>
#include <stream> //? "stream" is not a real header, I think you mean "string"

using namespace std;

int main()
{
    char floor, f;
    char roof, r;
    double length, L;
    char beam;
    double maxdeflect, Dmax;
    cout << "Enter length of the beam in feet => ";
    cin >> L;
    cout << "Enter type of beam, (floor or roof) => ";
    cin >> beam;
    cout >> "The max deflect is " << findcode(char beam) << endl;// cout <<, not cout >>
    return 0;
}

string findcode(char beam)
{
    if (beam == f)
    {
        Dmax = L/240
        return Dmax;//this was outside of the if structure before
    }
    else if (beam = r)
    {
        Dmax = L/180
        return Dmax;//again, this wasn't in the if structure before
    }
    else
    return "unknown";
}


That and Danny Toledo's post should help fix most of it.

Edit: @Danny Toledo: Yes you can return a string.
Last edited on
Topic archived. No new replies allowed.