A function error, Please help

This is a discussion on A function error, Please help within the C Programming forums, part of the General Programming Boards category; my getstring function is messing up if it's used after the program accepts an integer value the program does this ...

  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
    2,919
    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
    22,308
    How was using fgets problematic?
    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

  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, 03: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, 08:05 PM
  3. Replies: 8
    Last Post: 07-08-2011, 02:16 PM
  4. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 12:53 PM
  5. function calling within another function error
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 01:40 AM

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