# My introduction to C++. [Beginner's Problem(s)]

• 03-05-2012
My introduction to C++. [Beginner's Problem(s)]
I just started C++ again, after taking a summer school course on it.
That was a few months back, it's not offered during my normal school day.

I found some sample code from this website to use as my own, personal version of "Hello World!". Mainly used as a refresher.

The problems that I run into with this code, is that when I run this "game", the magic number always happens to be "41".

Code:

```#include <stdlib.h> #include <iostream> using namespace std; int main() {     int number=rand()%100;     int     guess=-1;     int trycount=0;     while(guess!=number && trycount<10)     {         cout<<"Please Enter a Numerical Guess: ";         cin>>guess;         if(guess<number)         cout<<"Sorry, That Number is too low! Please Try again!"<<endl;         if(guess>number)         cout<<"Sorry, That Number is too high! Please Try again!"<<endl;         trycount++;     }     if (guess==number)     cout<<"Congratulations! You correctly guessed the number!";     else     cout<<"Sorry, the correct number was:"<<number;     return 0; }```
41 (for me) is the number that always wins the game. Despite the fact that I used the Random tag. I can't use RAND_MAX because we're not going between 0 and 32767.

Any ideas, anybody?

P.S. If you find the problem, lay out what my code /should/ look like. I'm a noob at this, just resumed from a very long break from it today.

Thanks,
• 03-05-2012
manasij7479
Use srand (only once, at the beginning) before calling rand, for seeding the generator.
Code:

`srand(time(NULL));`
Here it is seeded with the current time, thus giving a different random series each time.
• 03-05-2012
so...where would I put that in the code? line 1? like before the #include's?
I'm a total noob hahah
• 03-05-2012
laserlight
At the start of the main function.
• 03-08-2012
Quote:

Originally Posted by laserlight
At the start of the main function.

Still a tiny bit confused...line 1? so above all of the #include's?

or before the main()?
• 03-08-2012
manasij7479
Quote:

Still a tiny bit confused...line 1? so above all of the #include's?

or before the main()?

Neither.
Inside main.
i.e
Code:

```int main() {       //Here       //Everything else }```
• 03-08-2012
It gives me an error that 'time' was not declared in this scope.
I think that means that it doesn't recognize "time" or that it's not a variable.
• 03-08-2012
oogabooga
• 03-09-2012
fengwilson
Hello, I am new to the forum.
• 03-09-2012
Elkvis
Quote:

Originally Posted by fengwilson
Hello, I am new to the forum.

so what? do you want a cookie? do you think it's ok to hijack someone else's thread to introduce yourself? for that, you should go to the general discussions forum. what you have just done is a rather egregious violation of proper forum etiquette.
• 03-09-2012
manasij7479
Quote:

Originally Posted by Elkvis
so what? do you want a cookie? do you think it's ok to hijack someone else's thread to introduce yourself? for that, you should go to the general discussions forum. what you have just done is a rather egregious violation of proper forum etiquette.

Haw Wude !!
Attachment 11549
• 03-09-2012
Elysia
Quote:

Originally Posted by manasij7479
Use srand (only once, at the beginning) before calling rand, for seeding the generator.
Code:

`srand(time(NULL));`
Here it is seeded with the current time, thus giving a different random series each time.

mana, you know as well as I do that should be "nullptr".
• 03-09-2012
manasij7479
Quote:

Originally Posted by Elysia
mana, you know as well as I do that should be "nullptr".

I had some doubts about whether it is alright to use with C functions.
As NULL is defined in ctime, and time is documented to work with NULL , I wrote that.
• 03-09-2012
Elysia
Well, you should really be using
#include <ctime>
#include <cstdlib>

and

std::srand(std::time(nullptr));

Anyway, nullptr is implicitly convertible to any pointer type, so yes, it works with C functions.