 |
 |
Help With C Command Line Tool
|
 |
|
 |
|
Baninated
Join Date: Jul 2006
Status:
Offline
|
|
I am learning C out of a book right. Problem the book while good is from 1988. Now in OS X only some things will work. But, these character counteing ones wont. This is the latest one
Code
#include <stdio.h>
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[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
I compile it, fine. I run the a.out file. then nothing happens. Then I start typeing hopping for a out put still nothing. Help please.
|
|
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: Oct 2005
Location: Houston, TX
Status:
Online
|
|
Your while loop needs some braces.
|
|
|
| |
|
|
|
 |
|
 |
|
Baninated
Join Date: Jul 2006
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2001
Location: Seattle
Status:
Offline
|
|
You may also want to change 't' to '\t' so that you will count tabs as whitespace rather than t's. 
|
|
|
| |
|
|
|
 |
|
 |
|
Baninated
Join Date: Jul 2006
Status:
Offline
|
|
Thnx That helped alot. Now it works.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |