# Guess word Problem

• 10-03-2012
Hemerson
Guess word Problem
hello there, i got problem with my code, when i starting to guess a letter and hit the enter, the letter i've guess is not printing in the blank i made. can someone help me to fix the code? thanks.

Code:

int main(){
int i;
int j;
int x=0;
char word[30];
char undscr[30];
char length;
char letter;

printf("enter a word: ");
gets(word);

length=strlen(word);
undscr[x]='_';
for(i=0;i<length;i++)
{
printf(" %c", undscr[x]);
}

printf("\n\nguess the letter: ");
scanf("%c", &letter);
for(j=0;j<i;j++)
{
if(letter==word[i])
{
undscr[i]=word[i];
}
}

getch();
return 0;
}

• 10-03-2012
Hemerson
• 10-03-2012
Try this:
Code:

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main(){
int i;
int done=0;
int good=0;
char word[30];
char undscr[30];
char length;
char letter;

_gotoxy(1,3);
printf("enter a word: ");
gets(word);

length=strlen(word);
for(i=0;i<length;i++) {
undscr[i]='_';
}
while(!done) {
_gotoxy(1,5);
for(i=0;i<length;i++)
{
printf("%c ", undscr[i]);
}
if(good>=length) {
done=1;
break;
}

printf("\n\nguess the letter: ");
letter=_getch();
printf("%c",'\b');

for(i=0;i<length;i++)
{
if(letter==word[i])
{
undscr[i]=word[i];
good++;
}
}
}
_gotoxy(1,9);
if(done)
printf("You guessed it!\n\n");
getchar();
return 0;
}

• 10-03-2012
Hemerson
Quote:

Try this:
Code:

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main(){
int i;
int done=0;
int good=0;
char word[30];
char undscr[30];
char length;
char letter;

_gotoxy(1,3);
printf("enter a word: ");
gets(word);

length=strlen(word);
for(i=0;i<length;i++) {
undscr[i]='_';
}
while(!done) {
_gotoxy(1,5);
for(i=0;i<length;i++)
{
printf("%c ", undscr[i]);
}
if(good>=length) {
done=1;
break;
}

printf("\n\nguess the letter: ");
letter=_getch();
printf("%c",'\b');

for(i=0;i<length;i++)
{
if(letter==word[i])
{
undscr[i]=word[i];
good++;
}
}
}
_gotoxy(1,9);
if(done)
printf("You guessed it!\n\n");
getchar();
return 0;
}

[Linker error] undefined reference to `gotoxy' , im using dec c++ as a compiler.
• 10-03-2012
Remove the underscore from in front of _gotoxy's. gotoxy() is part of conio.h, but my compiler uses the leading underscore for it nowadays.
• 10-03-2012
Hemerson
Quote:

Remove the underscore from in front of _gotoxy's. gotoxy() is part of conio.h, but my compiler uses the leading underscore for it nowadays.

its working thanks!!...
• 10-03-2012
Hemerson
but its only works in TurboC not in DevC++.
• 10-04-2012