I have to create a program that creates a guess the letter game.
Everything is built successfully. However, I run into a problem because the GetTheLetter function and the CompareThe Letters function are combining and printing at the same time. When it prompts the user for a guess, it is also printing out the first if statement of the CompareTheLetters function. What am I doing wrong?
#define _CRT_SECURE_NO_WARNINGS
#define MAXGUESSES 4
#include <stdio.h>
void GuessItRules(); //Game instructions
int PlayOneGame(char letter); //Runs a game
char GetTheLetter(); //Prompts user to guess and returns guess
int IsAWinner(char letter, char userguess); //Returns either a 1 or a 0 depending on if the user guessed correctly
void CompareTheLetters(char letter, char userguess); //Prints message dependent on whether guess comes before, after, or is the letter
int main()
{
//declare additional variables
//declare FILE pointer
FILE* PTR;
int numOfgames; //number of games user wants to play
int i; //iterator
char letter; //letter from file
int win; //variable for the return of the PlayOneGame function
//display instructions
GuessItRules();
//connect to the file HINT: use fopen
PTR = fopen("letters.txt", "r");
//get number of games to play
printf("Let's start playing!\n\nHow many games would you like to play (1-5)?\n\n");
scanf("%d", &numOfgames);
//this for loop will allow the player to play more than one game
//without recompiling
for (i = 0; i < numOfgames; i++)
{
//get a letter from file - use fscanf
fscanf(PTR, "%c", &letter);
//Play one game (Call PlayOneGame function) - remember the function has an int return type
win = PlayOneGame(letter);
//tell the player if they have won or lost (test the variable win)
if (win == 1)
printf("Congrats! You guessed the correct letter!!\n\n");
else if (win == 0)
printf("I'm sorry you did not guess the correct answer :( The letter was %c\n\n", letter);
}
//close file
fclose(PTR);
return 0;
}
//Function definitions
void GuessItRules()
{
printf("Welcome to the Guess the Letter Game!\n-------------------------------------\n\n");
printf("You will have 4 chances to guess the letter per a game\n\n");
}
int PlayOneGame(char letter)
{
int numOfguesses = 0;
int winOrLose = 0; //should be intialized
char userguess; // user guess
//As long as the user has not used up the maximum number
//of guesses and has not guessed correctly
//the game will continue using this while loop
while (numOfguesses < MAXGUESSES && winOrLose == 0)
{
//function call to GetTheletter - returns to userguess variable
userguess = GetTheLetter();
//function call to IsAWinner - returns to winOrLose variable (0 or 1)
winOrLose = IsAWinner(letter, userguess);
//function call to CompareTheLetters - gives the user a message
CompareTheLetters(letter, userguess);
//update counter for number of guesses used
numOfguesses = numOfguesses + 1;
}
return winOrLose; //(0 or 1)
}
char GetTheLetter()
{
char userguess;
printf("Please enter a guess \n\n");
scanf("%c", &userguess);
return (userguess);
}
int IsAWinner(char letter, char userguess)
{
if (userguess == letter)
return (1);
else
return (0);
}
void CompareTheLetters(char letter, char userguess)
{
if (userguess < letter)
{
printf("The letter you are trying to guess comes after %c\n\n", userguess);
}
else if (userguess > letter)
{
printf("The letter you are trying to guess comes before %c\n\n", userguess);
}
else if (userguess == letter)
{
printf("The letter is %c!!!!\n\n", userguess);
printf("Congrats!!! You did it!!\n\n");
}
}
The problem lies in when you ask the user for a letter to guess.
When you use the %c format specifier to scanf, a newline satisfies this format specifier. Since there will have been a newline left in the input buffer from your previous input, this picks up that newline.
Put a space at the beginning of the format string to absorb the newline:
scanf(" %c", &userguess);
Note that other format specifiers such as %d and %f will skip over newlines, unlike %c. So the leading space to skip newlines is needed only for %c.
Related
The program isn't printing after giving me the first chance to guess.
#include <stdio.h>
#include <string.h>
int main() {
char menu;
int c = 0, flag = 0, life = 8;
printf("\nWelcome to Hangman!!!");
printf("\nThis is a game of hangman.");
printf("Player 1 enters a random word and the other has to guess it.");
printf("You get 8 lives in total i.e. you can have a maximum of 8 wrong guesses.");
printf("\n");
printf("Press n for new game\n");
printf("Press q to quit\n");
printf("\n");
scanf("%c", &menu);
int i = 0, j = 0;
char w[20], ch;
if (menu == 'q') {
printf("Exiting...");
printf("Thanks for playing");
}
else if (menu == 'n') {
printf("Player 1 enters a word\n");
scanf("%s", w);
int len = strlen(w);
for (int i = 0; i < len; i++) {
toupper(w[i]);
}
printf("\e[1;1H\e[2J");
char arr[len - 1];
for (int i = 0; i < len - 1; i++) {
arr[i] = '_';
printf("%c", arr[i]);
}
printf("\n");
while (life != 0) {
for (int i = 0; i < len - 1; i++) {
if (arr[i] == '_') {
flag = 1;
break;
}
else {
flag = 0;
}
}
if (flag == 0) {
printf("You Won!!\n");
printf("You Guessed The Word: %s", w);
break;
}
else {
char ans;
printf("Enter a letter between A-Z");
scanf("%c", ans);
toupper(ans);
for (int j = 0; j < len; j++) {
if (ans == w[j]) {
arr[j] = ans;
c++;
}
}
if (c == 0) {
life--;
}
c = 0;
for (int j = 0; j < len; j++) {
printf("%c", arr[j]);
}
printf("\n Lives Remaining= %d \n", life);
}
}
if (life == 0) {
printf("\n You Lost!!! \n");
printf("The Word Was: %s", w);
}
}
else {
printf("Invalid Character");
}
}
Output:
Welcome to Hangman!!!
This is a game of hangman.Player 1 enters a random word and the other has to >guess it.You get 8 lives in total i.e. you can have a maximum of 8 wrong >guesses.
Press n for new game
Press q to quit
n
Player 1 enters a word
Hello
Enter a letter between A-Z
PS C:\Users\arora\Desktop\Programs\C>
There are quite a few problems with your program. Here are the major ones:
You want to use use space prefix in the format string for scanf(" %c", ...) to ensure previous newlines are ignored.
scanf("%c", ans); should be &ans. It causes scanf() to fail rendering the remain of the program non-interactive. Without input from the user the core game logic doesn't work.
Here are some of the other issues:
#include <ctype.h>.
(not fixed) Consider changing the menu logic so 'q' quits, and any other letter starts a game.
Game prompt contains long lines that are hard to read for the player(s).
You use a printf() per line which makes it hard to read. Use a single call and multi-line strings as input.
Try to branch your code less by making use of early return. It makes it easier to read.
Check the return value of scanf(). If it fails then whatever variable it read doesn't have a well defined value.
Ensure that scanf() read no more than 19 bytes into a 20 byte array w. It takes a little macro magic to generate the 19 so I didn't make this change but it's a good idea to #define constants for magic values like the 20.
arr is not \0 terminated (len-1). Most c programmers expect a string so it's not worth the confusion to save 1 byte.
Use a function or macro for the ANSI escape to clear the screen.
Eliminate unused variables i, j.
Reduce scope of variables (declare variables close to where you use them).
The calculation of the flag variable is cumbersome.
(not fixed) The prompt "Enter a letter between A-Z" is somewhat ambiguous. Suggest "... between A and Z".
It's generally a good idea to leave user input as you read. If you care about the repeated toupper() you can create a copy of the user input with letters in upper case, and create another variable to hold the upper case version of the player's guess. This avoid you saying things like you entered the word "BOB" when the actual input was "bob".
You attempt to use toupper() to convert each letter to upper case but don't assign the result to anything so it does not do anything constructive.
Consider some functions to document what each your code does. I added some comments for now.
(mostly not fixed) Consider using better variable names (c, w, arr, flag).
(not fixed) Should you reject a word with your magic '_' value? In general should you validate that the word is reasonable (a-z, len > 0, len < 20)?
(not fixed) Consider, in arr, just storing if a letter was correctly guess (boolean). When evaluating the state show the letter from w if it is already guessed otherwise the _.
(not fixed) If you guess a correct letter again, it's considered a good guess. Should it?
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define clear() printf("\e[1;1H\e[2J")
int main() {
printf(
"Welcome to Hangman!!!\n"
"\n"
"This is a game of hangman.\n"
"Player 1 enters a random word and the other has to guess it.\n"
"You get 8 lives in total i.e. you can have a maximum of 8 wrong guesses.\n"
"\n"
"Press n for new game\n"
"Press q to quit\n"
);
char menu;
if(scanf(" %c",&menu) != 1) {
printf("scanf failed\n");
return 1;
}
switch(menu) {
case 'q':
printf(
"Exiting..."
"Thanks for playing\n"
);
return 0;
case 'n':
break;
default:
printf("Invalid Character");
return 1;
}
printf("Player 1 enters a word\n");
char w[20];
if(scanf("%19s", w) != 1) {
printf("scanf failed\n");
return 1;
}
clear();
char arr[20];
int len=strlen(w);
for(int i=0;i<len;i++) {
arr[i]='_';
}
arr[len] = '\0';
int life=8;
for(;;) {
printf("%d Lives Remaining\n", life);
// read a guess from player
for(int i = 0; i < len; i++) {
printf("%c", arr[i]);
}
printf(" Enter a letter between A-Z ");
char guess;
if(scanf(" %c", &guess) != 1) {
printf("scanf failed\n");
return 1;
}
// determine if any of the letters are in the secret word
int c = 0;
for(int i=0; i<len; i++) {
if(toupper(guess) == toupper(w[i])) {
arr[i]=guess;
c = 1;
}
}
if(c==0) {
life--;
}
// game over?
int flag = 0;
for(int i = 0; i<len; i++) {
if(arr[i]=='_') {
flag=1;
break;
}
}
if(flag==0) {
printf("You Won!!\n");
printf("You Guessed The Word: %s\n",w);
break;
}
if(life==0) {
printf("\n You Lost!!!\n");
printf("The Word Was: %s\n", w);
break;
}
}
}
I have this code and it keeps adding what ever the guesses string is to the wordle string when I compare them, resulting in them to never be the same. How can I fix this?
#include <string.h>
int main() {
char wordle[5];
char guesses[5];
int guess = 5;
int value;
printf("Please input a secret 5 letter word:\n");
scanf("%s",wordle);
while (guess != 0){
printf("You have %d tries, please guess the word\n",guess);
scanf("%s",guesses);
value = strcmp(wordle,guesses);
if (value == 0){
printf("you win\n");
break;
}
guess = guess - 1;
}
return 0;
}```
Your program has undefined behavior. You're making two mistakes.
If your user enters 5 characters, it takes 6 characters to store the string. The program would attempt to write a null terminator into wordle[5] which is not a valid index.
Your user could enter any number of letters. You need to make sure they don't overflow your buffer.
#include <stdio.h>
#include <string.h>
int main() {
char wordle[6];
char guesses[6];
int guess = 5;
int value;
int chars_read;
do {
printf("Please input a secret 5 letter word:\n");
chars_read = scanf("%5s%*s\n", wordle);
} while(chars_read != 1 && strlen(wordle) != 5);
while (guess != 0){
do {
printf("You have %d tries, please guess the word\n", guess);
chars_read = scanf("%5s%*s\n", guesses);
} while(chars_read != 1 && strlen(wordle) != 5);
value = strcmp(wordle, guesses);
if (value == 0){
printf("you win\n");
break;
}
guess = guess - 1;
}
return 0;
}
See it in action
scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
MSC24-C. Do not use deprecated or obsolescent functions
Your strings for wordle and guesses are too short. You need to make room for '\0'. They should be 6 bytes long not 5.
char wordle[6];
char guesses[6];
The error I got was that the number of game wasn't shown correctly.
For example, I chose to play 2 games. I, then, tried to guess the first number correctly. The solution was shown and then jumped to the next game. However, the second game was shown as Game 3 instead of Game 2.
I tried again. This time, I guessed the letter 1 time incorrectly and 1 time correctly. After the second guess, the game showed the solution and then stopped the game despite me choosing to play 2 games and only 1 game was played.
The order of letters in LetterList file is
d
B
G
w
Q
t
r
Y
u
X
So the first game starts with 'd' and then 'B' and then etc....
The error was shown as if the program itself got rid of even numbers.
I don't know what was wrong with it.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 5
void LetterGuessRules();
void GuessTheLetter(char);
char GetTheGuess();
int CompareLetters(char, char);
int main()
{
FILE *inPtr;
int numGames, i = 0;
char letter;
//display the game rule
LetterGuessRules();
printf("\nHow many games do you want to play? (Max 10) >> ");
scanf("%d", &numGames);
printf("\n\n************************************************\n");
inPtr = fopen("letterList.txt", "r");
for (i = 0; i < numGames; i++)
{
//get a solution letter from file - use fscanf
fscanf(inPtr," %c", &letter);
//change the solution to lowercase
letter = tolower(letter);
//print the solution back onto the screen to test
//Close this when play the game to hide the foreseen solution
printf("\nThe letter is %c\n", letter);
//Number of match
printf("\t\tGame %d\n", i += 1);
//call the GuessTheLetter function and pass it the solution
GuessTheLetter(letter);
}
fclose(inPtr);
return 0;
}
void GuessTheLetter(char letter)
{
int win = 0;
int numGuesses = 0;
char myGuess;
while (numGuesses < MAXGUESSES && win == 0)
{
//get a guess from the user by calling the GetTheGuess function
myGuess = GetTheGuess();
//change the guess to lowercase
myGuess = tolower(myGuess);
//win = call the function to compare the guess with the solution
win = CompareLetters(letter, myGuess);
numGuesses++;//count the number of guesses so far
//use conditions to let the user know if they won or lost the round of the game
if (win == 0)
{
printf("\nOops its wrong.\n");
if (myGuess < letter)
{
printf("Your guessed letter -%c- comes before the solution\n", myGuess);
printf("Please guess again :)\n");
}
else if (myGuess > letter)
{
printf("Your guessed letter -%c- comes after the solution\n", myGuess);
printf("Please guess again :)\n");
}
if (numGuesses == MAXGUESSES && win == 0)
printf("Aw you have lost this game!");
printf("\n");
}
else if (win == 1)
{
printf("\nYou have guessed it right!\n");
printf("Wonderful! You ACE'd this match!\n");
printf("\n");
printf("**** If you play more than 1 game, new match will automatically start ****\n");
printf("\tYou only need to keep guessing for the next letter\n");
printf("------------------------------------------------------------------------------");
printf("\n");
}
}
}
char GetTheGuess()
{
char myGuess;
printf("\t_______________________");
printf("\n\t|What's your guess? >> ");
scanf(" %c", &myGuess);
return myGuess;
}
void LetterGuessRules()
{
printf("\n*** Instruction: ");
printf("\nYou will have 5 attempts to guess the right answer");
printf("\nIf you guess it right, the game will end with your victory.");
printf("\nOtherwise, you will have to guess again.");
printf("\nPlease have fun!");
}
int CompareLetters(char letter, char myGuess)
{
if (letter == myGuess)
{
return 1;
}
else
{
return 0;
}
}
The problem is you increment i twice. First in the for loop, and again here:
//Number of match
printf("\t\tGame %d\n", i += 1);
This is probably because you got Game 0 without it. The simple fix is to start the loop at 1 instead of 0.
Since i means something more than the "loop iterator" I'd call it something more descriptive like gameNum.
/* from 1 to numGames */
int gameNum;
for( gameNum = 1; gameNum <= numGames; gameNum++ ) {
...
}
Note that I check gameNum <= numGames rather than gameNum < numGames since we're now starting from 1.
Also you need to check if the file opened, otherwise it crashes if letterList.txt doesn't exist.
#include <errno.h> /* for errno */
#include <string.h> /* for strerror() */
#include <stdlib.h> /* for exit() */
inPtr = fopen("letterList.txt", "r");
if( inPtr == NULL ) {
fprintf(stderr, "Could not open letterList.txt: %s\n", strerror(errno));
exit(1);
}
Finally, I'd recommend against using #define _CRT_SECURE_NO_WARNINGS while learning C. Those security warnings are important.
Ok, been looking at this for hours now and could be just because it's late, but I'm braindead. So the code is supposed to accomplish the following:
Display instructions
Ask user for number of games to play
For each game
a. get a letter from file
b. Play one game
i. While still has guesses
ii. Get guess from player
iii. Compare guess to letter from file
iv. If guess is correct, return 1
v. Else increase counter
vi. Return 0 if 5 guesses incorrect
c. check for win or lose
This is the code I have so far, any ideas how to get this thing to run properly?:
#define _CRT_SECURE_NO_WARNINGS
#define MAXGUESSES 5
#include <stdio.h>
//Pre-Processor Directives
void gameRules();
//Game Instructions
int singleGame(char fileLetter);
//Checks guesses, returns 0 after 5 incorrect attempts
char retrieveGuess();
//Prompts user for guess and returns guess
//Called from inside singleGame function
int guessedIt(char answer, char inputLetter);
//Lets user know if answer comes before or after their guess.
//Returns 1 if answer is correct, and 0 for incorrect.
//Called from inside singleGame function.
int main()
{
char fileLetter = 'a';
FILE* filePtr;
int numOfGames = 0;
int counter = 0;
//run it
//Rules of Game
gameRules();
//File pointer that opens the file, "r" stands for reading the file
filePtr = fopen("lettersin.txt", "r");
/* File checking if-else
if (filePtr == NULL)
{
printf("I don't exist");
}
else
{
printf("I'm alive");
}
*/
printf("How many games would you like to play? (Pick from 1-4)\n");
scanf("%d", &numOfGames);
for (counter = 0; counter < numOfGames; counter++)
{
//Pulls a letter from the file and writes it to variable "fileLetter"
//Note to self: make sure to include "filePtr" so fscanf knows where to pull from
fscanf(filePtr, " %c", &fileLetter);
singleGame(fileLetter);
}
//Closes file, not necessary now, but good for later on in bigger programs that may need more files
fclose(filePtr);
return 0;
}
//Game rules function
void gameRules()
{
printf("Welcome to the Letter Guessing Game\n");
printf("You will enter the number of games you want to play (1-4 games).\n");
printf("You have 5 chances to guess each letter.\n");
printf("Let's begin:\n");
}
//Single Game Function
int singleGame(char fileLetter)
{
int numGuesses = 0;
while (numGuesses < MAXGUESSES)
{
retrieveGuess();
numGuesses = numGuesses + 1;
}
return 0;
}
//Guess retrieval function
char retrieveGuess()
{
char inputLetter;
printf("What is your guess? (Lowercase letters only, please)\n");
scanf(" %c", &inputLetter);
int guessedIt(char fileLetter, char inputLetter);
}
//Correct, or not, function
int guessedIt(char fileLetter, char inputLetter)
{
if (fileLetter == inputLetter)
{
printf("You guessed it!\n");
return '1';
}
else if (inputLetter < fileLetter)
{
printf("The letter comes after your guessed letter.\n");
return '0';
}
else
{
printf("The letter comes before your guessed letter.\n");
return '0';
}
}
A few things:
//Single Game Function
int singleGame(char fileLetter)
{
int numGuesses = 0;
while (numGuesses < MAXGUESSES)
{
retrieveGuess();
numGuesses = numGuesses + 1;
// You should return 1 here if the guess is right, no?
// Something like:
// char guessedChar = retrieveGuess();
// place call for function guessedIt here and if it
// is 1 return the result (if it's 0, don't return, let it keep looping)
}
return 0;
}
This function:
//Guess retrieval function
char retrieveGuess()
{
char inputLetter;
printf("What is your guess? (Lowercase letters only, please)\n");
scanf(" %c", &inputLetter);
int guessedIt(char fileLetter, char inputLetter);
}
Isn't returning anything. You need a return statement for each function that you define as returning something. Also, I don't think the last statement even makes sense, just return inputLetter
This function:
//Correct, or not, function
int guessedIt(char fileLetter, char inputLetter)
{
if (fileLetter == inputLetter)
{
printf("You guessed it!\n");
return '1';
}
else if (inputLetter < fileLetter)
{
printf("The letter comes after your guessed letter.\n");
return '0';
}
else
{
printf("The letter comes before your guessed letter.\n");
return '0';
}
}
Is returning a char ('1'/'0') even though it declares to be returning an int. You want to write return 1;/return 0;. You should place it where I put the comments
I am trying to get both parts of this program to work as you can see I have split it with first part (question 1) and second part (question 2). The problem is first part runs fine just when the second part starts I can not input any string and it just seems to skip through the code without letting me input the string.
If I delete the first part (question 1) of the program then everything works fine and I can input the string. What interrance is causing this issue.
int main()
{
first();
second();
}
//Question 1
int first()
{
/* dataarray.c */
float data[20] = {
50.972438, 93.765053, 9.252207, 1.851414, 16.717533,
71.583113, 97.377304, 20.352015, 56.309875, 0.072826,
23.986237, 36.685959, 80.911919, 86.621851, 53.453706,
96.443735, 29.845786, 18.119300, 31.079443, 52.197715 };
/* The number of elements in the data array */
int data_size = 20;
int pos;
int j;
int i;
int k;
printf("Question 1\n");
for(i=0;i<data_size;i++)
{
printf("\nArray %i is %f ",i,data[i]); //Initial arrays print statement
}
printf("\n\nArray number to delete:"); //User Choose one to delete
scanf("%i",&pos);
k =0;
for(j = 0; j< pos;j++)
{
printf("\n Array %i is now %f ",k,data[j]);
k++;
}
k=pos;
for(j=pos+1;j<data_size;j++)
{
printf("\n Array %i is now to %f ",k,data[j]); //Shows array changed to
k++;
}
data_size = data_size - 1; //Decreases data size
}
//Question 2
int second()
{
printf("\n\nQuestion 2\n");
int a,b,check=0;
char str[20];
printf("\nEnter a String:\n"); //User inputs word to check if its palindrome
gets(str);
for(b=0;str[b]!=0;b++); //Starts at 0 increment till the last length
b=b-1;
a=0;
while(a<=b)
{
if(str[a]==str[b]) //String a is forwards b is backwards
{
a=a+1;
b=b-1;
check=1; //If check = 1 then a palindrome
}
else
{
check=0; //If check = 0 then it not a plaindrome
break; //Loop break
}
}
if(check==1)
printf("It is a Palindrome:"); //Statement printed if check = 1
else
printf("It is not a Palindrome\n"); // Else if 0 this statement is printed
}
When you call scanf in part one, I presume you enter a number followed by a newline. scanf consumes the number, but the newline is left in the buffer. The gets() in part 2 then sees the newline and returns a blank line. An easy solution is to do
scanf( "%i\n", &pos );
BTW, never use gets. Use fgets instead.