//--------------------------------------------------------------------------- //Name: Game - guess a number (introduction to standard libraries) //Author: Przemyslaw Kudlacik //--------------------------------------------------------------------------- #include #include #include int main(int argc, char* argv[]) { //definition of variables int user_value; int random_value; int counter; char name[100]; printf("Please enter your name: "); scanf("%s",name); //print hello printf("Hi, %s. Nice to meet you.\nThis is a game:\nGuess a Number\n",name); printf("\n"); fprintf(stdout,"The rules are simple: guess a number as fast as you can\n\n"); //generating a random number srand(name[0]+name[1]+name[2]+name[3]); //setting a generation seed //srand( time(NULL) ) //- better to set a seed with actual time random_value = rand(); counter = 0; //game loop do { printf("Enter a number: "); scanf("%d",&user_value); if ( user_value > random_value){ printf("The number is too big\n"); } if ( user_value < random_value){ printf("The number is too small\n"); } counter = counter + 1; //increment counter } while ( user_value != random_value ); //print congratulations and good bye printf("\nCongratulations !!!\nYou guessed in %d turns\n\n",counter); printf("Thank you for playing\nHope you enjoyed it ;-)\nBye Bye ..."); fflush(stdin); //clear the std input's buffer getchar(); //wait for a user input return 0; } //---------------------------------------------------------------------------