I have this function that reads text from a user and counts how many letters of each type there are. However, the function doesn't wait for the user input when called.
What I'd typically try is to put a space before the %c however adding this space makes it so the program doesn't proceed after you hit enter.
void ReadText(int histo[], int *max) {
int i;
char userInput;
printf("ENTER A LINE OF TEXT:\n");
do{
if (scanf("%c", &userInput) != 1)
break;
if(userInput >= 97 && userInput <= 122)
histo[userInput-97] = histo[userInput-97] + 1;
else if(userInput >= 65 && userInput <= 90)
histo[userInput-65] = histo[userInput-65] + 1;
}while(userInput != '\n');
for(i = 0; i < 26; i++)
printf("%d ", histo[i]);
*max = histo[0];
for(i = 0; i < 25; i++)
if(histo[i] > *max)
*max = histo[i];
}
int main() {
char command;
int i, max;
int histo[26] = {0};
//User Input Validation
do{
printf("Enter command (C, R, P, or Q): ");
scanf(" %c", &command);
switch(command) {
case 'C': case 'c':
for(i = 0; i < 25; i++)
histo[i] = 0;
break;
case 'R': case 'r':
ReadText(histo, &max);
break;
case 'P': case 'p':
DrawHist(histo, max);
break;
case 'Q': case 'q':
return 0;
default:
printf("Invalid Command %c", command);
printf("\n");
}
}while(1);
return 0;
}