Why is this loop not working?
For instance if the mysteryWord=pig , and I guess "i", the program says it couldn't find that character in the word and subtracts 1 from the number of guesses I have left. But the next guess acts as if I have guessed "i". Here is the code. - #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <time.h>
-
#include <ctype.h>
-
-
#define WORD_COUNT 8
-
#define MAX_LENGTH 10
-
-
typedef char string[MAX_LENGTH];
-
-
void main(void)
-
{
-
-
string words[WORD_COUNT] = { "lion","tiger","dog","cat","pig","mouse","fish","bird" };
-
-
char answer[MAX_LENGTH];
-
char guess;
-
int count=-5, index, i, found, choice=-7;
-
char mysteryWord[MAX_LENGTH];
-
-
printf("Welcome to Hangman!\n");
-
-
printf("\n\nChoose an option\n"
-
"1) Easy\n"
-
"2) Moderate\n"
-
"3) Hard\n"
-
-
"Your choice: ");
-
-
scanf("%i", &choice);
-
switch(choice)
-
{case 1:
-
count=5;
-
break;
-
case 2:
-
count=3;
-
break;
-
case 3:
-
count=1;
-
-
}
-
-
srand(time(NULL));
-
index = rand() % WORD_COUNT;
-
-
strcpy(mysteryWord, words[index]);
-
for(i=0;i<strlen(mysteryWord);i=i+1)
-
-
{answer[i] = '-';}
-
-
answer[i]='\0';
-
-
printf("%s \n",answer);
-
-
while(1>0)
-
{
-
printf("\n %i guess(es) left\n",count);
-
printf("Guess a letter:");
-
scanf("%c\n",&guess);
-
guess = tolower(guess);
-
/* found is a "flag" variable. When found = 0 (false) we haven't found a match, */
-
/* and later, if we find a match, change found = 1; (true) */
-
found = 0;
-
-
for(i=0;i<strlen(mysteryWord);++i) /*or i++ */
-
{
-
if (mysteryWord[i]==guess)
-
{answer[i]=guess;
-
found = 1;
-
}
-
}
-
if(found==0)
-
{
-
printf("Not found!\n");
-
--count;
-
} /* or --count or count=count-1 */
-
-
if(count==0)
-
{
-
printf("Sorry, game over.\n");
-
printf("The answer is %s.",mysteryWord);
-
break; /* or return;? */
-
}
-
-
else
-
{if(strcmp(answer,mysteryWord)==0) /* try instead if(answer==mysteryWord) */
-
{printf("Yes, it's a %s\n"
-
"Congratulations!\n",answer);
-
break; /* or return */
-
}
-
else
-
printf("%s",answer);
-
}
-
} /* end of while loop */
-
-
} /* end of main */
3 1398 Banfa 9,065
Expert Mod 8TB
The problem is in line 60 scanf("%c\n",&guess); . Because %c deals in characters it can and does receive the end of line characters that are put into the input stream every time you press return. Although you attempt to deal with this with the \n there is a newline left in the stream from line 31.
Your code might work if there was a way to flush the input stream after line 31, but unfortunately the is no well defined way to do this. Adding a\n to line 31 looks like it might work but it doesn't. The best way I found to deal with this is to change line 60 to read a string from the user rather than a character, reading a string has the same advantage as reading an integer, all white space is used as separators and ignored, including newlines. Another solution you could try is to change line 31 to read a character.
I tried your advice on line 60. Didn't really seem to work.
Line 31 is working perfectly fine already. I definitely don't know more than you but I think something is wrong with the loops.
Here's an example. The problem lies in how the letter guessed gets processed one loop after the user inputs the letter.
Welcome to Hangman!
Choose an option
1) Easy
2) Moderate
3) Hard
Your choice: 1
---
5 guess(es) left
Guess a letter:p
Not found!
---
4 guess(es) left
Guess a letter:c
Not found!
---
3 guess(es) left
Guess a letter:d
Not found!
---
2 guess(es) left
Guess a letter:o
d--
2 guess(es) left
Guess a letter:g
do-
2 guess(es) left
Guess a letter:g
Yes, it's a dog
Congratulations!
Line 12: main must return int, not void.
Line 60: suggest adding a test to print an error message if isalpha(guess) returns false. If you get any of these error messages then go back and take another look at Banfa's post.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andreas |
last post by:
Hi!
I'm not very familiar to C, but I do know a little.
No I'm trying to read someone elses program, and this line (and
simular, less complex lines) realy confuses me. Can someone explain?
I'm...
|
by: jessebasketball |
last post by:
i am a very beginner programmer and am stuck on a very simple problem.
I am trying to make a game where the user
is presented with a scenerio and options. ie
you are standing in the parking lot...
|
by: Bl00dFox |
last post by:
Hi everyone!
I am trying to make a c++ program that calculates interest rates. So far, i have written this code, but my complier (dev c++) keeps error-ing. Can anyone please help?
...
|
by: footloose61 |
last post by:
I am extremely new to python and I need to write a program that simply outputs all the filenames within a specific directory to a text file. Thanks!
|
by: Jason |
last post by:
I would like to make a simple PING program that has the following
paremeters:
txtAddress - a text-box that you enter an IP address into
txtinterval - a text box that allows you to enter in an...
|
by: NoviceJava |
last post by:
I'm new to JAVA and need some help writing a simple calculator program.
Here are the instructions:
-expects espression with 2 operands together with either +, -, *, or / operator
-espects only...
|
by: CDMAPoster |
last post by:
I wrote a very simple demo program in A97 for concatenating email
lists. The undo and redo buttons only undo or redo a set of single
clicks, not a group operation. I might add a function to save...
|
by: Supermarklcable |
last post by:
How Can I Make a Simple Inventory Program (GUI) on Java?
pls , help me i don't know where to start ?
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |