Probably a simple program, but I'm failling

  • Thread starter Thread starter sheepcountme
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to compute the deflection of a wooden beam based on its type (ceiling or floor) and the type of wood used (pine, cedar, or oak). Participants address various coding errors and suggest improvements to the program's structure and logic.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in writing the program and mentions specific errors encountered, such as syntax errors related to variable identifiers.
  • Another participant suggests using a single character for input to represent beam types, such as 'c' for ceiling and 'f' for floor.
  • Concerns are raised about using a single '=' for condition testing instead of '==' in if statements, which is a common mistake in C++ programming.
  • There is a discussion about potential issues with dividing a float variable by an integer literal, with conflicting opinions on whether this is a problem.
  • Participants point out syntax errors in the structure of if statements and suggest that the test expressions should be enclosed in parentheses.
  • It is noted that the program has an excessive number of variables, and suggestions are made to simplify the variable names and reduce redundancy.
  • A participant expresses frustration with their coding abilities and seeks further assistance.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections and improvements in the program, but there are differing opinions on specific technical issues, such as the division of float variables and the necessity of certain variables.

Contextual Notes

Participants highlight missing if-else structures and the need for proper variable usage, but the discussion does not resolve all technical uncertainties or errors in the code.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in computational mechanics or structural engineering applications.

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
 
Technology news 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
 

Similar threads

  • · Replies 75 ·
3
Replies
75
Views
7K
Replies
7
Views
16K
  • · Replies 21 ·
Replies
21
Views
5K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K