Saying hi and posting my first code.
| jrobo1980 (4) | |||
| Hi, Like most people posting here in beginners i am new to C++ .. Although I do not have a specific question in mind I would like to post my first code (besides "hello world") for input and what not.. although I know their are easier and more effective ways to write this I am at my limits as to what i've learned so far (using the cplusplus.com tutorial) with the exception of pointers which i am still trying to wrap my nugget around. basically this code does simple geometric math for 4 different shapes.. I am using Bloodshed Dev-C++ and this is a console program. I assume I could use pointers to ensure that the memory allocation uses as little memory as possible (if im understanding pointers correctly) and i know in this small program it doesn't matter but i am sure it does in larger ones.. well on to the code.
thanks J | |||
| mahlerfive (117) | |||
| I would say you are developing some pretty good habits for a beginner - you have good indentation, are trying to make the program modular, and are trying to reuse code as much as possible. A few small things: 1 - There are a lot of accepted ways to line up your braces ( {, } ), but all of them have one thing in common - you should line the ending brace up vertically with the start of the structure that opens it. Ex
2 - It's a good idea to avoid NOT including braces in single statement if's. This is because if you go back later and add more code into the if, and you forget to add the braces, you will have a VERY hard time debugging what is going on. I would suggest always putting on braces. 3 - The only major "design" issue I would have, is that the Square(), Circle(), etc. functions should NOT call Root(). This is because you should be able to call these functions from anywhere, not just Root(). To solve this problem while removing the Root() call, you should instead have a loop inside the Root() function to continuously redisplay the menu and call the appropriate functions. | |||
| mahlerfive (117) | |||
| Also, the purpose of pointers is not to optimize memory usage. The main reason to use pointers (although not the only one), has to do with function scope. If you don't know about scope, you should read up on functions and scope more first. Basically, the scope of a function is the variables a function can "see" and access. A function can't access variables that are outside of itself, and although you can send variables as parameters to a function from outside, if they are passed normally (by value), then the function only gets a copy of the variables, so if you alter it, it will not change the variable outside the function. If you instead pass a pointer to the variable you want to change to the function (passing by reference), you can now directly change the variable's value in memory from inside the function, and when the function is over, these changes will be seen outside the function as well. | |||
| jrobo1980 (4) | |||
| Thanks for the input. Most of my habits are from prior coding experience (basic and pascal although those were more than 11 years ago) however this is my first crack at C++ (probably should have said this in my original post). I see what your saying about lining up the brackets ( {} ) I thought of that while I was writing this but after scrapping the first two design ideas it got lost in the process. I think I understand what your getting at with the Root() calls however i am coming to the end of a 12 hr shift and my brain is running on empty so once I get some sleep i will try to rehash out the issue .. what you said about pointers is what I've been trying to wrap my head around in the tutorials. It will probably be one of those things that I'll have to use once or twice before I really get it. The reason I was thinking about using less memory or better memory allocation was because instead of letting the computer assign a different memory address for each variable each time it is called you could reuse the same memory address over and over.. once again this assumption has a lot to do with not know how or when a addressed location by a variable is cleared or how C++ assigns variables in a memory aspect. Once again I could still be thinking of that idea wrong. thanks again | |||
| Duoas (1595) | |||||
| Be careful how you present your advice. If it is opinion, don't state it as fact. 1. His indent style is called Banner style. (And a couple instances of Pico style.) http://en.wikipedia.org/wiki/Indent_style There are several things to remember about indent styles - there is no such thing as "The One True Brace Style" --no matter how much K&R advocates (or <your favorite style advocates>) would like you to believe it. - when coding, consistency is more important than pretty-printers - when working on a team project, use the team project's style guidelines - everything else is opinion and fluff 2. It is a good suggestion, but not a good idea. Readable code does not depend on braces. The following are equally valid: if (foo) if (foo) { baz(); baz(); } ...and equally easy to modify with more or less statements. Anyone with minimal competency in C or C++ can recognize the differences. 3. Agreed. The fatal issue is that the stack always grows but never shrinks. For a small program like this it probably isn't an issue, but it is a bad habit. C++ forbids users from calling main() in their programs (and it has always been a bad thing in any case) in part for this very reason. The Root() function is just a substitute for main(), so that same flaw is not obviated. When doing something multiple times, a loop is almost always the better choice. You can fix it easily enough, due to your nice, modular design: 1. Change all calls to Root() to a simple return. (Your functions all say they return int, but only GetUserChoice() and Root() actually do. You should also change all those to void.) 2. Change main() to use a loop:
3. Add a non-terminating return code to the end of Root() (between lines 48 and 49):
Hope this helps. | |||||
| jrobo1980 (4) | |||
| Duoas thanks for the fix for the root() flaw in this. I didn't know that return could be used like that. | |||
| Duoas (1595) | |||
Er, yeah. Just to make sure we aren't mis-reading each other, my main() lines 3 and 4 are distinct (unrelated) statements.
| |||
| jrobo1980 (4) | |||
| Oh i was talking about use of return in place of the root() calls.. | |||
| firedraco (847) | |||
Return is used to make the function...er...return the value...i.e.:
| |||
| rajenipcv (8) | |||
| Good discussion guys! OP (owner of the post) has a good understanding as a beginner. All the best. Rajendra | |||
| Duoas (1595) | |||
| Agreed. :-) | |||
This topic is archived - New replies not allowed.
