#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Need help with C code


    //• Write a program that reads letters from a file called“inputLetter.txt”.
    • Your program will ask the user to enter the number of games they wish to play (1 to 4)
    • Your program will open the inputLetter.txt file read in one character at a time and repeat this for the number of games the user wants to play.
    • For this assignment the test file will contain at least 15 letters, all lowercase
    • When the number of games has been played, the program will end
    • A sample of an input file that you can use to test your program is included with the assignment.
    • A preprocessor directive must be used to define the maximum number of guesses as 6
    • If the player has used up all of their guesses, a message that the game is over should be displayed along with the letter they were trying to guess.
    • You must have at least 4 user defined functions as follows:

    //this function provides instructions to the user on how to play the game
    void Instructions( );

    //this function runs one entire game. It for checks either 6 incorrect guesses or a correct guess.
    //It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
    int PlayGuess(char solution);

    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the PlayGuess( ) function described above
    char GetLetter( );

    //this function takes two arguments, the guess from the player
    //and the solution letter from the file.
    //It lets the user know if the guess comes alphabetically before or after the answer
    //The function returns 1 if the guess matches the solution and returns a 0 if they do not match
    int CompareLetters(char guess, char solution);


    //this is my code, i'm completely lost, i have no idea what else to do.



    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #define MAXGUESSES 6
    
    //function prototypes with a comment for each one describing what the function does.
    //Copy and paste from assignment
    
    int main()
    {
    
    	
        //variable declarations
        int 
        i = 0;
        FILE* infile;
        int numberofgames;
        char ans, gameFunction;
            
        //prompt and get number of games
        
        printf("Welcome to Letter Guess\n");
        printf("You will enter the number of games you want to play (1 - 4 games)\n");
        printf("You have 6 chances to guess each letter\n");
        printf("Let's begin:\n\n");
        
        printf("How many games do you want to play (1-4)");
        scanf("%d",& numberofgames);
        
        
        //getting the file
        
        infile = fopen("inputLetter.txt", "r");
        
        
        	
        for(i=1;i<=numberofgames;i++)
        	{
                fscanf(infile," %c",&ans);
                printf("Let's play game %d\n", i);
                PlayGuess(ans);
     
             }
        
        fclose(infile);
        system("pause");
        return 0;
    }
    
    int PlayGuess(char answer)
    {	
        
        char getguess = 0;
    	int numGuesses = 0, numberofgames = 0;
    	while(numGuesses < MAXGUESSES)
    	{	
                printf("Enter a guess\n");
                scanf("%c", getguess);
                if(answer==getguess)
                {
                 printf("You guessed it!!!\n");  
                                
                }else{
                     if(answer>getguess)
                     {
                        printf("the letter you are trying to guess comes before:%d\n", getguess);
                     }else if(answer<getguess){
                           printf("the letter you are trying to guess comes after:%d\n", getguess);
                     }
            
                }
    numGuesses = numGuesses +1;
    	}
    	
    }
    The out put looks like this


    How many games do you want to play (1-5) 3

    ************************************
    Let's play game 1
    Enter a guess: e

    the letter you are trying to guess comes before e
    Enter a guess: c

    the letter you are trying to guess comes before c
    Enter a guess: a
    You guessed it!!!

    ************************************
    Let's play game 2
    Enter a guess: t

    the letter you are trying to guess comes before t
    Enter a guess: a

    the letter you are trying to guess comes after a
    Enter a guess: p

    the letter you are trying to guess comes before p
    Enter a guess: n

    the letter you are trying to guess comes before n
    Enter a guess: g

    the letter you are trying to guess comes after g
    Enter a guess: k

    the letter you are trying to guess comes after k
    You did not guess the letter. It was m
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Originally Posted by GugaVerdao
    this is my code, i'm completely lost, i have no idea what else to do.
    You could tell us what you expected to happen when you run your program. And what effectively happens. The compilation errors or warnings if that's your problem.

    Also using code tags makes the code easier to study; and gets you faster (and better) answers.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    I rearranged your code a bit and compiled with warning enabled: -Wall
    c.c: In function ‘PlayGuess’:
    c.c: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    This is a rather critical problem since it appears in scanf. scanf changes data at a memory location you provide, which is worse? (oh, remember that lawsuit against Microsoft when excel displayed the wrong magnitude of numbers in cells. I was aware of that problem at the time because I observed it. I knew what the data should be, and knew that the representation didn't match) than merely using bad data.
    Code:
    conversions = scanf("%c",address_of_a_location_big_enough_for_at_least_one_character); /* you need this, then make sure conversions matches your expectation. */
    When I ran your program it core dumped before it got there. That's because I didn't have an input file named as you've named it. You also must check that

    infile = fopen("inputLetter.txt", "r");

    succeeds.

    if (NULL == infile) open_failure_action();
    Last edited by b49P23TIvg; September 26th, 2012 at 08:53 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    Thanks! Can you clarify, I'm new to C
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    The output you presented must have been the desired output rather than actual from running your program.

    warnings are critical! turn on your compiler warnings -Wall to see them, then fix them.

    Code:
    /* see my comments marked with XXXXXXXXXXXXXXXX */
    
    
    #define _CRT_SECURE_NO_DEPRECATE
    #include<stdio.h>
    #include<stdlib.h> /* declare system for the compiler XXXXXXXXXXXXXXXX */
    #define MAXGUESSES 6
    
    void PlayGuess(char); /* declare play guess for the compiler XXXXXXXXXXXXXXXX*/
    
    int main() {
      //variable declarations
      int
        i = 0;
      FILE* infile;
      int numberofgames;
      char ans; /* gameFunction;  eliminate unused variable, XXXXXXXXXXXXXXXX */
    
      /* fail as soon as possible XXXXXXXXXXXXXXXX */
      /* don't make the user enter something if the program can't succeed. XXXXXXXXXXXXXXXX */
      /* (on that theme, we could read 4 characters here as well.)XXXXXXXXXXXXXXXX */
      infile = fopen("inputLetter.txt", "r");
    
      if (NULL == infile) {		/* verify your file of letters XXXXXXXXXXXXXXXX*/
        fprintf(stderr,"\nQuitting!  Failed to open %s\n","inputLetter.txt");
        return -1;
      }
    
      //prompt and get number of games
    
      puts("Welcome to Letter Guess"); /* puts is more efficient XXXXXXXXXXXXXXXX */
      puts("You will enter the number of games you want to play (1 - 4 games)");
      puts("Lambert Electronics, LLC.  USA, NY.");
      puts("You have 6 chances to guess each letter");
      puts("Let's begin:\n");
    
      printf("How many games do you want to play (1-4)"); /* XXXXXXXXXXXXXXXX */
      if (1 != scanf("%d",& numberofgames))	/* verify that scanf worked */
        numberofgames = 0;
      else if (numberofgames < 1) /* validate numberofgames.  Not important, don't belabor this, clip to interval [1,4] */
        numberofgames = 1;
      else if (4 < numberofgames)
        numberofgames = 4;
    
      for(i=1;i<=numberofgames;i++) { /* for loop clearer than while with scattered initializer and increment XXXXXXXXXXXXXXXX */
        fscanf(infile," %c",&ans);
        printf("Let's play game %d\n", i);
        PlayGuess(ans);
      }
    
      fclose(infile);
      system("pause");
      return 0;
    }
    
    void /* int unused, unsupplied return type. XXXXXXXXXXXXXXXX */ PlayGuess(char answer) {
    
      char getguess = 0;
      int numGuesses /*, numberofgames = 0 eliminate unused variable XXXXXXXXXXXXXXXX */;
      for (numGuesses = 0; numGuesses < MAXGUESSES; ++numGuesses) {
        puts("Enter a guess");  /* puts more efficient XXXXXXXXXXXXXXXX */
        scanf(" %c", &getguess);  /* crazy!!!  you need the address of the character XXXXXXXXXXXXXXXX */
        /* inserted a space character before % in previous scanf XXXXXXXXXXXXXXXX */
        if(answer>getguess)	      /* AFTER  XXXXXXXXXXXXXXXX */
          printf("the letter you are trying to guess comes after:%c\n", getguess); /* I admit the integer is more useful but I doubt you intended it  changed %d to %c XXXXXXXXXXXXXXXX */
        else if(answer<getguess)	/* BEFORE XXXXXXXXXXXXXXXX */
          printf("the letter you are trying to guess comes before:%c\n", getguess);	/* The integer IS more useful.  I doubt you intended it.  Changed %d to %c XXXXXXXXXXXXXXXX */
        else	/* rearrangement eliminates a test XXXXXXXXXXXXXXXX */
          puts("You guessed it!!!");
      }
      /* ++numGuesses; useless, removed XXXXXXXXXXXXXXXX */
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by GugaVerdao
    Thanks! Can you clarify, I'm new to C
    Stating it another way:
    Warnings are very important. They are even more important than error messages. Never ignore warnings! And if you have your compiler set up to not display warnings, then you need to tell it to display them, all of them. Or as we will often say here: "Turn warnings on, and up!"

    Exactly how you turn warnings on differs from one compiler/IDE to another. b49P23TIvg was offering -Wall because that's how we do it with a rather common compiler, gcc. However that macro, _CRT_SECURE_NO_DEPRECATE, tells me that you're using Microsoft's Visual Studio, possibly 2008 or 2010.

    In Visual Studio 2008, the Warning Level is the setting you'd want to change. You would find that in the project's property pages (accessible via the Main Menu under Project), then on the property page: Configuration Properties, C/C++, General. If warnings are turned off, then set them to at the very least Level 3.

    However, if warnings are indeed turned on (I've never changed that setting, so it apparently defaults to Level 3), then the problem is that you have been ignoring them. You must not ignore warnings!

IMN logo majestic logo threadwatch logo seochat tools logo