Probably a simple program, but I'm failling

  • Thread starter Thread starter sheepcountme
  • Start date Start date
  • Tags Tags
    Program
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
sheepcountme
Messages
80
Reaction score
1
I need to write a program to compute the deflection of a wooden beam.

The program needs to first ask if it is a ceiling or floor beam. Then what type of wood it is made of (pine, cedar, or oak). Then based on this divide by certain numbers to determine the deflection.

Here are the numbers:

Ceiling: Pine (length/240), Cedar (L/250), Oak (L/270)

Floor: Pine (length/180), Cedar (L/190), Oak (L/210)

First off, I can't even get this program right, I'm completely awful at computer science, but I need it for my major. Unfortunately I also have a fulltime job and there's only one comp science tutor at our school so I can never go for help.

Here's what I've written:

#include <iostream>
using namespace std;

int main()

{

double deflection, length;
char beam;
char ceiling;
char floor;
char pine;
char cedar;
char oak;
char kind;
char ans;
char wood;

count << "Is the beam a ceiling or floor beam? (type in 'ceiling' or 'floor') ";
cin >> beam;

if
(beam=ceiling)

{
char wood;
count << "What type of wood is the beam? (type in 'pine' 'cedar' or 'oak') ";
cin >> wood;

if
wood=pine; {
count << "What is the length of the beam in inches? ";
cin >> length;
deflection=(length/240);
}
else if
kind=cedar {
count << "What is the length of the beam in inches? ";
cin >> length;
deflection=(length/250);
}
else if
kind=oak {
count << "What is the length of the beam in inches? ";
cin >> length;
deflection=(length/270);
}
}
else if
beam=floor

{
count << "What type of wood is the beam? (type in 'pine' 'cedar' or 'oak') ";
cin >> kind;
if
kind=pine {
count << "What is the length of the beam in inches? ";
cin >> length;
deflection=(length/180);
}
else if
kind=cedar; {
count << "What is the length of the beam in inches? ";
cin >> length;
deflection=(length/190);
}
else if
kind=oak; {
count << "What is the length of the beam in inches? ";
cin >> length;
deflection=(length/210);
}
}

count << deflection;

count << "Do you want to continue (Y/N)? \n";
cin >> ans;


while (ans == 'y'|| ans =='Y');


return 0;

}


I get a lot of errors that I don't understand such as:
syntax error : identifier 'wood'
syntax error : identifier 'kind'

I've looked for ages through our text and cannot find anything on how to correct this.

I've also got some missing if's and else's apparently but I cannot figure these out either.

We are also supposed to include the following into our program, but I'm just trying to get a program that works first before trying to modify it for these:

1) You must use a single non-void function that returns the maximum deflection in inches given the length of the beam in inches, its material, and whether it is a floor or ceiling beam.

2) Collect at least some of the user input as data type char; in C++ code, a single character is denoted by single quotes, as in
char initial = 'M';

3) Results should be output to 2 decimal places.

4) Use good commenting style in your program.


I know I'm probably a long way off on this program, but any help at all would be very very appreciated.

Thanks
 
on Phys.org
Just a couple of things that jump out at me right away:
1) If you're going to use a char data type, collect a single character for input. Ex, prompt user to type c for ceiling or f for floor.
2) There are a bunch of places where you are using = instead of == to test for a condition. Single = is for assignment.
 
There is also a possible problem with dividing a float variable by an integer literal

float length;
length/210 doesn't do what you think it does
 
NobodySpecial said:
There is also a possible problem with dividing a float variable by an integer literal

float length;
length/210 doesn't do what you think it does

I'm pretty sure it does exactly what was intended. I don't see a problem here.
 
Besides the problems already noted by Math Is Hard, you have a number of syntax errors in how your if statements are formed.
sheepcountme said:
if
wood=pine; {
count << "What is the length of the beam in inches? ";
cin >> length;
deflection=(length/240);
}

The test expression in an if statement needs to be in parentheses. For this code, it would look like this:
if (wood == pine)
{
// statements to be executed if wood == pine
}

The same is true for else if statements.

Another problem is that you have way too many variables, and you are confused about the difference between a variable and the possible values it can have. You don't need separate variables for the different types of wood; e.g., you don't need the pine, cedar, and oak variables. The value of the wood variable should probably be 'p' for pine, 'c' for cedar, and 'o' for oak.

You also don't need separate variables for ceiling and floor. The value of the beam variable should be 'c' for ceiling and 'f' for floor.

You also don't need the kind variable.
 
Ughhh, I am so awful at this!

Thanks for your help!
 
Mark44 said:
You also don't need separate variables for ceiling and floor. The value of the beam variable should be 'c' for ceiling and 'f' for floor.
Especially since floor is a bad name for a variable in c - it's the name of a standard math library function