| skn (9) | |||
|
This was a simple program I wrote to allow me to decide between multiple choices and also give it a personal touch. I'd like to know if I have some bad habits, ways to improve it and such. I also want to know how my commenting skills are, and if I'd need more or less of them.
| |||
|
|
|||
| hamsterman (4435) | |
|
There is little point to include <sstream>. Same can be done with cin, with a little bit of code added. But then, I guess there is no harm here. In C++, arrays start from 0. Since you start your for loops from 1, you're just ignoring the first element. That is why you declared an array of 20 elements as "objects[21]". This is wasteful and slightly confusing. You should fix that. You're commenting a lot more than you need to. Most of the code explains itself. Unless you're doing something weird, it would be best if you only described each function in a couple of lines above it. Also, you only need to call srand once. Here it does no harm. If the loop was fast, you might get not random results though. | |
|
Last edited on
|
|
| skn (9) | ||||
^- I have a problem understanding this one, what exactly do you mean by that? From the C++ book i use he declares the namespace's for rand\srand and time the way I do and also includes those libraries. -- So I should just have the array from 20, and instead using a + 1 and - 1 where it's needed? Such as this:
-- I use the sstream for the input loop to make sure it's a number, as it turns into an unlimited loop if someone accidently give a non integer value with cin. I also read(might have been here can't remember) that this is a suggested form for input instead of using cin. Thank's for the constructive criticism | ||||
|
|
||||
| CodeMonkey (661) | ||||||||||
hamsterman is not entirely correct in this. The C++ standard says that the contents of the C headers shall be put in C++ header along the lines of name.h becomes cname. It also says that, in the C++ headers, declaration and definitions (except macros in C) are within the namespace scope of namespace std. So your book is correct. -------------
| ||||||||||
|
Last edited on
|
||||||||||
| skn (9) | |
|
Object_amount is a user input\choice. Which would be between 2-20, however unless I remove 1 from it(with the 20 array) i will always gain an higher value then planned. For instance: user input = 2 then the for loop is supposed to loop 2 times, however unless i remove 1 from object_amount it would loop 3 times. | |
|
|
|
| alhypo (13) | ||
Notice that CodeMonkey replaced the less-than-or-equal operator with the less-than operator in your loop condition statement. Those two loops are functionally the same, but using a single operator and not subtracting 1 is much more concise. | ||
|
|
||
| CodeMonkey (661) | |
|
Note the less than rather than the less than or equal to. Edit: Very slow typing on my part or not refreshing. :0) | |
|
Last edited on
|
|
| skn (9) | |
|
Ah of course my bad haha xD. Didn't even notice that. Thanks =). Now onto the comments, is it good practice to use comments to indicate the end of a loop like I do? Or should I drop that? | |
|
|
|
| CodeMonkey (661) | |||
|
On small loops where you can clearly see the starting point, it is probably not worth it. If you have a large loop with other indentation due to other constructs, it can be worth putting a comment on it. i.e.
| |||
|
|
|||
| skn (9) | |
| Ah I see, so using it for the main loop could be good. but for the smaller few lines one I have thrown in all over it's not needed. I see, thanks :). | |
|
|
|
| Framework (3242) | |
|
I would incorporate some sort of thread sleeping function such as Sleep( ) (<Windows.h>) within any loop that uses many clock-cycles. Sleep( ) suspends the execution of the current process thread to allow the CPU to execute other threads. This effectively cuts down the the amount of clock-cycles (CPU usage) your program uses. Wazzak | |
|
|
|
| skn (9) | |
|
How would I know how many clock-cycles a loop would run trough? And how can I know what argument to use with Sleep? I'm pretty new to programming and only learning trough the c++ book, no school or anything. | |
|
|
|
| Framework (3242) | |||||||
Unless there's a function within your operating system's API, there's no true way of knowing.
Same here. Wazzak | |||||||
|
Last edited on
|
|||||||
| skn (9) | |
|
So not doing anything wrong by for instance, using 50 as an argument? As it depends on a need-to need type of thing? Thanks for that code, quite useful to stop and pause the code for x-amount of seconds. | |
|
|
|
| Framework (3242) | |
|
Even 50ms is too long; 1ms would do the job. However, the time given depends on how much time you want to give to the CPU for the other threads. Wazzak | |
|
|
|
| ne555 (4385) | |||
| |||
|
|
|||
| Framework (3242) | |||
So it's not using too many CPU clock-cycles, that's why; it's called greed otherwise. Wazzak | |||
|
|
|||
| CodeMonkey (661) | |
| Framework, I can't quite see where you are coming from on this. | |
|
|
|
| Framework (3242) | |
|
See here: http://siber.cankaya.edu.tr/OperatingSystems/ceng328/node116.html The images aren't exactly pukka[what?], but the text is what you want. Wazzak | |
|
|
|
| ne555 (4385) | |
|
Let the scheduler take care of that, don't worry about others. Your program needs those precious cycles to accomplish its task. Considering Svein Kristian Nykaas's code, ¿where will you put those sleep calls? | |
|
|
|