Trouble counting digits

This is a discussion on Trouble counting digits within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <fcntl.h> #include <sys/stat.h> int main(int argc, char** argv) { FILE *textfile; int ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Trouble counting digits

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    
    int main(int argc, char** argv) {
    
        FILE *textfile;
        int max_chars = 1000;
        int chars = 0;
        int digits = 0;
        char contents[max_chars];
        char c;
    
        textfile = fopen("file.txt", "r");
    
         if (textfile == NULL) {
            printf("Cannot open file.");
            exit(8);
        }
    
        while ((c = fgetc(textfile)) != EOF) {
            chars++;
        }
    
        fread(contents,max_chars,1,textfile);
    
        printf("Characters in text file: %d\n", chars);
    
        for (i = 0; i < chars; i++) {
        if ((contents[i] != '\0') && (contents[i] != '\n') && (contents[i] != '\t')) {
    
            if isdigit(contents[i]) {
                printf("digit: %c\n",contents[i]);
                digits++;
            }
    
           }
        }
    
        printf("Total digits: %d\n", digits);
    
        fclose(textfile);
    
    
        return (EXIT_SUCCESS);
    }
    file.txt:
    July 4th, 1776.
    Output:
    Characters in text file: 15
    digit: 4
    Total digits: 1
    I'm not understanding why it is only counting the 4 and not the other digits.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    Try rewind () before doing fread. You are at the EOF why do you think it will read anything.

    Tim S.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Thanks. I knew it had something to do with the fgetc() being at the EOF but I wasn't sure how to go back to the beginning.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok so you're only counting digits... why don't you just read it in character by character?
    You don't need arrays or buffers or anything fancy, in fact you don't even have to store the file's contents
    Just go at it with plain old fgetc() and isdigit()...

    Like this...
    Code:
    // open your file
    do 
     { if (isdigit(fgetc(MyFile)))
         digits++;
       characters++;
     } while (c > 0)
    // close your file
    // print your report
    One of the biggest mistakes in programming is the tendency to overthink things to the point of sheer lunacy...
    Work the problem out on paper... write down what you need to do step by step.
    Now cross out everything you listed that you do not absolutely have to do.
    There's your program...

    (hint: a good programmer could do this in about 12 lines)

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    I was going to storing them in an array because I was going to compare all the characters to see if they are the same.

    If I want to compare the characters, I would have to store them in an array, right?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes if you have additional things to do you may need arrays and such to store the file's contents.

    In that case, change gears... open your file, read the whole thing into a character buffer, close your file.
    Now you can work on the buffer and forget about the file... count characters, count digits, compare things, whatever you need to do.

    Always look for the easy way... It makes things a lot, ummm, easier...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    In the specific case of comparing all characters to see if they are the same, you don't need an array. Just read the first character, then as you read the remaining characters, check if they are equal to that first character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    In the specific case of comparing all characters to see if they are the same, you don't need an array. Just read the first character, then as you read the remaining characters, check if they are equal to that first character.
    Thanks laze...

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Quote Originally Posted by laserlight View Post
    In the specific case of comparing all characters to see if they are the same, you don't need an array. Just read the first character, then as you read the remaining characters, check if they are equal to that first character.
    I have to count the frequency of each character. Do I need an array then?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by never_lose
    I have to count the frequency of each character. Do I need an array then?
    Ah, then yes. In fact, if you map the value of each character read to an array index, you will find that this task is pretty easy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    What is an array index, and how would I map a character into one?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by never_lose
    What is an array index
    The number used to access an element of an array, e.g., 0 in contents[0].

    Quote Originally Posted by never_lose
    and how would I map a character into one?
    The value of a character is a number.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    The values of the characters are stored in contents[]. So by "mapping them to an array index" do you mean I have to store them in another array?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No... mapping refers to how you store them in the array you've already got.
    For example: your fread() call already maps them into the array in file order.
    Another form of mapping might be to have an array of 26 positins represeinting how many of each letter you read.

    However; I would caution you against constantly changing the specifications of your project. Not only will you turn it into a hack job, you'll eventually wear out people's patience. Your best bet is to sit down and decide what you want to do --write a specification-- then post it here so we all know what we're talking about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 09:39 PM
  2. Max Digits
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 05:28 AM
  3. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 07:32 PM
  4. loss of signifigant digits
    By Wjahvces in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2001, 11:15 PM
  5. Extracting individual digits from a short
    By G'n'R in forum C Programming
    Replies: 9
    Last Post: 08-30-2001, 10:30 AM

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