Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help With C Command Line Tool

Help With C Command Line Tool
Thread Tools
Baninated
Join Date: Jul 2006
Status: Offline
Reply With Quote
Apr 29, 2006, 08:48 PM
 
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
Reply With Quote
Apr 29, 2006, 09:06 PM
 
Your while loop needs some braces.
     
Baninated
Join Date: Jul 2006
Status: Offline
Reply With Quote
Apr 29, 2006, 09:22 PM
 
Got It Thanks
     
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Apr 29, 2006, 10:52 PM
 
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
Reply With Quote
Apr 30, 2006, 11:36 AM
 
Thnx That helped alot. Now it works.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:47 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2