Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
# include<stdio.h>
# include<stdlib.h>
# include<time.h>
int main(){
    int number, guess, nguesses=1;
    srand(time(0));
    number = rand()%100 = 1;
    printf("The number is %d\n", number);

    do{
        printf("Guess the number between 1 to 100\n");
        scanf("%d", guess);
        if(guess<number){
            printf("Higher number please!\n");
        }
        else if(guess>number){
            printf("Lower number please!\n");
        }
        else{
            printf("You guessed it in %d attempts\n", nguesses);
        }
        nguesses++;
    }while(guess!=number);
     return 0;
}


What I have tried:

I can't think of anything....I tried to find an error but was unable to do so, but VS code is giving an error in rand() function
Posted
Updated 30-Oct-21 5:57am
v4
Comments
Richard MacCutchan 30-Oct-21 12:13pm    
You have updated your question, but have still not told us what the error is.
Kartik D 30-Oct-21 12:17pm    
What are you saying...see the accepted solution he corrected me and I thanked him.
Richard MacCutchan 30-Oct-21 12:27pm    
Then why did you edit your question without mentioning that the problem is solved?
Kartik D 30-Oct-21 12:44pm    
Isn't it obvious if I have confirmed the answer than it is solved.

Look at what you are trying to do.

You are trying to set an expression to 1 which makes no sense.

rand() % 100 = 1

Try

number = rand() % 100;
 
Share this answer
 
Comments
Kartik D 23-Oct-21 11:43am    
@Tony Hills thanks
The scanf function requires the address of its variable:
C++
scanf("%d", &guess);
            ^ the addressof operator must be used here.
 
Share this answer
 
I assume that the random number is the same value everytime you run the program.

You are not seeding the random number generator properly, I tried this and it seems OK.

# include<stdio.h>
# include<stdlib.h>
# include<time.h>
int main() {
	int number;
	int guess = 0;
	int nguesses = 1;
	time_t t;
	srand((unsigned) time(&t));

	number = rand() % 100;
	printf("The number is %d\n", number);

	do {
		printf("Guess the number between 1 to 100\n");

		scanf("%d", guess);
		if (guess < number) {
			printf("Higher number please!\n");
		}
		else if (guess > number) {
			printf("Lower number please!\n");
		}
		else {
			printf("You guessed it in %d attempts\n", nguesses);
		}
		nguesses++;
	} while (guess != number);
	return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900