So some information, I am making a hangman game, that get's a random word from an array, which then that random word is masked and printed as '_' to show the user how many letters the random word has.
A typical hangman is you have a number of tries and once you reach a certain threshold you lose. This is shown in the code by a print that prints the hangman slowly building with each increase in counter.
Now the issue lies in I have no idea how to connect these concepts. How scanf code will interact with the random word, and counter. Especially since the masked word is in int, I tried using the char m, for loop;
int counter = 0; answer != m; counter
but nothing happens instead it just outputs the print for the "You win!" statement despite only putting in one letter.
I even tried;
int counter = 0; answer != 'q'; counter
So I can see if when I input a letter not q if it will start building the hang man, but even then it still prints out "You win!"
Also tried;
for (int counter = 0; counter >= 7; counter )
So it has to continue counting until greater or equal to 7, yet still nothing.
Any tips?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
#include <string>
#define ARRAY_SIZE 10
int main()
{
//randomwordgenerator
char word[ARRAY_SIZE][200] = {
"tiger", "lion", "elephant", "zebra", "horse",
"camel", "deer", "crocodile", "rabbit", "cat"
};
int x = 0;
srand(time(0));
x = rand() % ARRAY_SIZE;
system("pause"); //will pause the rand function
// masking and unmasking word
char m = strlen(word[x]); //will count the number of letters of the random word
// int mask[200]{};
int mask[200]{};
for (int i = 0; i < m; i) { //loop until all leters are masked
mask[i] = '_';
printf("%c ", mask[i]);
}
//Introduction
printf("\nHey! Can you please save me? \n");
printf(" O\n/|\\ \n/ \\ \n");
//Ask for answer
printf(
"\n"
"Type a letter to guess the word and save me. Here are some tips!\n"
"1) The '_' above are how many letters make up the word, isn't that neat!\n"
"2) The letters are case sensitive so please pick lower case or I might die\n"
"3) Have fun! \nNow with that out of the way please type in your guess; "
);
char answer;
scanf_s("%c", &answer);
//loop w counter
for (int counter = 0; answer != 'q'; counter ) {
if (counter == 1) {
printf("\n=========");
}
else if (counter == 2) {
printf("\n \n|\n|\n|\n|\n|\n=========");
}
else if (counter == 3) {
printf("\n --- \n| |\n|\n|\n|\n|\n=========");
}
else if (counter == 4) {
printf("\n --- \n| |\n| O\n|\n|\n|\n=========");
}
else if (counter == 5) {
printf("\n --- \n| |\n| O\n| |\n|\n|\n=========");
}
else if (counter == 6) {
printf("\n --- \n| |\n| O\n| |\n| / \\ \n|\n=========");
}
else if (counter == 7) {
printf("\n --- \n| |\n| O\n| /| \n| / \\ \n|\n=========");
}
else if (counter == 8) {
printf("\n --- \n| |\n| O\n| /|\\ \n| / \\ \n|\n=========");
}
else if (counter == 9) {
printf("\nReally left me hanging there buddy");
return 0;
}
else {
printf("\nThanks for saving me!");
}
return 0;
}
}
CodePudding user response:
I can see several problems here:
Your for
loop initializes and updates counter
but tests against answer
. This could be simplified by simply looping while counter is < 10.
This for
loop also initializes counter to 0 but this falls in the else
of your loop body, printing the win message.
You have a return 0 at the end of your loop, making it exit the program directly after 1 iteration.
Next, you are not updating your answer
inside the loop (but I am assuming you had problems before so you did not go that far yet). You will need to call scanf inside to update the answer. There's a lot to be improved upon but I think this can help you with your current problems. I tried to not change your code much.
for (int counter = 1; counter < 10; counter ) {
scanf_s("%c", &answer);
// you can handle the answer logic here
if (answer ...)
{
}
if (counter == 1)
{
printf("\n=========");
}
else if (counter == 2)
{
printf("\n \n|\n|\n|\n|\n|\n=========");
}
else if (counter == 3)
{
printf("\n --- \n| |\n|\n|\n|\n|\n=========");
}
else if (counter == 4)
{
printf("\n --- \n| |\n| O\n|\n|\n|\n=========");
}
else if (counter == 5)
{
printf("\n --- \n| |\n| O\n| |\n|\n|\n=========");
}
else if (counter == 6)
{
printf("\n --- \n| |\n| O\n| |\n| / \\ \n|\n=========");
}
else if (counter == 7)
{
printf("\n --- \n| |\n| O\n| /| \n| / \\ \n|\n=========");
}
else if (counter == 8)
{
printf("\n --- \n| |\n| O\n| /|\\ \n| / \\ \n|\n=========");
}
else if (counter == 9)
{
printf("\nReally left me hanging there buddy");
break;
}
else
{
printf("\nThanks for saving me!");
break;
}
}
return 0;