Thread: Program using strictly pointers to count string input

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    17

    Talking Program using strictly pointers to count string input

    Hi,
    Im trying to code a program that counts variables in a string input
    The program must use only pointers.. I have a logic error in here somewhere that I can't spot. 0 syntax.

    for some reason print f's print out 0,*fluctuating huge number, 0

    my brain hurts. can anyone help me?

    Thank you,
    This forum helps new coders so much!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void){
    
    
        char line[50];
        char *stringletters = line;
        int one=0;
        int two=0;
        int three=0;
        int *o = &three;
        int *e = &one;
        int *i = &two;
    
    
        printf("Enter a line of string:\n");
        fflush(stdin);
        scanf(" %c " ,&stringletters[50]);
    
    
        printf("p1");
        while(*stringletters != '\0')
        {
            if(*stringletters == 'e'){
            one++;
            }
            if(*stringletters == 'i'){
                    two++;
                    }
            if(*stringletters == 'o'){
                    three++;
                    }}
    
    
    
    
        printf("Vowels E or e: %d \n" ,*e);
        printf("Vowels I or i: %d \n" ,*i);
        printf("Vowels O or o: %d \n" ,*o);
    
    
        system("pause");
        return 0;
    
    
    }





    Quote Originally Posted by Hodor View Post
    I suspect that your problem is on line 21
    I changed it to fgets() hopefully thats what you meant and not my pointer.
    Last edited by Glassjaw; 11-29-2015 at 11:53 PM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I suspect that your problem is on line 21

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you use a for loop instead of a while loop. The loop can be of this form:
    Code:
    char *current_letter;
    for (current_letter = line; *current_letter != '\0'; ++current_letter)
    {
        /* ... */
    }
    Frankly, the o, e and i pointers to int are plain silly. You should use the int variables directly (and indeed you did so within the loop body) as the requirement to "use only pointers" surely does not apply to them. However, the int variables should be renamed to say, o_count, e_count and i_count, respectively.

    Note that fflush(stdin) results in undefined behaviour as fflush is only defined for output streams and update streams for which the last operation was output. Anyway, you don't need it here.

    Instead of your incorrect use of scanf, you may find it better to use fgets, but keep in mind that fgets will store the newline from entering the line if there is space for it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    17
    Thanks for your reply

    I thought it was pretty useless to use O,E,I pointers as well. I only swapped them in the loop body to try and make them work thinking the pointers may have been my issue.

    are you saying I should use current_letter to point my array pointer
    to the correct value ... example *stringletters[current_letter]
    or just use it to guide the for loop?

    I made the swaps based on your comments but it still isn't working properly... I'm pretty sure it has to do with *stringletters and *current_letter..

    *Current_letter doesn't point to anything so I think I misunderstood what you mean...

    Heres what I have now

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void){
    
    
        char line[50];
        char *stringletters = line;
        char *current_letter;
        int e_count;
        int i_count;
        int o_count;
    
    
        printf("Enter characters:\n");
        fgets(stringletters, 50, stdin);
    
    
        for (current_letter = line; *current_letter != '\0'; ++current_letter)
        {
            if(*stringletters == 'e'){
            e_count++;
            }
            if(*stringletters == 'i'){
                    i_count++;
                    }
            if(*stringletters == 'o'){
                    o_count++;
        }}
    
    
    
    
        printf("Vowels E or e: %d \n" ,e_count);
        printf("Vowels I or i: %d \n" ,i_count);
        printf("Vowels O or o: %d \n" ,o_count);
    
    
        system("pause");
        return 0;
    
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to initialise e_count, i_count, and o_count to 0.

    You should get rid of stringletters: I basically renamed it to current_letter.

    You need to indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    17
    Alright, That makes sense!
    Ill indent too.
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to count each distinct word input
    By nvangogh in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2015, 09:50 AM
  2. Question over count (string w/ pointers)
    By Roadrunner2015 in forum C Programming
    Replies: 4
    Last Post: 02-01-2014, 09:18 PM
  3. Replies: 2
    Last Post: 07-28-2010, 02:07 PM
  4. Replies: 7
    Last Post: 01-30-2006, 02:39 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM