Counting Program

Hey guys,

I'm making a simple counting program that will count the amount of digits, white spaces and other things.

It's a textbook example but I just can't get the hang of it.
I've written it like it should be (I think),
but when I want to test it, it just functions like a notepad.

Thanks in advance!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int c, i, nwhite, nother;
	int ndigit[10];

	nwhite = nother = 0;
	for (i = 0; i < 10; i++)
		ndigit[i] = 0;

	while (( c = getchar()) != EOF){
		if 
			(c >= '0' && c <= '9')
				++ndigit[c-'0'];
			
		else if
			(c == ' ' || c == '\t' || c == '\n')
			++nwhite;
		
		else
			++nother;
		}

	printf("digits =");
	for (i = 0; i < 10; i++)
		printf(" %d", ndigit[i]);
	printf(", white space = %d, other = %d\n", nwhite, nother);
}
I don't really know about EOF,,
I once use them for competitive programming
that's it

I never able to output the program in the command prompt
I don't really know why..
but for this kind of input

I use ideone.com

http://ideone.com/QvsQLC

see your output here
That's the output I was looking for.
I have a C++ manual that my college gave us where exactly this code is used and the output is also given.
When I tried it myself, I just didn't get an output.

Strange..

But thanks for your help!
you probably want this
while (( c = getchar()) != '\n'){
nice!
Works!
Topic archived. No new replies allowed.