Expand|Select|Wrap|Line Numbers
- //Assignment 2 Play
- //In this guessing game the user will be asked to guess a number from between 1-100
- //The user has ten tries if they donot guess the right number its game over
- //The user also gets to choose how many times they want to play between 1-10
- #define _CRT_SECURE_NO_DEPRECATE
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- int
- main(void)
- {
- FILE *inptr;
- char firsti; //Input users first initial
- char secondi; //Input users second initial
- int games; //Input, number of games user wants to play
- int valid=0; //For the while loop, how many games the user wants to play
- int tries; //number of tries the user has to get the right number which is 10
- int guess1=0; //Input the users guess at the number
- int guess2;
- int number; //The ramdom number the computer picks so the user can guess
- int i=0;
- void Instructions();
- printf("Welcome to Number Guess\n");
- printf("To begin, you will need to enter your initials\n");
- printf("You will then enter the number of games you want to play\n");
- printf("You have 10 chances to guess the number\n");
- printf("Let’s begin:\n\n");
- //Asks the user to input his/her initials
- printf("Enter your two initials: ");
- scanf("%c%c", &firsti, &secondi);
- //opening file
- inptr=fopen("answers.txt","r");
- //Loop for the number of times the user wants to play
- while(valid==0)
- {
- printf("\nHow many games would you like to play %c%c?(1-10)\n",firsti, secondi);
- scanf("%d", &games);
- if(games<1)
- {
- printf("Thats less than 1\n");
- valid=0;
- }
- else if(games>10)
- {
- printf("That's too many!\n");
- valid=0;
- }
- else
- valid=1;
- }
- //User has to guess the number
- //Prompts the user the game number they are playing
- printf("Lets play game %c%c\n", firsti, secondi);
- printf("You have 10 guesses to get the correct answer\n");
- srand(time(NULL));
- number = 1 + rand() % 100;
- //Loop for game
- for(tries=1; tries<11&&guess1!=number;tries++)
- {
- //Asks user to input their guess
- printf("please enter a number to guess between 0 and 100: ");
- scanf("%d", &guess1);
- if(guess1>number)
- printf("Incorrect, you are to high\n");
- if(guess1<number)
- printf("Incorrect, you are to low\n");
- if(guess1==number);
- printf("Good job! You guessed it!\n");
- system("PAUSE");
- printf("Game over\n");
- return(0);
- }