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

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Help scanf is not working right


    ok my program is suppose to get user input from a person to guess the correct letter from a text file but when they enter the letter the program reads the one before the letter is p by the way

    i cant figure it out like the first time the user inputs a letter it displays nothing but the next time another letter is entered it calculates the previous letter and determines if it is correct

    here is my code

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #define MAXGUESSES 7
     
     
    
     void Instructions();
     char  GetLetter();
     int PlayGuess(char solution);
     int CompareLetters(char guess, char solution);
    int main()
    {
    
    
    int i;
    int solution;
    
    int gamesToPlay;
    
    FILE *pFile;
    pFile=fopen("text.txt","r");
    
    Instructions();
    scanf("%d", &gamesToPlay);
    printf("Ok lets begin!\n");
    printf("Guess the letter!\n\n\n\n\n");
    
    	for(i=1;i<=gamesToPlay;i++)
    	{
    	int WinOrLose;
    	printf("This is game %d\n",i);
    	fscanf(pFile,"%c",&solution);	//get a letter from file
    	WinOrLose = PlayGuess(solution);
    	}
    	
    	fclose(pFile);
    	
    	return 0;
    
    }
    
    void Instructions(){
    	printf("Guess the Letter\n");
    	printf("You have 6 chances to guess the correct letter.\n");
    	printf("Enter the number of games to play.\n");
    }
    
    
    char  GetLetter(){
    	char guess;
    	scanf("%c\n",&guess);
    	return  guess;
    }
    
    int PlayGuess(char solution){
    
    	int numGuesses = 0;
    	while(numGuesses < MAXGUESSES)
    	{	
    		int WinOrLose;
    		char guess = GetLetter();
    		printf("The letter you chose is %c\n",guess);
    		WinOrLose = CompareLetters( guess, solution);						
    		numGuesses = numGuesses +1;
    		if (WinOrLose==1){
    			break;
    			return WinOrLose;
    		}
    	}
    }
    
    int CompareLetters(char guess, char solution){
    	int WinOrLose;
    		if(guess==solution){
    	printf("You got it right\n\n");
    	return 1;
    	}
    	else if (guess<solution){
    	printf("The letter comes after %c Please try again\n\n", guess);
    	return  0;
    	}
    	else if (guess>solution){
    	printf("The letter comes before %c Please try again\n\n",guess);
    	return 0;
    	}
    }
    this is how it looks
    http://imageshack.us/a/img442/9131/21593640.jpg
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0
    also i figured out how to exit of while loop once the user puts p they have 7 attempts per game but i dont know how to exit the for loop to finish the program ive been working on it all day

    for example once they put p it goes on to the next game until all the games the player wants to play finishes how do i exit this for loop once they get it right.

    I tried doing the same thing for the foor loop with the break statement

    apparently the winorlose value that is returned in the for loop is somehow ends up being 2 so
    i did it like this
    Code:
    for(i=1;i<=gamesToPlay;i++)
    {
    	int WinOrLose;
    	printf("This is game %d\n",i);
    	fscanf(pFile,"%c",&solution);	//get a letter from file
    	WinOrLose = PlayGuess(solution);
            if(WinOrLose==2){
            break;
            }
    }
    but when i run only 1 game is played everytime
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    You can't imagine how PlayGuess can return anything but 0 or 1. Turn on your compiler warnings, fix the problems. Your c compiler flag might be -Wall meaning "turn on all warnings". There's one person who responds to questions in these forums who claims that warnings are more serious than errors. How can that be so? With an outright error, you won't get an executable program. However, with warnings you'll get an executable BUT YOU WON'T KNOW WHAT IT DOES! Press the launch button and climb under a desk.

    In this case, PlayGuess does not choose the value it returns. You've got a break; statement immediately before return WinOrLose; . That return statement cannot happen in a correctly functioning system. And what happens after the seventh guess? Have you got a return statement? NO!

    Code:
    int PlayGuess(char solution){
      int numGuesses = 0;
      while(numGuesses < MAXGUESSES) {	
        int WinOrLose;
        char guess = GetLetter();
        printf("The letter you chose is %c\n",guess);
        WinOrLose = CompareLetters( guess, solution);						
        numGuesses = numGuesses +1;
        if (WinOrLose==1){
          break; /////////////////swap these two statements
          return WinOrLose;//////////////////////////////////
        }
      }
      //////////////return 0;////////////here
    }
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo