If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[Advice Needed] Noob Code!

Hello all, I recently began to learn C++ from reading a book. I'm up to chapter 4 of 24 and thought I'd like to try out what I've learnt to far. My program works fine, however I'd like some advice on the way I have written the program and whether or not it can be simplified and how. The program overall is to work of the area of some shapes. The user needs to input the shape he is using the the information required to work out the area.

Also where the result of a calculation is not necessarily an integer (such as area of triangle) rather than returning an int, float could be returned.

I agree with GCDEF about not putting output statements in a function. What if you wanted to use these functions when you were writing a graphical program and the console was not available? Or you wanted output to go to a file instead? A function should really do only one thing (but well!) and not have any un-expected side-effects (such as outputing text).

Question for you to consider. Why have you used short int rather than just int?

Re: [Advice Needed] Noob Code!

Thank you both for the feedback. I have now removed the output from the functions and included the 'using namespace std;' into the code. I have used a short int instead of a int as on my computer int is bigger than an int so I did this to make the program smaller.

I'll look into the switch thing, but I'd rather wait to learn fully how to use it later on. Thanks for telling me about it though guys :P

Re: [Advice Needed] Noob Code!

Originally Posted by Kegs

Thank you both for the feedback. I have now removed the output from the functions and included the 'using namespace std;' into the code. I have used a short int instead of a int as on my computer int is bigger than an int so I did this to make the program smaller.

I'll look into the switch thing, but I'd rather wait to learn fully how to use it later on. Thanks for telling me about it though guys :P

Switch is really just a stylistic thing. Either is okay. As to making your program smaller, 40 years ago saving every byte possible was important, at that level, it's not something you need to be concerned with these days.

Re: [Advice Needed] Noob Code!

Originally Posted by Kegs

Thank you both for the feedback. I have now removed the output from the functions and included the 'using namespace std;' into the code. I have used a short int instead of a int as on my computer int is bigger than an int so I did this to make the program smaller.

Personally, I'd advice against putting "using namespace std;" in your code. It doesn't save much typing (which is always a bad reason, by the way), doesn't make your code that much more readable and it can be an annoying habit later on, when you will work more with header files.

Regarding types, you should default to using (unsigned) int when you need an integer type and double if you need a floating point type. Only use float instead of double if you need to save memory (or disk space) or if you have profiled your application and using float calculations is that much faster than using doubles. Otherwise, just go for the extra precision.

One thing that wasn't mentioned before, is that it's bad style to define all your variables at the beginning of a function. You should try to only define variables once you can give them a meaningful initial value.

Cheers, D Drmmr

Please put [code][/code] tags around your code to preserve indentation and make it more readable.

As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

EDIT: I have tried this out, and for each if statement, I need to reinitialize each double. For example, I do double Height, in the if Triangle bit, but then again in if Square, I need to do double Height again. Is this ok or would it be easier to just do them all in the begging like I did to begin with? Thanks

Re: [Advice Needed] Noob Code!

Yes, that's what meant. However, as you have found, this brings about the concept of variable scope which you probably haven't come across yet in your tutorial book (look for a section(s) on scope). Basically, identifiers declared inside a block (between {..}) have block scope. Block scope begins at the identifier's declaration and ends at the terminating }. Outside of this block scope, the variable is not defined. That's why you had to define the variables in each 'bit' of your program. When they were defined at the beginning they were available to all the 'bits' but when defined in a 'bit' they are only available in that 'bit'. As Radius is only appropriate to a circle, for example, then good practice says that Radius should only be defined in the block that processes circle. Also that variables should be either initialised when defined or as soon as possible after. This is to make sure that a variable is not used before its value has been set. Some compliers on some warning levels produce a compile time message about this.

Just a further observation. The height and width of a square are the same. If they are not then it is a rectangle. So the area of a square is just width * width.

I'd advise against putting "using namespace std;" in your code ... doesn't make your code that much more readable and it can be an annoying habit later on, when you will work more with header files.

I agree that you don't put 'using ...' in any header file. However, unless you are using multiple namespaces then my personal opinion is that having 'using namespace std;' after the #include in your program and not using std:: everywhere does make the code a lot more readable - especially when later you'll start to use other standard classes like string, vector etc. What does your book have to say on this?

Re: [Advice Needed] Noob Code!

Originally Posted by 2kaud

I agree that you don't put 'using ...' in any header file. However, unless you are using multiple namespaces...

So, what do you do when you introduce, say, a boost library to a cpp file that you created before? Do you remove "using namespace std;" and spend maybe 10 minutes to fix all the compiler errors? Doesn't seem like a good idea to use a coding style that is so fragile to changes.

The problem I meant with using header files is when you have class definitions or function declarations in a header file and the implementation in a cpp file. If the (member) functions have arguments or a return value that is a class in the std namespace, then the signature will look different in the header file and the cpp file. E.g. in the header file you have

Re: [Advice Needed] Noob Code!

Right, so I've had a bit more of free time so I decided to learn some more. I've learnt about basic classes and the switch method which was previously told here. I have a couple of questions: Firstly, is this better written code? Is there anything in which I still need to improve on.
Secondly, I'm using Code::Blocks to write my code and when I click 'New' there is an option for class. (I'm not sure if this will be detailed in a later chapter), however would it be possible to create my Class there and refer to it in my main.cpp?

Re: [Advice Needed] Noob Code!

The main problem I have with your class is that it doesn't make much sense to me. I understand what you are doing with the class, but it's backwards. Your class does one thing, and that's find the area of some shapes. Now, typically, you'd make a basic shape class. From this class you can create various child/derived classes such as: Triangle, Rectangle, Circle, Trapezium, etc. Each of these classes would derive from your base shape class. Then, each of these classes would have their own area function (as well as other functions). Within main you'd get the type of shape, the basic data, and create an object for that shape and then just call its area function.

As for Code::Blocks, creating a new class that way will add two new files to your project. You'll have a .h file where you define your class, then you'll have .cpp file where you actually implement the class. Once you get that done, you can then include the header for the class in your main.cpp file and use the class accordingly.