Inputting two "Enter a letter"
Good evening. I'm writing a program to squeeze a string removing every character the user wants. I used fgetc(stdin) for input but I'm getting that buffer problem:
Code:
Enter the word you want: thames
Enter the number of letters of the alphabet you want (1-26): 4
Enter a letter: Enter a letter: t
Enter a letter: Enter a letter: h
t
h
P.S.: I also tried to scanf the string and use getchar with c.
Code:
#include <stdio.h>
#include <stdlib.h> /* for malloc() and free() */
#include <ctype.h>
#ifndef MAXLINE
#define MAXLINE 100
#endif
#ifndef ALPHA
#define ALPHA 26
#endif
/* the function removes t characters chosen by the user from the s string */
void squeeze(char* s, char* t)
{
}
int main(void)
{
char* s;
char* t;
int c;
int n;
int i = 0;
s = malloc(MAXLINE * sizeof(char*));
t = malloc(ALPHA * sizeof(char*));
printf("Enter the word you want: ");
fgets(s, MAXLINE, stdin);
do {
printf("Enter the number of letters of the alphabet you want (1-26): ");
scanf("%d", &n);
} while(n < 1 || n > 26);
while(i < n)
{
printf("Enter a letter: ");
c = fgetc(stdin);
if( (isalpha(c) == EOF))
{
printf("The input character is invalid");
exit(0);
}
t[i++] = c;
}
printf("%s\n", t);
free(s);
free(t);
return 0;
}