Like Tree2Likes
  • 2 Post By AndiPersti

Need help ASAP with a code

This is a discussion on Need help ASAP with a code within the C Programming forums, part of the General Programming Boards category; I haven't gone into much details yet because I am just trying to get this code to work first. The ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Need help ASAP with a code

    I haven't gone into much details yet because I am just trying to get this code to work first. The program runs with zero errors but when I run the program the printf statement at the end prints numbers like -858993451 when instead I want to show the number of characters for a, then b, then c. What is the problem? I assume it is something basic with my code and how it increments.

    Code:
    void main()
    {
        FILE *inPtr;
        char alpha[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
                        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        int numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int letter[ALPHABET];
        int digit[NUM];
    
    
        int ch, i, x, y, z;
        y = 0;
    
    
        inPtr = fopen("C:\\Temp\\indata.txt", "r");
        if(!inPtr)
        {
            printf("\nError open input file\n");
            exit(13);
        }
    
    
        while(1)
        {
            ch = fgetc(inPtr);
    
    
            if(ch == EOF)
                break;
            else if(isalpha(ch))
            {
                for(i = 0; i < ALPHABET; i++)
                {
                    ch = tolower(ch);
    
    
                    if(ch == alpha[i])
                        letter[i]++;
                }
            }
            else if(isdigit(ch))
            {
                for(x = 0; x < NUM; x++)
                {
                    if(ch == numbers[x])
                        digit[x]++;
                }
            }
            else if(ispunct(ch))
                y++;
        }
    
    
        for(z = 0; z < ALPHABET; z++)
        {
            printf("%c's: %d\n", alpha[z], letter[z]);
        }
        for(z = 0; z < NUM; z++)
        {
            printf("\n%d's: %d", numbers[z], digit[z]);
        }
        printf("\nSpecial Characters found: %d\n", y);
    
    
        fclose(inPtr);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    You need to initialise
    Code:
        int letter[ALPHABET] = { 0 };
        int digit[NUM] = { 0 };
    And main returns an int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Yeah I know I can use int main() and not void main() but I didn't really feel like it. It also appears that my program isn't incrementing the digits. All I get is a 0 at the printf part for digit[z] Thanks for the help with the initializing...it's always something simple that makes errors...
    Last edited by CALiiGeDDon; 10-31-2012 at 12:06 AM.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Found a way to get the digit counter to work. Not sure if it is a good way but in the array declaration for numbers[] i changed each number from 0, 1, 2... to '0', '1', '2'... and at the printf I used z instead of numbers[z]. Is this a clean way to do this? It works perfectly fine.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've gone to a lot of work to do this - with experience, you'll get this program down to about 10 lines of code, flat.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by CALiiGeDDon View Post
    The program runs with zero errors
    I guess your compiler needs a service:
    Code:
    $ gcc -ggdb3 -Wall -Wextra -o test test.c
    test.c:1:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    test.c: In function ‘main’:
    test.c:3:5: error: unknown type name ‘FILE’
    test.c:7:16: error: ‘ALPHABET’ undeclared (first use in this function)
    test.c:7:16: note: each undeclared identifier is reported only once for each function it appears in
    test.c:8:15: error: ‘NUM’ undeclared (first use in this function)
    test.c:15:5: warning: implicit declaration of function ‘fopen’ [-Wimplicit-function-declaration]
    test.c:15:11: warning: assignment makes pointer from integer without a cast [enabled by default]
    test.c:18:9: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    test.c:18:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    test.c:19:9: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
    test.c:19:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    test.c:25:9: warning: implicit declaration of function ‘fgetc’ [-Wimplicit-function-declaration]
    test.c:28:18: error: ‘EOF’ undeclared (first use in this function)
    test.c:30:9: warning: implicit declaration of function ‘isalpha’ [-Wimplicit-function-declaration]
    test.c:34:17: warning: implicit declaration of function ‘tolower’ [-Wimplicit-function-declaration]
    test.c:41:9: warning: implicit declaration of function ‘isdigit’ [-Wimplicit-function-declaration]
    test.c:49:9: warning: implicit declaration of function ‘ispunct’ [-Wimplicit-function-declaration]
    test.c:56:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    test.c:60:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    test.c:62:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    test.c:65:5: warning: implicit declaration of function ‘fclose’ [-Wimplicit-function-declaration]
    test.c:8:9: warning: unused variable ‘digit’ [-Wunused-variable]
    test.c:7:9: warning: unused variable ‘letter’ [-Wunused-variable]
    Quote Originally Posted by CALiiGeDDon View Post
    Yeah I know I can use int main() and not void main() but I didn't really feel like it.
    Do you really think such attitude will get you any help here?

    Bye, Andreas
    Salem and dmh2000 like this.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Quote Originally Posted by AndiPersti View Post
    Do you really think such attitude will get you any help here?
    Yeah, I checked out at that point.
    No point on wasting effort on people who don't listen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CALiiGeDDon View Post
    Yeah I know I can use int main() and not void main() but I didn't really feel like it. It also appears that my program isn't incrementing the digits. All I get is a 0 at the printf part for digit[z] Thanks for the help with the initializing...it's always something simple that makes errors...
    It may be extra work - but this is THE place to make that effort, and learn and practice, to code in C, correctly.

    Make that effort! It will be worth it.

    Does the program have an include <stdio.h> ? I thought it was just an oversight, but when I saw the list of warnings, I became concerned.

    You're trying to count up the number of letters, digits, and special characters.

    Define special character:

    Can you use ctype.h for this? It has functions just for this type of work.

    And don't be lazy or thin-skinned - be anxious to learn to do C, the right way. These guys (and gals), really know their C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help asap!
    By tayexdrums in forum C Programming
    Replies: 17
    Last Post: 09-22-2011, 11:26 PM
  2. Need help asap!!! Please!!!
    By calibean in forum C Programming
    Replies: 11
    Last Post: 07-09-2011, 04:39 PM
  3. Someone Please Help! ASAP!!
    By CB44 in forum C++ Programming
    Replies: 8
    Last Post: 01-17-2011, 12:13 AM
  4. Someone Please Help! ASAP!!
    By CB44 in forum C Programming
    Replies: 1
    Last Post: 01-16-2011, 04:41 PM
  5. Need help ASAP if you can
    By Wiggin in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2001, 03:01 PM

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