Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with char inputs without buffering

Status
Not open for further replies.

bigtamscot

Programmer
Apr 28, 2001
37
GB
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[] = &quot;concatenation&quot;;
char guess[] = &quot;-------------&quot;;
char word[strlen(magic)];
char ans, a;
int i, attempts = 1;

printf(&quot;Program to play hangman where user enters letters\n&quot;);
printf(&quot;Then tries to guess the magic word in under 15 attempts\n&quot;);

do
{
printf(&quot;\&quot;%s\&quot;\n&quot;, guess);
printf(&quot;Enter a letter : \n&quot;);
ans = tolower(getche());

a = strlen(magic);
for(i = 0; i < a; i++)
{
if(ans == magic)
guess = ans;
}
printf(&quot;\n\&quot;%s\&quot;\n&quot;, guess);
printf(&quot;Enter your word guess #%d: \n&quot;, attempts);
gets(word);
if(!strcmp(magic, word))
{
printf(&quot;You Win !!\n&quot;);
printf(&quot;In %d Attempts\n&quot;, 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.
 
Hi, Below are a few suggestions and a way to get around your problem.

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

You can get rid of this. See below.

>#include <ctype.h>
>#include <stdlib.h>


int
discardUpToNewline(void)
{
int c;

while ((c=getchar())!='\n' && c!=EOF)
; /* empty loop */
return c==EOF;
}

this will discard everything up through the newline.

>int main(void)
>{
> char magic[] = &quot;concatenation&quot;;
> char guess[] = &quot;-------------&quot;;
> char word[strlen(magic)];

Variable length arrays aren't allowed by the current C standard, although the new
standard (C99) (as yet unimplemented) does support them.

> char ans, a;

int ans;
size_t a;

for getchar(), which returns an int (see below).
strlen() returns a size_t

> int i, attempts = 1;

> printf(&quot;Program to play hangman where user enters letters\n&quot;);
> printf(&quot;Then tries to guess the magic word in under 15 attempts\n&quot;);

> do
> {
> printf(&quot;\&quot;%s\&quot;\n&quot;, guess);
> printf(&quot;Enter a letter : \n&quot;);
> ans = tolower(getche());

ans=tolower((unsigned char)getchar());

The unsigned char cast is needed because getchar() returns an int
and tolower() requires an unsigned char or EOF.

if (EOF==discardUpToNewline()) {
/* User sent an EOF */
break;
}

Discard all input up through the newline.

> a = strlen(magic);

You could do this outside of the loop rather than calculate
it every time.

> for(i = 0; i < a; i++)
> {
> if(ans == magic)
> guess = ans;
> }

Maybe make a comparison here if the user has entered all the
correct letters.

> printf(&quot;\n\&quot;%s\&quot;\n&quot;, guess);
> printf(&quot;Enter your word guess #%d: \n&quot;, attempts);
> gets(word);

gets() is a bug because it doesn't stop the user from entering
more characters than your buffer can handle. Use fgets()
instead.

if (fgets(word,sizeof word,stdin)!=NULL) {
/* ... */

> if(!strcmp(magic, word))
> {
> printf(&quot;You Win !!\n&quot;);
> printf(&quot;In %d Attempts\n&quot;, attempts);
> attempts = 16;
> }
> attempts++;
> } while(attempts < 16);

> return 0;
>}

Also, make sure you uncheck the Process TGML box or enclose your code in
Code:
tags to prevent from being interpreted as &quot;start italics.&quot;

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top