Thread: Problem with Simple C Program

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Problem with Simple C Program

    I need help here.

    This is my simple C Code

    main( ) {
    int let, dig, other, c;
    let = dig = other = 0;
    while ((c=getchar( )) != '\0')
    if( ('A'<=c && c<='Z') || ('a'<=c && c<='z') )
    ++let;
    else if( '0'<=c && c<='9' ) ++dig;
    else ++other;
    printf("%d letters, %d digits, %d others\n", let, dig, other);
    }

    The problem I am having is ocne it is compiled, I execute it, and then I type in something and hit enter.

    I know it goes through the loop but for some reason the printf never runs and it just keeps prompting me for input.

    I am using the gcc gnu compiler.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Researched

    Basically what I found out is it is not exiting the while loop. Even as I hit enter it wont exit. I tried everything I know for end of file such as ctrl d , z eetc and it wont exit. I'm a newbie and just want to know what I might be forgetting to do??

    thanks

  3. #3
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    on 'while ((c=getchar( )) != '\0')'
    change '\0' to '\n' and try again.
    -- Add Your Signature Here --

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://cboard.cprogramming.com/showthread.php?t=25765

    1. You need
    #include <stdio.h>

    2. don't just say main()
    Do
    int main()

    and finish with
    return 0;

    3. while ((c=getchar( )) != '\0')
    This should be
    while ((c=getchar( )) != EOF )

    Pressing ctrl-D will then get you out of the loop

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Thanks

    Ok got it to work. In fact both methods worked. The \n took enter and the Ctrl Z worked with the GNU gcc I was using which is wingw.org's version.
    Last edited by djtomr941; 11-19-2004 at 04:36 AM.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    My Code

    This is what the code looks like now and it works, any other suggestions as far as good coding practice for C?

    Code:
    #include <stdio.h>
    
    int main( ) {
              int let, dig, other, space, c;
              let = dig = other = space = 0;
              printf("Please type in your letters and we will count the characters?\n");
    	  while ((c=getchar( )) != '\n' )
                      if( ('A'<=c && c<='Z') || ('a'<=c &&  c<='z') )  ++let;
                      else if( '0'<=c && c<='9' ) ++dig;
                      else if( c == ' ' ) ++space; 
                      else  ++other;
              printf("%d letters, %d digits, %d spaces, %d others\n", let, dig, space, other);
              return 0;
      }

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, you can replace:
    Code:
    if( ('A'<=c && c<='Z') || ('a'<=c &&  c<='z') )
    if( '0'<=c && c<='9' )
    if( c == ' ' )
    with
    Code:
    if(isalpha(c))
    if(isdigit(c))
    if(isspace(c))
    It's just easier to see what you're doing at a glance that way. Just don't forget to #include <ctype.h>
    If you understand what you're doing, you're not learning anything.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > any other suggestions as far as good coding practice for C?
    Always use braces to surround groups of statements even when they appear to be optional.

    The problem is, when they become compusory, the code will more often than not still compile, only it will do something different to what you expect. Most code will grow to having more than one statement per block.

    For example, attempting to add
    printf( "Letter = %c\n", c );
    inside your while loop would cause it to fail miserably. Sure it would print out every letter, but the if/else group would only be executed ONCE (when c == '\n') and your final totals would reflect that!

    Code:
    	  while ((c=getchar( )) != '\n' ) {
                      /* printf( "Letter = %c\n", c ); */
                      if( ('A'<=c && c<='Z') || ('a'<=c &&  c<='z') ) {
                          ++let;
                      } else if( '0'<=c && c<='9' ) ++dig;
                      else if( c == ' ' ) ++space; 
                      else  ++other;
    	  }
    The while loop pretty much needs braces simply because of the length of code which is within the loop. The individual cases of say ++let are short enough to get away without braces, though personally I would not think it unusual to have them there.
    The loop will not change if you uncomment the printf() statement.

    Also, use more meaningful variable names. let instead of letter may be obvious enough in a small program like this, but in a larger program it can become very tiresome to look at and hard to understand. This is especially true when your idea of a good abbreviation doesn't match that of the readers.
    The most common exception to the rule is using say i and j as for loop control variables - for ( i = 0 ; i < 10 ; i++ )


    > if(isspace(c))
    Except isspace() is true for more characters than just ' '

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Thanks

    Thanks everyone.

    I'm not familiar with ctype.h so I appeciate the heads up on that.

    Also Salem.

    What is the difference between these kinds of brackets ( ) and these kind { }.

    Thanks

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    () for arithmetic and logical expressions
    {} for statements

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I don't like this form:
    Code:
    int let, dig, other, space, c;
    let = dig = other = space = 0;
    Whenever I see it, deep down inside it feels like evil is taking over the world.
    I prefer single style!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
    	int let = 0;		// Letters
    	int dig = 0;		// Numbers
    	int space = 0;		// Space!
    	int other = 0;		// Some other character
    	int c;
    
    	printf("Type anything you want:\n");
    	while ((c=getchar()) != '\n') {
    		if(isalpha(c)) ++let;
    		else if(isdigit(c)) ++dig;
    		else if(isspace(c)) ++space;
    		else ++other;
    	}
    	
    	printf("\nSTATS:\n%d letter(s)\n%d digit(s)\n%d space(s)\n%d other(s)\n", let, dig, space, other);
    	return 0;
    }
    I really liked the ctype.h functions :)

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I prefer single style!
    Same here, but it's just a matter of personal style like the difference between
    Code:
    int factorial(int x){
       ...
    }
    and
    Code:
    int factorial(int x)
    {
       ...
    }

  13. #13
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Personal style!? I think not! There's a conspiracy here! And I think people who don't use single style are worshippers of the devil!

  14. #14
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I've seen something like this before
    Code:
    int main(void){
    int main(void){
            int x=1;
            printf("%d",x);
            {
            int y=1;
            printf("%d\n",y);
            }
            return 0;
    }
    Well a dumned down version of what they had. They just put braces to make it the beginning of a block so they could declare new variables in scope. I though that was terrible.

  15. #15
    ---
    Join Date
    May 2004
    Posts
    1,379
    I think you guys are forgetting that this is yet ANOTHER month old thread that Kleid-0 has bumped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM