> while( guess > 0 || !won() );
Maybe && rather than ||
As soon as you win, you want to get out of this loop.
> while( guess > 0 || !won() );
Maybe && rather than ||
As soon as you win, you want to get out of this loop.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I don't see any catch of won's return value. Does the whole function evaluate to a 1 or 0 even though the flag is not caught by a variable?Code:} while( guess > 0 || !won() );
Change the || to &&, in the above line of code.
Edit: Too Slow!!
Adak
So changed it to && and now, when I guess a letter, it runs the won() function no matter what LOL
It's supposed to, isn't it? Otherwise you can't tell if they've won or not.
Code:int won() { int flag = 0; if(strcmp(word_to_guess, word_in_prog)) { printf("\n You've WON! %s is the right answer!", word_in_prog); flag = 1; } return flag; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
from post#4:Originally Posted by nadroj
Yes, ie
Code:if(!strcmp
Last edited by zacs7; 04-10-2007 at 09:00 PM.
Use == 0 rather than ! for extra readability.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Awesome, thank you so much for the help so far guys. The == 0 thing worked well but it's still taking away a guess when I've guess right. I know it's a big favor, but can someone if they have spare time look over it and maybe help me with a few more changes to make it work. I've honestly looked through it and am not really sure what I've done wrong. I would much appriciate the help if someone could do that
You have the same problem here as you had when asking the player if he wants to play again.Code:do { printf("\n Number of tries left : %d", guess); printf("\n %s", letters_guessed); printf("\n Word to guess : "); for(i = 0; i<strlen(word_in_prog); i++) printf("%c ", word_in_prog[i]); printf("\n Please guess a letter : "); scanf("%c", &c); c = tolower(c);
A newline character is left over for each scanf, and consumed by the next iteration of your while loop.
I'd have to see your current code to find out why you're losing a turn on a correct guess. However, one thing that needs pointing out:
Using strlen() as the control condition in a loop is a bad idea 99.9% of the time. strlen() itself has to traverse the string to determine the length so you're really traversing it twice. It doesn't make much difference on small strings like you're using here, but the alternative, better solution is easy to implement.Code:void initialize() { int i; guess = 6; for(i=0; i<26; i++) { letters_guessed[i] = '-'; } for(i=0; i<strlen(word_to_guess); i++) { word_in_prog[i] = '_'; } }
Just replace:
with:Code:for(i=0; i<strlen(word_to_guess); i++)
It will iterate through each array element until the nul-terminator is reached. You use the strlen() technique several times throughout your code, and it's best to get into the habit of doing it the right way sooner than later.Code:for(i=0; word_to_guess[i] != '\0'; i++)
Also, there's a function called memset() that would be handy for use in your initialize() function. You could rewrite the function like this:
Also, if you made word_in_prog a string instead of an array of chars (word_in_prog[strlen(word_to_guess)] = '\0') then you could use the != '\0' trick in your for() loops on it as well.Code:void initialize() { guess = 6; memset(letters_guessed, '-', 26); memset(word_in_prog, '_', strlen(word_to_guess)); }
Last edited by itsme86; 04-11-2007 at 02:33 PM.
If you understand what you're doing, you're not learning anything.
I don't understand really. What's wrong with that? The /n is messing it up or is something else wrong?
K, I've got what itsme is saying and will replace that. Not sure about before with the scanf's
So, I've taken all the advice so far but it's still not running right. Every time I guess a letter, it still minuses from the guesses, even if the letter is in the word. And, it doesn't print out the sentences in the do...while function for some reason. It's supposed to display the # of guesses left and the letters guessed already. Any help with any of this?
Please, if include help, can you include code or an example of code with explination. I'm a little confused with the earlier posts and would really enjoy if I could get this pleaseCode:#include <stdio.h> #include <string.h> #include <ctype.h> #define MAX_SIZE = 20; #define MAX_LETTERS = 26; #define MAX_GUESS = 6; void instruct(); int play_again(); void initialize(); int letter_check(char c); int won(); char word_to_guess[20], word_in_prog[20], letters_guessed[26]; int guess; int main() { int i; char c; FILE *in_file; in_file = fopen("wordlist.txt","r"); do { if( feof(in_file) ) rewind(in_file); fscanf(in_file, "%s", word_to_guess); for(i = 0; i < strlen(word_to_guess); i++) { word_to_guess[i] = tolower(word_to_guess[i]); } initialize(); instruct(); do { printf("\n Number of tries left : %d", guess); printf("\n %s", letters_guessed); printf("\n Word to guess : "); for(i = 0; i<strlen(word_in_prog); i++) printf("%c ", word_in_prog[i]); printf("\n Please guess a letter : "); scanf("%c", &c); c = tolower(c); if( !letter_check(c) ) guess--; } while( guess > 0 || !won() ); if( guess == 0 ) printf("\n You LOSE!"); } while(play_again()); fclose(in_file); } void instruct() { printf("\n Hangman v6.9"); printf("\n To play, guess a letter. You get 6 wrong tries, then your man gets hung. Is that a bad thing?"); } int play_again() { char c; printf("\n Would you like to play again? [y/n]"); scanf("%c", &c); c = tolower(c); if(c == 'y') return 1; else return 0; } void initialize() { guess = 6; memset(letters_guessed, '-', 26); memset(word_in_prog, '_', strlen(word_to_guess)); } int letter_check(char c) { int i, flag = 0; for(i = 0; i<strlen(word_to_guess); i++) { if(c == word_to_guess[i]) { flag = 1; word_in_prog[i] = c; } } return flag; } int won() { int flag = 0; if(strcmp(word_to_guess, word_in_prog)==0) { printf("\n You've WON! %s is the right answer!", word_in_prog); flag = 1; } return flag; }
Should be:Code:define MAX_SIZE = 20; #define MAX_LETTERS = 26; #define MAX_GUESS = 6;Code:define MAX_SIZE 20 #define MAX_LETTERS 26 #define MAX_GUESS 6Should be:Code:void initialize() { guess = 6; memset(letters_guessed, '-', 26); memset(word_in_prog, '_', strlen(word_to_guess)); }You're still using scanf:Code:void initialize() { guess = MAX_GUESS; memset(letters_guessed, '-', MAX_LETTERS); memset(word_in_prog, '_', strlen(word_to_guess)); }What happens when you press a letter? Nothing. It waits for you to press ENTER. You aren't doing anything with the ENTER key.Code:printf("\n Please guess a letter : "); scanf("%c", &c);
Quzah.
Hope is the first step on the road to disappointment.
as suggested afew times, you havent changed your while condition yetCode:} while( guess > 0 || !won() );