Help with "srand" for number guessing game

I have to write a program for a number guessing game. (Computer picks a completely random number between 1 and 1000, and the user then has to guess the number within 10 tries.) I am having problems with "srand". I am getting an error C2296: '%' illegal, left operand has type 'void'. Also IntelliSense: expression must have integral or enum type. An one warning, C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data.

Any help to make me understand and fix these error messages is greatly appreciated. Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void displayInstructions();
int playGame();
int cinInRange(int low, int high);

int main()
{
displayInstructions();

playGame();

return 0;
}

void displayInstructions()
{
cout<<"Let's play a number guessing game. I am thinking of a "
<<endl<<"number between 1 and 1000. You get 10 tries to guess my "
<<endl<<"number. Each time you make a guess, I'lll tell you whether "
<<endl<<"my number is higher or lower than your guess."<<endl<<endl;

return;
}

int playGame()
{
char again = 'y';

while(again == 'y')
{
int count = 1, choice1;
while(count < 11)
{
int correctnum;

correctnum = 1 + (srand(time(0)) % 1000);


cout<<"Guess #"<<count<<endl;
choice1 = cinInRange(1,1000);
count = count++;

if(choice1 < correctnum)
{
cout<<choice1<<" is lower than my number."<<endl;
}
if(choice1 > correctnum)
{
cout<<choice1<<" is higher than my number."<<endl;
}
if(choice1 == correctnum)
{
cout<<endl<<"You win!! You guessed my number in just "<<count<<" tries!"<<endl
<<endl<<"Play again (y/n)? ";
cin>> again;
}
}
}
}

int cinInRange(int low, int high)
{
int choice;

cout<<"Enter a number between "<< low << " and "<< high <<": ";
cin>> choice;

while (choice < low || choice > high)
{
cout<<"Invalid entry. Enter a number between "<<low<<" and "<<high<<": ";
cin>> choice;
}
return choice;
}

first, why put the bulk of a program this short within a function, and not within main(). this makes it much harder to read.
second, after i got the code to work you forgot to add one to the value of the counter in order to limit, so each guess is the 1st, you did count++, which adds 1 after the function, if you use ++count than it actually will add +1 each occurrence .
third, there is no need for the time(0) in the (srand(time(0)) % 1000), (rand() % 1000) works just fine.
forth, you defined
1
2
3
4
5
6
while(again == 'y')
{
int count = 1, choice1;
while(count < 11)
{
int correctnum;

defining numbers within a while statement is also extremely hard to read, try defining all your variables at the beginning of every function its not like you cannot go back and insert the code for that and it makes the code SO much more readable, especially if you are sharing it at this website or publicly in general.
- that will fix the general issues although you should work on your while loop nesting because at the moment it will go on infinitely forever unless the user wins the game, and the random number will change EACH time you enter a number, making it EXTREMELY inconsistent.
- i also do not just want to hand the working code to you and have you not learn anything.
Please use code tags [code] /*code here*/ [/code]. It makes it much easier to read your code.

ui uiho wrote:
first, why put the bulk of a program this short within a function, and not within main(). this makes it much harder to read.

I think it's a good thing that he tries to split the code into different functions. I would say it makes it more readable.

ui uiho wrote:
there is no need for the time(0) in the (srand(time(0)) % 1000), (rand() % 1000) works just fine.

Just to clarify. You call srand to seed the random generator. A different seed gives a different sequence of numbers from rand(). Only call srand once at the beginning of your program.

ui uiho wrote:
defining numbers within a while statement is also extremely hard to read, try defining all your variables at the beginning of every function its not like you cannot go back and insert the code for that and it makes the code SO much more readable

I disagree with this. Define the variables as late as possible.

when i was trying to get the program to run i did this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int playGame(){
    // variables used in function
    char again = 'y';
    int count= 1, choice1;
    int correctnum;
    srand(time(NULL)); 
    correctnum = 1 + (rand() % 1000);
    
    while(again == 'y')
    {// continuation of game loop
        while(count < 11)
        {// continuation of guess number loop
            
            cout<<"Guess #"<<count<<endl;
            cin >> choice1;
            ++count;
       

for variables and correctly setting the random number, along with properly working the while statements.
to me the seems alot more readable within a function, but neither of us is wrong, it is just preference when reading code.

I think it's a good thing that he tries to split the code into different functions. I would say it makes it more readable.

for using larger programs i would agree, but the fact that the program is so small, throwing the while code within a single function just makes it even longer and doesn't accomplish anything.
if your program was say several hundred or even several thousand lines of code i would say use functions like this, but as long as you can comment at all ( or name your variables, both are easy and you should do both) this secondary main function is not needed.
Topic archived. No new replies allowed.