Create a structure Rectangle with data members length and breadth in C++
Okay guys. I appreciate both of your answers. Can you look at this again. I tried doing it your way, Lerner, doing the same thing for perimeter as you suggested for area. Now it compiles, asks for length and width twice each and the correct calculations as long as I enter the same values both times for length and width. can you tell me why? Ugh, I think my brain is fried.
Here is the output:#include private: Rectangle::Rectangle(float L, float W) // Scope function void Rectangle::setWidth(float W) void Rectangle::setLength(float L) //float Rectangle::perimeter(float L, float W) //float Rectangle::area(float L, float W) int main() MyRectangle.setWidth(width); return 0 ;
Creating Class rectangle, length & width, functions area, perimeter, get and setHi there. I am trying to create a class rectangle. It should have data members length and width of type float (which should default to 1). The class should have member functions that calc. area() and perimeter() and also separate get() and set() functions for length and width. These should require the user to enter valid length and width between 0 and 20.0 (use a do while loop). Then I have to add a draw function, but I'm not even there yet. Can someone look at my code and give me a general idea of where I am going? It all looks like a big mess to me and I just keep making it worse now! Thanks for taking the time to help me.
Here are some changes I've made. Can anyone offer feedback for me? I'll also post the errors I get.
Errors: ya-there are a lot! Anthony, I can't make head or tails of your program, but I am still learning C++. The tutorial on classes on this site uses the area of a rectangle as an example. It is a very good example to which you can compare your own program. Then you will get a good appreciation for just how confused your code is. Good Luck. Oay, I've cleaned it up quite a bit. It's making more sense now, but I'm still a few errors and need to add the draw part once I get this to run. Can anyone help? Thanks! Here's my code:
And here are my errors: Okay folks, still working here. Happy Halloween everybody! My code now compiles! Yay! Only I need some help to change the if/ else parts to do/ while and then I'm working on the draw function. I need to
add the draw function to class rectangle, truncate the numbers for the purpose of drawing them (ie 6.11 would become 6) I believe I can do this by converting the float to int. It would look something like this: Any new input? Here's the working code without the changes I am going to make:
Well, I got it to run correctly by just taking the set functions out of the constructor. Now I have to add a draw function to the rectangle class using length and width but truncatd to integers. I can use any character to do this. I'm working on this but I think I'm going about it wrong. Any input? Here's the code with the draw part I'm working on.
Your set functions don't act the way I expect them to...instead of setting the width/length to the value I pass, it ignores that and instead prompts me for a value between 0 and 20 for some reason. Oh, and your (IMO) poor indentation makes it really hard to read your program. I suggest you try to clean that up as well. Last edited on I will try to clean up the indents. I want to limit the length and width to 0.0-20.0, so is it doing what I want or is there an error in my set functions? Topic archived. No new replies allowed. |