0

I am working through K&R's exercises right now and I am at the one where you count the number of blanks, spaces, and tabs using the C language. I have built the following code:

#include <stdio.h>
#include <stdlib.h>

/*Write a program that counts blanks, tabs, and newlines*/

int main()
{
    int c, numblanks, numtabs, numnewlines;

    numblanks = 0;
    numtabs = 0;
    numnewlines = 0;

    printf("Enter some text and press \"Enter\"\n");

    while ((c = getchar()) != EOF) {
        if (c == ' ')
            ++numblanks;
        if (c == '\t')
            ++numtabs;
        if (c == '\n')
            ++numnewlines;
    }

    printf("The total number of blanks is %i\n", numblanks);
    printf("The total number of tabs is %i\n", numtabs);
    printf("The total number of new lines is %i\n", numnewlines);
}

I am using Codeblocks and the built-in GCC compiler that is installed with it on a Windows 10 OS. When I run the program, I type some text into the program window that pops up and press "Enter" and nothing happens. I am not sure why. I was wondering if someone could help me get another pair of eyes on my code to see if there is something I am missing. Here is an image of what happens in the program when I run it:

Program window with typed text, but no reaction

  • Ohhhh, that's what I was looking for, just did not entirely know how to say it properly (ie. noob here...haha). Thanks bud! Ctrl + Z did the trick! – Derek ZhongXuan Kwok Nov 28 '17 at 05:23
  • 1
    Also be aware of [CTRL+Z does not generate EOF in Windows 10](http://proc-x.com/2017/06/ctrlz-does-not-generate-eof-in-windows-10/) unless you have the right box checked. – David C. Rankin Nov 28 '17 at 06:13

1 Answers1

0

I have not tried your solution but sometimes this can happen when the program is finished, the terminal closes. Try to put a getchar at the end of your program.

printf("Enter key to exit");
getchar();

This can help after string input (but sometimes require creating loop with getchar() and checking the result of this function)

VolAnd
  • 6,367
  • 3
  • 25
  • 43
perz
  • 43
  • 1
  • 5