The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Help scanf is not working right
Discuss Help scanf is not working right in the C Programming forum on Dev Shed. Help scanf is not working right C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 25th, 2012, 06:53 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 9
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
|

September 25th, 2012, 07:02 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 9
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
|

September 25th, 2012, 09:47 PM
|
 |
Contributing User
|
|
|
|
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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|