0

I'm using a code segment out of a book that I'm learning from. For some reason it isn't working and I haven't been able to figure out how it works.

When I use the srand function it says I can't convert my (short) Number variable to a (void) srand (time(NULL)) there is an error.

Errors:
- Error C2440: '=' : cannot convert from 'void' to 'short'
Expressions of type void cannot be converted to other types
-Warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

main ()
{
	short Number, Guess = 0;

             Number = srand (time(NULL));
             Number++;

	while (Guess != Number)
	{
		cout <<"Enter a number between 1 and 100: ";
		cin>> Guess;
	

	if (Guess < Number)
	  {
		cout <<"You are guessing to low." <<endl;
	  }

	if (Guess > Number)
	  {
		cout <<"You are guessing to high." <<endl;
	  }
    }


cout <<"You got it bud, the winning number is: " <<Number <<endl;

return 0;
}
2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by frrossk
0

srand() only establishes the seed for the rand() function.
Try

srand (time (0));
Number = rand ();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.