bigtamscot
Programmer
Hi, I have a problem with input of char data from the keyboard. I have tried functions I know about (learner here). Tried getchar(), which as we know creates a buffer until return key pressed then stores return as next char. Tried #include<conio.h> and function getch(), which does the same. Most success I have had is with #include <conio.h> with getche(). The troublr with the latter is that it accepts the input char but duplicates it at the next stage of the program which requires input from keyboard. My compiler is bloodshed devC++ and operating system win98.
Example
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
char magic[] = "concatenation";
char guess[] = "-------------";
char word[strlen(magic)];
char ans, a;
int i, attempts = 1;
printf("Program to play hangman where user enters letters\n"
printf("Then tries to guess the magic word in under 15 attempts\n"
do
{
printf("\"%s\"\n", guess);
printf("Enter a letter : \n"
ans = tolower(getche());
a = strlen(magic);
for(i = 0; i < a; i++)
{
if(ans == magic)
guess = ans;
}
printf("\n\"%s\"\n", guess);
printf("Enter your word guess #%d: \n", attempts);
gets(word);
if(!strcmp(magic, word))
{
printf("You Win !!\n"
printf("In %d Attempts\n", attempts);
attempts = 16;
}
attempts++;
} while(attempts < 16);
return 0;
}
output from this would be when run:
--------------
Enter a letter
c /*users input here*/
c--c----------
Enter your word guess #1
c /*users input duplicated before next point of input*/
/*imagine if single char value was asked for*/
Any help gratefully appreciated Hoping to get certified..in C programming.
Example
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
char magic[] = "concatenation";
char guess[] = "-------------";
char word[strlen(magic)];
char ans, a;
int i, attempts = 1;
printf("Program to play hangman where user enters letters\n"
printf("Then tries to guess the magic word in under 15 attempts\n"
do
{
printf("\"%s\"\n", guess);
printf("Enter a letter : \n"
ans = tolower(getche());
a = strlen(magic);
for(i = 0; i < a; i++)
{
if(ans == magic)
guess = ans;
}
printf("\n\"%s\"\n", guess);
printf("Enter your word guess #%d: \n", attempts);
gets(word);
if(!strcmp(magic, word))
{
printf("You Win !!\n"
printf("In %d Attempts\n", attempts);
attempts = 16;
}
attempts++;
} while(attempts < 16);
return 0;
}
output from this would be when run:
--------------
Enter a letter
c /*users input here*/
c--c----------
Enter your word guess #1
c /*users input duplicated before next point of input*/
/*imagine if single char value was asked for*/
Any help gratefully appreciated Hoping to get certified..in C programming.