Probably a simple program, but I'm failling

  • Thread starter sheepcountme
  • Start date
  • Tags
    Program
In summary: In fact, you probably don't need any of the input variables at all. See if you can simplify your program to just the function and the data types.In summary, the program needs to ask if the beam is a ceiling or floor beam, what type of wood it is, and then based on this, it calculates the deflection.
  • #1
sheepcountme
80
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;

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

if
(beam=ceiling)

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

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

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

cout << deflection;

cout << "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
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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
 
  • #4
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.
 
  • #5
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; {
cout << "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.
 
  • #6
Ughhh, I am so awful at this!

Thanks for your help!
 
  • #7
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
 

Related to Probably a simple program, but I'm failling

1. What could be causing my program to fail?

There could be several reasons why your program is failing. It could be due to errors in your code, missing dependencies, or compatibility issues with your system. It is important to carefully review your code and check for any errors or missing components.

2. How can I fix my failing program?

To fix a failing program, you first need to identify the root cause of the issue. You can do this by debugging your code and checking for any errors or by seeking help from other programmers. Once you have identified the problem, you can make the necessary changes to your code or system to resolve the issue.

3. Is there a specific programming language I should use for this program?

The choice of programming language depends on the requirements and functionality of your program. Some languages may be better suited for certain tasks than others. It is important to choose a language that you are familiar with and that is compatible with your system.

4. How can I prevent my program from failing in the future?

To prevent your program from failing in the future, it is important to follow good programming practices. This includes writing efficient and error-free code, regularly testing your program, and keeping your system and dependencies up to date. It is also helpful to document your code and save backups in case of any future issues.

5. Are there any resources or tools that can help me with my failing program?

Yes, there are many resources and tools available to help with a failing program. Online forums and communities, as well as programming tutorials and guides, can provide valuable insights and solutions. There are also debugging tools and software that can assist in identifying and fixing errors in your code.

Similar threads

  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
7
Views
15K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
Replies
8
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Back
Top