NEED HELP WITH BASIC C++ PROGRAM

closed account (2N8vC542)
Ijkjbkjbkjbkjuiujbkjbkj
Last edited on
That's impossible. I'm not helping you, unless you give it a try. Come on, you can do it!
closed account (2N8vC542)
Then help, please this is the best i got. this hurts my brain, and dude my teacher sucks, didnt even teach how to do all these.

#include <iostream>

using namespace std;

int main(){
float ppp;
int pine,oak,mahogony,length,width,desk,area=,fifty,total,drawer;
cout << "Enter Inches Length of Desk" << endl;
cin >> length;
cout << "Enter Inches Width of Desk" << endl;
cin >> width;
cout << "Area Inches of Desk " << length*width;
cin >> area;

if ( area > 75 )
cout << " Additional $50.00 to Total Price ";




system("pause");
return 0;
}
Alright, lets try taking this step by step, I'll help guide you through this:

Alright. Something I don't like:

cout << "Area Inches of Desk " << length*width;

Looks kind of messy to me.

How about making a variable called a, and initializing it to the value of length*width, then printing out a, instead of "couting" length* width like that in a cout statement.

Change that and we'll move on =]

You might want to change system("pause"), to cin.ignore();... It's frowned upon in C++.
It'd go like this.. here's an example

1
2
3
int a = length * width;

cout << "StuffGoHere" << a << endl;
closed account (2N8vC542)
ooo ok, lemme see
Tell me when your done making those changes.
@Code Assassin
Unless you're going to reuse (length*width) a few times, it's stupid to make a temporary variable for it.
Alright.Thanks.
1
2
3
4
5
6
7
8
9
cout << "Enter Inches Length of Desk" << endl;
cin >> length;
cout << "Enter Inches Width of Desk" << endl;
cin >> width;
cout << "Area Inches of Desk " << length*width;
cin >> area;

if ( area > 75 )
cout << " Additional $50.00 to Total Price ";


"area" not well defined. Why not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

cout << "Enter Inches Length of Desk" << endl;
cin >> length;
cout << "Enter Inches Width of Desk" << endl;
cin >> width;

int area = length*width;                         // Better definition for area
cout << "Area Inches of Desk " << area << endl;

if ( area > 75 ){
total = area + 50;                                // Add 50 to total, if area is greater than 75
cout << " Additional $50.00 to Total Price "<<endl;
cout << "Total: $" << total ;
}
else{
        total = area ;
	cout << "Total is: $" << total ;
	}


return 0;


Try your hands on other part of the program and let me know what other problems you encounter.
Last edited on
This is a guide, to determining the kind of wood. You may have to use include string class function i.e #include <string.h> or use case switch (by assigning 1 for mahogany, 2 for Oak and 3 for pine). The "switch case" is preferred as it reduces error from user input.

How does it reduce error? If anything it would increase it.
As an aside, I have a problem with the "surface area" math. A desk is a three dimensional object with six surface planes. Determining the surface area requires additional info (height) and a different calculation than L*W*H as that would calculate volume. The right formula would be 2LW+2WH+2LH or 6LW if L=W=H. Bare that in mind as you "help" the OP.
Unless you're going to reuse (length*width) a few times, it's stupid to make a temporary variable for it.


I probably wouldn't call it outright stupid per se. I believe it mostly boils down to preference, and there are advantages and disadvantages associated with it. A disadvantage would be your program is allocating more memory than it should. But there are also some advantages, like the code looks a bit cleaner ( preference of the programmer ). It can also be a bit more readable and improve clarity, and in case you change your mind about reusing the computation then you just do so immediately without having to alter your previous code.

Actually, I use the (length*width) style. In my humble opinion though, I believe it really just depends on the programmer's purpose and preferences. Does he want readability (for maintenance) or efficiency (through saving memory), but it's probably not appropriate to call it stupid just because it's different from how we do things. That's just my opinion on the matter though if you don't mind me saying. =)
Last edited on
How does declaring an unnecessary variable make something more legible? It has the opposite effect of that, if anything.

It's only four bytes, anyhow.
well cause I've seen people use it to make conditions much clearer as well like

1
2
3
4
5
6
7
8
9
bool done = false; // temporary variable
while(!done)
{
    // do something
    if(condition)
    {
        done = true;
    }
}


so if creating a temporary variable would be such a bad idea wouldn't it be just better to do

1
2
3
4
while(condition)
{
    // do something
}


that's just what I mean though, there's nothing wrong with either styles it's just preference.

Edit:

I've also seen some people do something like:

1
2
int returnValue = myFunction(); // temp variable
cout << returnValue;


while some others do it like

 
cout << myFunction();


I think the most important thing here is balance, else we may end up with something like:

 
myFunc(myFunc1(myFunc2(myFunc3())));


due to following certain ideas too strictly.

That's only my opinion though and I could be wrong about it.
Last edited on
I was asked to remove the code so if anyone reads this post and wants the code I will PM it to you. So it's not completely taken away.
Last edited on
Someone mentioned using string and switch? You can't use those together because C++ doesn't recognize strings as a data type. You can use char array strings, but not typical strings.

Oh never-mind, I see what you were saying. In my code I assigned 1 to pine, 2 to oak, and 3 to mahog. I was confused because I thought you meant to use string and switch together.
Last edited on
Topic archived. No new replies allowed.