C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 25th, 2012, 06:53 PM
program57 program57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 9 program57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 42 m 16 sec
Reputation 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

Reply With Quote
  #2  
Old September 25th, 2012, 07:02 PM
program57 program57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 9 program57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 42 m 16 sec
Reputation 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

Reply With Quote
  #3  
Old September 25th, 2012, 09:47 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help scanf is not working right

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap