Im doing a hangman game as a school project but Im facing an issue:
do{
system("cls");
// Header of the game
printf("\n HANGMAN GAME\n\n\n");
// Present letters found
for (i=0; word[i]!='\0'; i++)
printf (" %c ", word_2[i]);
printf("\n");
// Present positions to the letters
for (i=0; word[i]!='\0'; i++)
printf("___ ");
printf("\n");
// ****PLAYER'S ANSWERS*****
// Read player's answers
printf("\n\n Whats your guess + <enter>: ");
scanf("%c", &letter);
scanf("%c", &c);
// Verify if the letter is in the word
found=0;
for(i=0; word[i]!='\0'; i++)
if (word[i] == letter){
word_2[i] = letter;
corrects++;
max_attemps--;
found = 1;
printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
if(found == 0){
max_attemps--;
printf("\nOh no, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
if (max_attemps <= 0 || corrects == lenght) {
end = 1;
}
} while (end == 0);
When I got a right letter that have two or more position in the word it takes from me two or more attempts because of the system("pause") when in fact just had to takes me one. But if I don't put the system("pause") the board will be cleared before I can see the message. Anyone knows what can I do to solve this? I'll be very grateful.
Check your found flag only after you have scanned the whole word:
// Verify if the letter is in the word
found=0;
for(i=0; word[i]!='\0'; i++)
if (word[i] == letter){
word_2[i] = letter;
corrects++;
max_attemps--;
found = 1;
// comment out
// printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
// system("pause");
}
if(found == 1)
{
printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
else{
max_attemps--;
printf("\nOh no, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
if (max_attemps <= 0 || corrects == lenght) {
end = 1;
}
Related
i tried to make a calculator but could the compiler is checking my if conditions properly.
here is my code,
i could not figure out how to solve this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int first;
int sec;
char mode;
printf("enter your forst number : ");
scanf("%d",&first);
printf("enter your second number : ");
scanf("%d",&sec);
printf("to add press \"a\" \n");
printf("to subtract press \"s\" \n");
printf("to multiply press \"m\" \n");
printf("to divide press \"d\" \n");
printf("so, what do you wanna do ");
scanf(" %c",&mode);
printf("%d %d %s \n",first,sec,mode);
if (mode == 'a')
{
printf("%d \n",first + sec);
}
else if (mode == "s")
{
printf("%d \n",first-sec);
}
else if (mode == "m")
{
printf("%d \n",first*sec);
}
else if (mode == "d")
{
printf("%d \n",first/sec);
}
else
{
printf("enter a valid operation code \n");
}
return 0;
}
void def(char name[],int age)
{
printf("het ur a %s and yo age is %i \n",name,age);
}
first attempt tried using a string instead of character (failed )
second attempt tried using a character but failed though!!
For comparing a single character in c, you must use single quotes. Consider an array of characters array[4] = {'c','a','r','\0'};
if(array[0] == 'c'){
//...
}
Comparing with double quotes, make the value inside it a string. For comparing strings you should #include <strings.h> and use the strcmp function.
There are two problems
mode == "s" mode is a char so just use mode == 's' and do the same for others also.
printf("%d %d %s \n",first,sec,mode); should be printf("%d %d %c \n",first,sec,mode);
you are using %s for printing a char
In your code "s" is a string not a caracter,to get a caracter you should write like this: 's'
here is the correction of your code :
int main()
{
int first;
int sec;
char mode;
printf("enter your forst number : ");
scanf("%d",&first);
printf("enter your second number : ");
scanf("%d",&sec);
printf("\nMenu : \n");
printf("\nto add press \"a\" \n");
printf("to subtract press \"s\" \n");
printf("to multiply press \"m\" \n");
printf("to divide press \"d\" \n");
printf("so, what do you wanna do ");
scanf(" %c", &mode);
printf("%d %d %c \n",first,sec,mode);
if(mode == 'a')
{
printf("%d \n",first + sec);
}
else if (mode == 's')
{
printf("%d \n",first-sec);
}
else if (mode == 'm')
{
printf("%d \n",first*sec);
}
else if (mode == 'd')
{
printf("%d \n",first/sec);
}
else
{
printf("enter a valid operation code \n");
}
return 0;
}
I want to compare the two strings and show the amount of wins for each player. I do not quite understand how the string.h library works, but in searches I've shown that it should work for this comparison
#include <stdio.h>
#include <string.h>
int main()
{
printf("Player 1: ");
scanf("%s", &play1);
printf("Player 2: ");
scanf("%s", &play2);
printf("Total matches: ");
scanf("%d", &t_matches);
for (i = 1; i <= t_matches; i++) {
printf("Winner match %d: ", i);
scanf("%s", &win1);
if (strcmp(win1, play1)) {
p1++;
} else if(strcmp (win1, play2)) {
p2++;
}
}
printf("%s win %d matches\n", play1, p1);
printf("%s win %d matches\n", play2, p2);
}
The strcmp function returns 0 if the strings are equal. You're checking if they are unequal. You instead want:
if (strcmp(win1, play1) == 0) {
p1++;
} else if(strcmp (win1, play2) == 0) {
p2++;
}
Hey guys im learning C and i started programming a Hangman Game. That should be played over 1 word and i choosed "informatik".
What i want to do is asking for users name and then start.
#include<stdio.h>
int main() {
int correct = 0;
int mistake = 0;
int number = 0;
char name[20];
char Word[]={'i','n','f','o','r','m','a','t','i','k'};
char guessStatus[]={'_','_','_','_','_','_','_','_','_','_','\0'};
char guess;
printf("##### Welcome to Hangman #####\nWhat is your Name?\n");
scanf(" %s", name);
fflush(stdin);
printf("Hello %s!\n", name);
while (mistake <=10 && number<10) {
printf("Guess a letter: ");
guess = getchar();
printf("%c", guess);
correct = 0;
for (int search=0; search<10; search++)
{
if (guess == Word[search]){
number++;
guessStatus[search] = Word[search];
correct = 1;
}
}
if (correct == 0){
mistake++;
printf("%c is wrong!\nYour Status is: %s\nYou have %i tries left\n", guess,guessStatus,10-mistake);
}
else {
printf("Good job %s!\nYour Status is: %s\nYou have %i tries left\n", name,guessStatus,10-mistake);
}
}
}
Problem is when i run the code and enter any letter, program counts "enter" also as a letter.I used fflush(stdin) as a hope even i don't know anything about it but didn't work :)
Problem output like:
Welcome to Hangman
What is your Name?
john
Hello john!
Guess a letter:
is wrong!
Your Status is: __________
You have 9 tries left
Guess a letter: i
iGood job john!
Your Status is: i_______i_
You have 9 tries left
Guess a letter:
is wrong!
Your Status is: i_______i_
You have 8 tries left
I don't know how to deal with it.Help would be very appreciated.
Add the gethcar(); will be ok for read the \n:
#include <stdio.h>
int main() {
int correct = 0;
int mistake = 0;
int number = 0;
char name[20];
char Word[]={'i','n','f','o','r','m','a','t','i','k'};
char guessStatus[]={'_','_','_','_','_','_','_','_','_','_','\0'};
char guess;
printf("##### Welcome to Hangman #####\nWhat is your Name?\n");
scanf(" %s", name);
getchar(); // add this line
printf("Hello %s!\n", name);
while (mistake <=10 && number<10) {
printf("Guess a letter: ");
guess = getchar();
printf("%c", guess);
correct = 0;
for (int search=0; search<10; search++)
{
if (guess == Word[search]){
number++;
guessStatus[search] = Word[search];
correct = 1;
}
}
if (correct == 0){
mistake++;
printf("%c is wrong!\nYour Status is: %s\nYou have %i tries left\n", guess,guessStatus,10-mistake);
}
else {
printf("Good job %s!\nYour Status is: %s\nYou have %i tries left\n", name,guessStatus,10-mistake);
}
}
}
try this it should work
Use
scanf(" %c",&guess);
instead of
guess = getchar(); //remove this line
printf("%c", guess); //remove this line also
the hang man has a problem please identify it??
the letters when typed the code shows 'not found'and the same letter has to be typed twice to get it accepted? and the chances to guess the letter decreases how to fix it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define WORD_COUNT 3
#define MAX_LENGTH 10
typedef char string[MAX_LENGTH];
void main(void) {
string words[WORD_COUNT] = { "bird","fish","lion","ants","bear","deer","fowl" };
char answer[MAX_LENGTH];
char guess;
int count = -0, 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); a biref menu case
switch (choice) {
case 1:
count = 5;
break;
case 2:
count = 2;
break;
case 3:
count = 1;
}
srand(time(NULL));
index = rand() % WORD_COUNT;
strcpy(mysteryWord, words[index]);/*actual comparing */
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 = 0;
for (i = 0; i < strlen(mysteryWord); i++)
{
if (mysteryWord[i] == guess) {
answer[i] = guess;
found = 1;
}
}
if (found == 0) {
printf("Not found!\n");
--count;
}
if (count == 0) {
printf("Game over\n");
printf("The answer is %s.", mysteryWord);
break;
}
else {
what should be here instead of if(answer==mysteryWord) ?
if (strcmp(answer, mysteryWord) == 0)
{
printf("Yes, it's a %s\n", answer);
break; /* or return */
} else
printf("%s", answer);
}
} end of while loop ?
} end of main ?
Change
scanf("%c\n", &guess);
To
scanf(" %c", &guess);
Note the space before %c. The space discards all blanks like newlines and spaces and the %c will then scan the next non-whitespace character.
In your case,when you input data for any scanf,you enter the data and press the enter key.scanf reads the data entered and leaves the \n(newline character) in the stdin. When you scan a character using %c , scanf reads the \n left out by the previous scanf and thus,does not wait for input.
scanf(" %c", &guess);
Please make sure your scanf() is like above with a space before %c
The purpose of space is it gobbles whitespace and special characters
Currently I have set up an array to hold 50 packets created by the program. However I would like to change this array to use malloc instead along with the use of storing a maximum of 50 pieces of information just as the current array does.
Here's the current code used to set up the array used under int main
struct packet create[50];
int countpackets = 0;
And the array is incremented within another function within the code as so
int add(struct packet *create, int countpackets){
char inputDest[10],inputType[4],inputPort[2],inputData[50],inputSource[10];
if (countpackets == 50){
puts("Too many packets already entered.");
}else{
printf("\n\n");
int i = 0;
printf("\t Source - Must be 1024 or below >\t");
scanf("%s", inputSource);
create[countpackets].source = atoi(inputSource);
for (i = 0; i < strlen(inputSource); i++){
while(isdigit(inputSource[i])== 0 || create[countpackets].source > 1024){
printf("************************************************** \n");
puts("Invalid input, numbers only or number too big\n");
printf("\t please re-enter your Source >");
scanf("%s", inputSource); //rescans if it's a letter
create[countpackets].source = atoi(inputSource);
}
}
printf("\t Destination - Must be 1024 or below >\t");
scanf("%s", inputDest);
create[countpackets].destination = atoi(inputDest);
for (i = 0; i < strlen(inputDest); i++)
{
while(isdigit(inputDest[i])== 0 || create[countpackets].destination > 1024){
printf("************************************************** \n");
puts("Invalid input, numbers only or number too big\n");
printf("\t please re-enter your Destination >");
scanf("%s", inputDest); //rescans if it's a letter
create[countpackets].destination = atoi(inputDest);
}
}
printf("\t Type - Must be 10 or below >\t");
scanf("%s", inputType);
create[countpackets].type = atoi(inputType);
for (i = 0; i < strlen(inputType); i++){
while(isdigit(inputType[i])== 0 || create[countpackets].type > 10){
printf("************************************************** \n");
puts("Invalid input, numbers only or number too big \t \n");
printf("\t please re-enter your Type >");
scanf("%s", inputType); //rescans if it's a letter
create[countpackets].type = atoi(inputType);
}
}
printf("\t Port - Must be 1024 or below >\t");
scanf("%s", inputPort);
create[countpackets].port = atoi(inputPort);
for (i = 0; i < strlen(inputPort); i++)
{
while(isdigit(inputPort[i])== 0 || create[countpackets].port > 1024){
printf("************************************************** \n");
puts("Invalid input, numbers only or number too big \t \n");
printf("\t please re-enter your Type >");
scanf("%s", inputPort); //rescans if it's a letter
create[countpackets].port = atoi(inputPort);
}
}
printf("\t Data have less than 50 characters >\t");
scanf("%s", inputData);
for (i = 0; i < strlen(inputData); i++){
while(isdigit(inputData[i])== 0){ //checks if it's a letter
printf("************************************************** \n");
puts("Invalid input, numbers only or number too big \t \n");
printf("\t please re-enter your Type >");
scanf("%s", inputData); //rescans if it's a letter
}
strcpy(create[countpackets].data, inputData);
}
}
countpackets++;
return countpackets;
}
I'm pretty new to C and I do believe that is all the code I need to show, however if need be I will put up my full program. Any help would be much appreciated.
If you want to declare this...
struct packet create[50];
...with malloc, you'll have to do this...
struct packet *pCreate = malloc(50 * sizeof(struct packet));
You can use it the same.
create[0] = pCreate[0]; // First element
create[49] = pCreate[49]; // Last element