News:

Welcome to RetroCoders Community

Main Menu

guess computer number game in C

Started by ron77, Jun 03, 2023, 12:46 PM

Previous topic - Next topic

ron77

Hi everyone... here is a simple game - the program randomly generates a number between 0 to 100 and you got 10 turns to try and guess it... used to play this game with my mom as a small kid while waiting in line at the child doctor's office... sure was helpful to pass the time...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <windows.h>
#include <stdbool.h>




int main() {
	
	bool isOver = false;
	int input = 0;
	srand ( (unsigned int)time(NULL) );
	int number = rand()%101;
	int turns = 1;
	
	for (int i = 0; i < 10; i++) {
	system("cls");
	printf("i'm thinking about a number beween 0 to 100\n\nyou have 10 turns to try and guess it!\n\ncan you guess my number???\n\n");
	printf("\n\nturn number %i\n\nwhat is you guess?\n\n", turns);
	scanf("%i", &input);
	
	if (input < 0 || input > 100) {
		printf("\n\nmy number is between 0 to 100 invalid guess! you wasted a turn!");
	}else if (input > number) {
		printf("\n\nyour number is too high!! guess lower!!");
	}else if (input < number) {
		printf("\n\nyour number is too low!! guess higher!!");
	}else if (input == number) {
		isOver = true;
		printf("\n\nCORRECT YOU GUESSED MY NUMBER! IT'S %i WELL DONE!!", number);
	}
	
	turns++;
	
	if (isOver == true) {
		break;
	}
	
	Sleep(5000);
	
	}
	printf("\n\nGAME OVER!!!");
	
	
	return 0;
}