daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //header with your name, etc
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <string>
  6.  
  7. #define MAX_GUESSES 6
  8.  
  9. //function prototypes
  10. void Instructions();
  11. char GetGuess();
  12. int PlayGame(char answer);
  13. int CompareGuess(char guess, char answer);
  14.  
  15.  
  16. int main()
  17. {
  18.    
  19.     //variable declarations
  20.     int numGames=0;
  21.     char answer;
  22.  
  23.     //file pointer declaration
  24.     FILE* infile;
  25.    
  26.     //connect to the file
  27.     infile = fopen("letters.txt", "r");
  28.    
  29.     if (infile==NULL)
  30.     {
  31.         printf("File Not Opened\n");
  32.     }
  33.  
  34.     Instructions();
  35.    
  36.    
  37.     //get number of games the user wants to play
  38.     printf("How many games would you like to play\n");
  39.     scanf("%d", &numGames);
  40.     printf("\n%d games\n",numGames);
  41.  
  42.  
  43.     for(int i=1;i<=numGames;i++)
  44.     {
  45.         //print current game (value of i)
  46.         printf("%d currentGame\n",i);
  47.         //get letter to guess from file
  48.         fscanf(infile," %c", &answer);
  49.  
  50.         //call the play function to play a game
  51.         PlayGame(answer);
  52.         //it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
  53.        
  54.         //condition here to print win or lose
  55.        
  56.     }
  57.     fclose(infile);
  58.     return 0;
  59. }
  60.  
  61.  
  62. //function prototypes
  63. //do not forget to add comments above the function definitions (see examples)
  64.  
  65.  
  66. //print the instructions on how to play the game
  67. void Instructions()
  68. {
  69.     printf("Welcome to Letter Guess\nYou have 6 chances to guess each letter\nLets Begin:\n***********************\n");
  70.  
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. int PlayGame(char answer)
  79. {
  80.     int numGuesses = 0;
  81.     int winorlose = 0;
  82.     char userguess;
  83.  
  84.     while(numGuesses < MAX_GUESSES && winorlose == 0)
  85.     {
  86.         //call get guess
  87.         userguess = GetGuess();
  88.    
  89.         //call compare function
  90.         CompareGuess(userguess, answer);
  91.        
  92.         //update numGuesses
  93.         numGuesses++;
  94.         return 0;
  95.  
  96.     }
  97.     return 0;
  98.  
  99. }
  100.  
  101.  
  102. char GetGuess()
  103. {
  104.     char userguess;
  105.  
  106.     printf("Enter a guess: \n");
  107.     scanf("%c", &userguess);
  108.  
  109.     //get the users' guess
  110.     userguess = tolower(userguess);//change to lower case
  111.     return userguess;
  112. }
  113.  
  114. int CompareGuess(char guess, char answer)
  115. {
  116.     //compare the guess and the answer
  117.     if(guess == answer)
  118.     {
  119.         printf("You got the letter right!\n");
  120.         return 1;
  121.     }
  122.     else (guess != answer);
  123.     {
  124.         if( guess < answer)
  125.         {
  126.             printf("the letter you are trying to after %s\n",guess);
  127.         }
  128.         else if (guess > answer)
  129.         {
  130.             printf("the letter you are trying to before %s",guess);
  131.         }
  132.  
  133.         return 0;
  134.     }
  135. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top