Problem with Simple C Program

This is a discussion on Problem with Simple C Program within the C Programming forums, part of the General Programming Boards category; Originally Posted by djtomr941 This is what the code looks like now and it works, any other suggestions as far ...

  1. #16
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by djtomr941
    This is what the code looks like now and it works, any other suggestions as far as good coding practice for C?
    Yes, better to stay portable:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main (void)
    {
       int let = 0;
       int dig = 0;
       int other = 0;
    
       int c;
    
       while ((c = getchar ()) != '\n' && c != EOF)
       {
          if (isalpha (c))
          {
             ++let;
          }
          else if (isdigit (c))
          {
             ++dig;
          }
          else
          {
             ++other;
          }
       }
    
       printf ("%d letters, %d digits, %d others\n", let, dig, other);
    
       return 0;
    }
    Last edited by Emmanuel Delaha; 12-21-2004 at 05:59 AM.
    Emmanuel Delahaye

    "C is a sharp tool"

  2. #17
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Emmanuel Delaha
    Yes, better to stay portable:
    But what makes it more portable? Is it not using:
    Code:
    isspace()
    more portable because it only counts \n instead of \r\n?

    Code:
    while ((c = getchar ()) != '\n' && c != EOF)
    Is the EOF really that necessary? Because when the user hits enter, there has to be a \n, and thus that's all we need to quit the while loop.

  3. #18
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Kleid-0
    But what makes it more portable? Is it not using:
    Code:
    isspace()
    more portable because it only counts \n instead of \r\n?
    There is no \r\n on a text stream (stdin, text file). The End Of Line character (EOL) is '\n', whatever the platform.
    Code:
    while ((c = getchar ()) != '\n' && c != EOF)
    Is the EOF really that necessary? Because when the user hits enter, there has to be a \n, and thus that's all we need to quit the while loop.
    Yes, because stdin can be redirected from a text file.
    Emmanuel Delahaye

    "C is a sharp tool"

  4. #19
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    But in this case we wouldn't need to check for EOF because we're not using a text file for input, technically speaking?

  5. #20
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Kleid-0
    But in this case we wouldn't need to check for EOF because we're not using a text file for input, technically speaking?
    Who knows ? It doesn't harm to leave the 'EOF' test and it makes the code more portable (ready for a file text entry)

    $ myprog < myfile.txt

    .
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #21
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by djtomr941
    What is the difference between these kinds of brackets ( ) and these kind { }.
    The meaning (semantic) is different. The details belong to your C-book.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #22
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Emmanuel Delaha
    $ myprog < myfile.txt
    Holy cow that is so cool!
    Code:
    Shiva:/home/*****/Programming/Focus# ./a.out < input.txt
    34 --> 34
    I really like that! I thought you could only do output with >. Thank you :)

  8. #23
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    < isn't technically output (in fact, it's quite the opposite), but yes, it is cool
    If you understand what you're doing, you're not learning anything.

Page 2 of 2 FirstFirst 12
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, 02:30 PM
  2. Replies: 1
    Last Post: 12-30-2007, 09: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, 04:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21