Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Simple program.. Am I developing any bad habits?

(Mar 2, 2014 05:14 PM)asillychilly Wrote:  
Code
int generateFunction()
{
    int intGen;
srand(time(NULL));
intGen = rand()%10;
return intGen;
}

You could just do return rand()%10, instead of storing it in a variable.
That's pretty good and that's how I generate random numbers. There are other ways, but for learning purposes, that's just fine.

Like someone said, you're listing your header files in C format instead of C++. And I don't see anything wrong with 'using namespace std'. Some older programs might not use that so it's good to be aware that's not always used.

Also, that will generate a number 0-9 not 1-10. No biggie, but just something to be aware of.

Lastly, awesome of you for making a post like this. Too many people make post like "Where I learn C++?!?!" Or they clearly post some copy and pasted material none of which they know nothing about. You learn by doing, just like what you're doing. And then ask for guidance to make sure you're on the right track, just like you're doing.


Possibly Related Threads...
Thread / Author Replies Views Last Post
by Lukas555May 19, 2024 09:21 AM 3 199 May 19, 2024 09:21 AM
Last Post: DaddyBux
by SIM-5262639Jan 28, 2023 03:00 PM 0 226 Jan 28, 2023 03:00 PM
Last Post: SIM-5262639
[Python] by DarkTitanJul 27, 2020 12:33 PM 0 118 Jul 27, 2020 12:33 PM
Last Post: DarkTitan