Reputation: 3
I'm following along with a book on C to learn it and I am writing all the code in the book to follow along, the latest one to do with arrays is supposed to say how many white spaces, tabs etc. There are but when I execute it, nothing shows up, it's just blank, as in I can type something then press enter and nothing happens, is it supposed to tell me how many of each thing there is?
I am too new to understand if this program is actually supposed to output anything so I thought I would post it on here and get an opinion, it compiles and runs fine, no errors, but the book sort of refers to it outputting stuff, but nothing happens when I'm running it and typing stuff in, can just keep typing stuff forever.
Here is the code
#include <stdio.h>
int main()
{
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 == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[1]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
Upvotes: 0
Views: 285
Reputation: 7912
The application takes in input some values and then counts the number of digits (0-9) and the white spaces. The key combination to interrupt the cycle is not ENTER but EOF which in Linux is CRTL-D and in WINDOWS is CTRL-Z.
Then, in you application there is a bug :
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[1]);
In order to show the number of digits this should be :
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
Unfortunately, getting interactive input is quite problematic when using scanf(), getchar(), fgets(), etc. That's why most people usually write their own custom functions, often getting a whole line from stdin and then parsing it according to their needs. However, if you want to use ENTER to stop the cycle you can modify the code as follows, but you will lose the possibility to count the number of new lines in the input.
#include <stdio.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()) != '\n')
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
return 0;
}
This should work as you expected. However, you should consider to write a better input function, there are several interesting solution online.
EDIT
The main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only int. Some compilers accept void main(), but that is non-standard and shouldn't be used.
Check some examples here : http://www.parashift.com/c++-faq-lite/main-returns-int.html
Upvotes: 1
Reputation: 1263
Change
printf(" %d", ndigit[1]);
to
printf(" %d", ndigit[i]);
Press ctrl+d to give EOF after entering value
Input:
2 4 6 8 //3 white space + 1 new line = 4 white space
[ctrl + d]
Output:
digits = 0 0 1 0 1 0 1 0 1 0, white space = 4, other =0
Upvotes: 0
Reputation: 47784
You can provide a EOF using following
WINDOWS:
Press F6 then ENTER
or
Ctrl+Z
LINUX:
Ctrl+D
Upvotes: 0