Thread: A function error, Please help

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    A function error, Please help

    my getstring function is messing up if it's used after the program accepts an integer value


    the program does this :

    enter name 1 : mike
    enter name 2 : john
    enter age 1 : 7
    enter name 3 : //after age 1 is entered name 3 is skipped and the program asks for age 2
    enter age 2 : 8




    Code:
    #include <stdio.h>
    
    
    void getstring(char str[]){
         
         int state = 1;
         int x = 0;
         char ch;
         
         while( (ch=getchar()) != '\n' )
         {
                if( ch == ' ' || ch == '\t' )
                {
                    if(state == 0)
                    {
                             str[x++]= ch;
                             state = 1;
                    }
                }
                else
                {
                    str[x++]= ch;
                    state = 0;
                }
         }
         str[x] = '\0';
         return ;
    }
    
    
    
    
    int main(void){
        
        int x,y;
        char name[50];
        char name2[50];
        char name3[50];
        
        printf("Enter name 1: ");
        getstring(name);
        printf("Enter name 2: ");
        getstring(name2);
        printf("\nEnter age 1: ");
        scanf("%d",&x);
        printf("\nEnter name 3: ");
        getstring(name3);
        printf("\nEnter age  2: ");
        scanf("%d",&y);
        getchar();
        return 0;
    }
    positive criticism as well as solutions would be much appreciated

    Also, thanks to Whiteflags and Grumpy for helping me with my last error.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When you used scanf() to retrieve the number the end of line character was left in the input buffer, which is causing your problems. You need to clear the input buffer before you try to get your character input.

    Is there a reason you're not using the fgets() function to retrieve data instead of making your own?

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    i created my own because trying to input data into a structure using fgets was problematic

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How was using fgets problematic?
    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

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    There's this return value called EOF that you're supposed to check.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int skipblank(FILE *iop)
    {
        int c;
    
        while ((c = getc(iop)) != EOF && isblank(c))
            ;
        return c;
    }
    
    int getline(char *s, size_t n, FILE *iop)
    {
        char *endp = s + n - 1;
        int c;
    
        for (c = skipblank(iop); c != EOF && c != '\n'; c = getc(iop))
            if (s < endp)
                *s++ = c;
        *s = '\0';
        return c;
    }
    If you're going to strip out the whitespace from the end as well, you'd probably benefit from making your input function as simple as possible then simply making a copy of the string without the whitespace and stuff. It just seems like a more straightforward process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2012, 02:37 PM
  2. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  3. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  4. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  5. function calling within another function error
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 01:40 AM