My first program!

I feel sort of accomplished after one day of learning the c++ language. I just made my very first program. All it does is ask you to enter a number and it will generate random numbers based on how many you entered. I know its very basic but it's a good start, I think..?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime> // file needed to use the time function

using namespace std;

int main()
{
srand(time(0)); 

int z;

cout << "Enter the amount of random numbers that you want to genreate:";
cin >> z;

for (int x = 1; x <= z; x++) // for loop, starts with 1 and then keep looping until it reaches z which is the number you enter. x++ is just a fancy way to keep adding 1.

{
    cout << 1+(rand())%16 << endl; // takes a random number, divide it by 16 and take the reminder of it then add 1 to it
}

}


Any criticism is welcomed, I want to learn, learn and learn, don't worries, my feelings won't be hurt.
Last edited on
Code is good.
Can you teach me how to give the person the option to continue entering more numbers when the number of z loop finishes? I know it's something having to do with not equal to -1 so unless they don't put -1 the command won't close.
Only thing I find annoying is the lack of indentation, although that's probably because you edited in the code tags after the indentation was already lost.

More nitpicky stuff:
z could probably have a better name like no_of_numbers or something to make it more obvious.

Your comments aren't really helpful, but then again you aren't building a large program. Usually you should comment why you are doing something. Only comment what for large sections or places that would be hard to figure out (or you could rewrite the code so it is easier to read :P)

Finally, I would suggest avoiding using namespace std;. It might save some typing but it pulls tons of stuff into global scope (like how rand() does not need to be std::rand() ) and sometimes that can be annoying (you can't have a variable named count) or even generated errors (you have another library that happens to have a function that is called the same name as one in the STL)
If you want to avoid typing std::, use specific using clauses like:
using std::cout;
It is more specific and means only what you want will be put into the global scope.
Thank you firedraco, I've noted all your tips down and I will remember them, especially the one about commenting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
srand(time(0)); 

int z;

while(1)
{
cout << "Enter the amount of random numbers that you want to genreate(-1 to exit):";
cin >> z;
if(z==-1)
   break;
for (int x = 1; x <= z; x++) // for loop, starts with 1 and then keep looping until it reaches z which is the number you enter. x++ is just a fancy way to keep adding 1.

{
    cout << 1+(rand())%16 << endl; // takes a random number, divide it by 16 and take the reminder of it then add 1 to it
}
}

}
It's pretty good for starters program.
I suggest several things:
1) you don't need <cmath> header
2) I suggest using <random> instedad of <cstdlib>. You don't need whole standart C library here.
3) Think about getting rid of "using namespace std". When you get to namespaces in your study, make sure you understand what this statement means and what is the downsides of it. For now you can just move it from global scope to your main() function.
4) Adopt some codestyle. Yours is very good for a beginner, but some things like empty string between for(..) and { ticks me off.
5) Parentheses around rand() on line 20 are not needed. Did you have something other in them and forgot to remove them?
6) You forgot return 0; at the end of main(). Always return from main()
@tvrameshmc, can you explain while(1) and why break; is used here? (I know what break is used for)
while(1) is equal to while(true). In short it an infinite loop. It will repeat ofer and over suggesting to enter number and generating random numbers. To exit we compare entered number to -1. If user entered -1 then we use break; to exit while loop.
@MiiNiPaa

1) Sorry about that I forgot to remove it when I was testing out different things.
2) Got it, now I know there's a file that is specifically for randomization.
3) I will look more into namespace, right now I am calling the library because its makes it easy on me, I think?
4) Thank you I will remember not to put extra spaces.
5) I just realized that too, I think it makes it a little neater?
6) I am using CodeBlocks and a few programmers told me that I don't need to include return 0 in my main functions because it will automatically do it even if I don't write it, not sure if this is good.

Thank you so much for the tips and help, thank you.
Last edited on
while is also like a for loop here we used infinite loop(never ending loop).

break is used to break the current loop which is while.
I am using CodeBlocks and a few programmers told me that I don't need to include return 0 in my main functions because it will automatically do it even if I don't write it, not sure if this is good.


Well, there is such little thing as C++ standart. Think about it. If you ever move on other compiler which not so forgiving? What if your program will be launched by other programs and whole program chain will stop because it returned some non-zero garbage from stack which will counts as an error?

In short: use return 0;/return some_error_code; in main. Standart console program template in Code::Blocks should include it AFAIR.
while is a looping construct like for

The terminating condition goes in the parentheses. So, in while (1) we're saying that the loop doesn't terminate ever on the loop condition. That's where the break comes in. All in all, that was a poor excuse for a loop. It's generally a good idea to get the condition you're looping on in the loop condition.

I might've rewritten that code this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand(std::time(0)); 

    char const* prompt = "Enter the amount of random numbers that you wish to generate: " ;
    const unsigned sentinel = 0 ;
    const unsigned maxRandVal = 16 ;

    unsigned nNums ;

    std::cout << prompt ;

    // while input is successful and it isn't our sentinel
    while ( std::cin >> nNums  &&  nNums != sentinel )
    {
        std::cout << "{ " ;
        for ( unsigned i=0; i < nNums; ++i )
            std::cout << (std::rand() % maxRandVal + 1) << ' ' ; 
        std::cout << "}\n" ;

        std::cout << prompt ;
    }
}


The standard guarantees that if execution falls off the end of main, it will be as if the program had returned 0.
Last edited on
Topic archived. No new replies allowed.