I am making a hangman program. It displays the word "PROGRAMMING" as all *'s and replaces a * with the correct letter if that letter is guessed. I am trying to set it so that it only allows 7 errors before the player loses.
My program keeps printing the output twice after each guess, and since I have added the "else" statement it automatically prints "WRONG" 7 times and ends the program instead of just adding one the int wrong.
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char word[]= "PROGRAMMING"; //The correct word
char guessed[] = "***********"; //blanks for the word
char letter; //letter to be guessed
int wrong; //counter for how many attempts have been made
int main(void)
{
int wordlength = strlen(word);
int i = 0; //use i as a counter in for loops
printf("Welcome to Hangman.\n\n\n"); //title
while (guessed != word && wrong < 7) //loop until the whole word is guessed or too many attempts are made
{
printf("\n"); //for spacing
printf("Word: %s\n", &guessed); //show word guessed so far
printf("Guess a letter: "); //ask for a letter
scanf("%c", &letter); //scan for the letter
letter = toupper(letter); //set letter to uppercase
for (i = 0; i < wordlength; i++) //loop through the word and see if letter guessed is within it
{
if (letter == word[i])
{
guessed[i] = letter;// if it is, replace the * with the letter
}
else
{
printf("WRONG!");
wrong += 1; //add one more attempt
}
}
}
if (guessed == word) //If word was guessed, congrats to the winner
printf("Congratulations! You WIN!!!!!\n");
else
printf("Too many incorrect guesses. You are dead.\n"); //too many guesses
return 0;
}
for (i = 0; i < wordlength; i++) //loop through the word and see if letter guessed is within it
{
if (letter == word[i])
{
guessed[i] = letter;// if it is, replace the * with the letter
}
else
{
printf("WRONG!");
wrong += 1; //add one more attempt
}
}
It searches word one char at a time, and for each char that is incorrect in increments wrong. For example, if you guess the
letter 'P' you would end up with 10 wrong guesses.
Ok, I changed the code so it just counts all attempts made and counters at the end of the while loop.
But, it still doesn't exit the loop when the word has been guessed correctly and it still prints the output twice after each letter is entered...
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char word[]= "PROGRAMMING"; //The correct word
char guessed[] = "***********"; //blanks for the word
char letter; //letter to be guessed
int attempt; //counter for how many attempts have been made
int main(void)
{
int wordlength = strlen(word);
int i = 0; //use i as a counter in for loops
printf("Welcome to Hangman.\n\n\n"); //title
while (guessed != word && attempt < 20) //loop until the whole word is guessed or too many attempts are made
{
printf("\n"); //for spacing
printf("Word: %s\n\n", &guessed); //show word guessed so far
printf("Guess a letter: "); //ask for a letter
scanf("%c", &letter); //scan for the letter
letter = toupper(letter); //set letter to uppercase
for (i = 0; i < wordlength; i++) //loop through the word and see if letter guessed is within it
{
if (letter == word[i])
{
guessed[i] = letter;// if it is, replace the * with the letter
}
}
attempt += 1;
}
if (guessed == word) //If word was guessed, congrats to the winner
printf("Congratulations! You WIN!!!!!\n");
else
printf("Too many incorrect guesses. You are dead.\n"); //too many guesses
return 0;
}
You can't compare two c strings by simply using != and == (see lines 20 and 39). Someone else would have to verify this, but I believe that compares their pointers. You can, however, use strcmp. Strcmp is a little different from normal functions in that it returns zero if the two strings are equal, and a value other than zero if they are not. http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char word[]= "PROGRAMMING"; //The correct word
char guessed[] = "***********"; //blanks for the word
char letter; //letter to be guessed
int attempt; //counter for how many attempts have been made
int main(void)
{
int wordlength = strlen(word);
int i = 0; //use i as a counter in for loops
printf("Welcome to Hangman.\n\n\n"); //title
while ((strcmp (word, guessed) != 0) && attempt < 20) //loop until the whole word is guessed or too many attempts are made
{
printf("\n"); //for spacing
printf("Word: %s\n\n", &guessed); //show word guessed so far
printf("Guess a letter: "); //ask for a letter
scanf("%c", &letter); //scan for the letter
letter = toupper(letter); //set letter to uppercase
for (i = 0; i < wordlength; i++) //loop through the word and see if letter guessed is within it
{
if (letter == word[i])
{
guessed[i] = letter;// if it is, replace the * with the letter
}
}
attempt += 1;
}
if (strcmp(word, guessed) == 0) //If word was guessed, congrats to the winner
printf("Congratulations! You WIN!!!!!\n");
else
printf("Too many incorrect guesses. You are dead.\n"); //too many guesses
return 0;
}
I am still having a problem with it printing lines 22-24 twice instead of once after each letter is input.
This is a really common problem when using scanf. After you perform scanf, there's still an extra '\n' character in the buffer. This causes scanf to pull out that character during the next iteration, meaning that the loop runs twice for every user input. There are a couple ways to fix it, but I think the easiest is to follow scanf with getchar();
Ah, I see. I am in a regular C class right now, though, so I have to stick with the basics of C.
Are there any other ways to avoid the double print?
I guess that means it is incrementing the attempt counter twice each time too...
A simple method to debug looping and recursion problems is to insert something like printf("\n*\n"); at certain points in your code. This way you can easily see how your code progresses while the program is running, and you can identify the point(s) where its behavior is not what you intended.
I am using Visual Studio, and I set a break point just before the while loop and went through it step by step...it runs through the loop once, increments attempt, then goes to the beginning of the while loop again, but doesn't wait for a letter to be entered in scanf, and continues through the loop a second time, incrementing attempt again.
So, it runs twice for each letter entered, not waiting for the user to enter a letter the second time.