0

I am supposed to come up with a program to calculate the number of words, vowels, lowercase letters and alphabetic letters for an assignment.

And I have two questions, why cant I use while (getchar() != '\n') instead of assigning it to a variable ch and why fflush(stdin) must be used???

    #include <stdio.h>
    #include <ctype.h>

    int main(void)
    {

        char ch, repeat;
        int numVowel = 0, numLower = 0, numAlpha = 0, numWords = 0, inWord = 0;

        // Tells user what the program does
        printf("This program prompts a user to enter a piece of text and\ncalculates the number of alphabetic letters, lowercase letters, \nvowels and words in that piece of text.\n\n");


        do
        {
             printf("Type the text here (hit \"Enter\" to end): ");  

             while ((ch = getchar()) != '\n')         //*dont understand this part*
            {
                    if (ch == ' ')                             
                        inWord = 0;

            else if (inWord == 0 && isalpha(ch))     
            {                                       
                inWord = 1;
                numWords += 1;
            }

            if (isalpha(ch))             
                numAlpha += 1;

            if (islower(ch))             
                numLower += 1;

            switch(ch)                   
            {
                case 'a':
                case 'A':
                case 'e':
                case 'E':
                case 'i':
                case 'I':
                case 'o':
                case 'O':
                case 'u':
                case 'U': numVowel += 1;
                          break;
            }
        }

        printf("\nNumber of alphabetical letters: %-4d\n", numAlpha);
        printf("Number of lowercase letters   : %-4d\n", numLower);
        printf("Number of vowels              : %-4d\n", numVowel);
        printf("Number of words               : %-4d\n\n", numWords);

        do
        {
            printf("Do you want to try again? (Y/N): ");

            scanf("%c", &repeat);
            repeat = toupper(repeat);
            if (repeat != 'Y' && repeat != 'N')         
                printf("Invalid answer. Please enter 'Y' or 'N'.\n\n");
            fflush(stdin);               //*why is this required?*               

        } while (repeat != 'N' && repeat != 'Y');       

        printf("\n");
        fflush(stdin);                   //*why is this required?*
        numVowel = 0, numLower = 0, numAlpha = 0, numWords = 0, inWord = 0;  

    } while (repeat == 'Y');           


    return 0;
}
Nurjan
  • 5,889
  • 5
  • 34
  • 54
12johnny
  • 71
  • 2
  • 8

2 Answers2

1

why cant I use while (getchar() != '\n') instead of assigning it to a variable ch

If you do that, you won't be able to use the value returned by getchar() in the block of code in the while statement: the if statements, the else if statement, the switch statement.

why fflush(stdin) must be used?

fflush is meant to be used for output streams, not input streams. Hence, fflush(stdin) is undefined behavior. See http://en.cppreference.com/w/cpp/io/c/fflush1.

It seems that the intent of fflush(stdin) in your code is to read and discard everything until the end of the line. Write a function to do that.

void readAndDiscardRestOfLine(FILE* in)
{
   int c;
   while ( (c = fgetc(in)) != EOF && c != '\n');
}

and use

readAndDiscardRestOfLine(stdin);

instead of

fflush(stdin);

1POSIX extends the definition of fflush for input streams. However, it extends them only to seekable devices. The extension does not apply to stdin.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
-1

The problem occurs when you scan a character using scanf("%c", &repeat); Generally a user will enter the character 'y' or 'n' and then press enter. The character is read into the repeat variable, but the enter character (\n) remains in the buffer.

When you try to read the character again, in the loop, you will read this \n not a 'y' or 'n' as you expect.

The solution for this is to change the scanf statement to

scanf(" %c", &repeat);

The space character in fromt of the %c will read any whitespace character and only store the 'y' or 'n' in repeat. In this case, you will not need fflush.

fflush(stdin) is not a good practice. For more information you should look at Using fflush(stdin)

Community
  • 1
  • 1
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31