Thread: C gets() help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    C gets() help

    Okay, so in this program I have to read a string of characters then write that into a file. The max characters it can be is 20. Anyway, the problem I have is that when I use scanf() you can't enter spaces in the name. So when I put in gets() it just skips the user input and goes to the next step, what have i done wrong? Here is the slice of code from my function:

    product_id is a global variable that's why it isnt listed, so is offset. Yes I know its bad to do but our teacher made us do it. I don't think this is the problem though....
    Code:
    void update(FILE *fp) {
    	int qty;
    	float cost;
    	char name[21];
    	printf("Enter old product id:");
    	scanf("%d", &product_id);
    	if (search(fp, &offset)) {
    		printf("ERROR\n");
    		}
    	else {
    		fseek(fp, offset, SEEK_SET);
    		printf("Product id Found Successfully\n");
    		printf("\nEnter New Product Id That is Unique:");
    		scanf("%d", &product_id);
    		printf("Enter New Product Name(20 character max):");
    		gets ( name );
    		printf("Enter New Product Quantity:");
    		scanf("%d", &qty);
    		printf("Enter New Product Price:");
    		scanf("%f", &cost);
    		fprintf(fp, "%4d %20.20s %4d %10.2f", product_id, name, qty, cost);
    
    		} /* end else */

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not use gets: http://cpwiki.sourceforge.net/Gets
    And add getchar(); below your scanf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    so i would replace
    Code:
    gets(name);
    with
    Code:
    scanf("%s", &name);
           getchar();
    What does that do?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you should not. You should use fgets instead of gets.
    Add getchar under your
    scanf("%d", &product_id);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    Well, can anyone help me use gets(), this isn't an actual program for a business, just a hw assignment which calls for gets()

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you don't need gets. You should never, ever use gets.
    Read the link properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    I don't understand the link

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is difficult to understand in the link?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    The last bit of code it says to replace gets, I dont understand how to implement my code like that

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    I'm just going to tell the user to use _ for spaces and use scanf, thanks for your help elysia

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Big fat easy example:
    Code:
    #include <string.h>
    
    char * safegets(char * buffer, size_t count)
    {
        char * result = buffer;
        char * tail;
        if(buffer == NULL || count <= 0) 
            result = NULL;
        else if(count == 1)
            *result = '\0';
        else if(fgets(buffer, count, stdin) != NULL) {
            if( (tail = strchr(buffer, '\n')) != NULL)
                *tail = '\0';
        }
        return result;
    }
    
    int main()
    {
        char buf[100];
        printf("Enter something: ");
        safegets(buf, sizeof(buf));
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed