Like Tree1Likes
  • 1 Post By anduril462

scanf lock the gets?

This is a discussion on scanf lock the gets? within the C Programming forums, part of the General Programming Boards category; if i use scanf() the program do not work by gets it"s work Code: #include <stdio.h> #include <stdlib.h> int main() ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33

    scanf lock the gets?

    if i use scanf() the program do not work by gets it"s work
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     int main()
     {
        char *ptr;
       char a[6]="mohsn";
    gets(a);
    puts(a);
    scanf("%s",ptr);        //if i use gets(ptr);program will be work  
    puts(ptr);
    ptr=a;
    gets(ptr);    //this gets do not get the new value ?
    puts(ptr);
     return (1);
     }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    First, read these links:
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    Then, you need to explain exactly how scanf isn't working. What input do you give? What output do you get? What output do you think you should get?

    In your program ptr doesn't point to valid memory, so trying to store a string in ptr, whether with scanf or gets, will result in undefined behavior, meaning anything can happen. Don't do it. Make it point to valid memory first, by malloc'ing some memory or pointing it at a valid char array like a. You should put a length limiter in scanf, like "%5s" to avoid overflowing a.

    I'm not sure what you mean by "this gets do not get the new value ?", please be more clear. And again, don't use gets.

    Also, you should return 0 from main to indicate successful completion of your program. Non-zero values typically signify some error.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Quote Originally Posted by anduril462 View Post
    First, read these links:

    Then, you need to explain exactly how scanf isn't working. What input do you give? What output do you get? What output do you think you should get?



    I'm not sure what you mean by "this gets do not get the new value ?", please be more clear. And again, don't use gets.

    Also, you should return 0 from main to indicate successful completion of your program. Non-zero values typically signify some error.
    hi
    if use scanf & gets in both sutation it s worked but the last gets() do not take a new string
    the last puts() show the older ptr (ptr=a). give three different string to the program but the last sting will not show on the screen

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    What's with the giant, bold font. Please just use the regular font from now on.

    I think I understand your problem now. When you use scanf with %s to read input, it stops at the first white space, which includes a new line. After you type your string and hit enter, scanf takes the string out of the input buffer but leaves the new line. Then the next gets looks at the input buffer and sees a new line. It thinks you pressed enter without typing anything. scanf is often not the best option for user input, but gets is worse. Never, ever use or ask or think about gets again. It is a dirty, nasty, horrible function that never should have been invented. Read the links (the red text -- click it) from my first post. It will explain how to use fgets to read a line of input. That's your best bet in this case.
    mohsen likes this.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Really thank you anduril.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    I'm not sure what you mean by "this gets do not get the new value ?", please be more clear. And again, don't use gets.
    Type "bob", hit enter.

    scanf eats everything up to the white space, leaving it there. gets takes the whitespace, because there isn't anything else there. So all that gets does is immediately see a newline, and stop.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lock and unlock
    By munna_dude in forum C Programming
    Replies: 2
    Last Post: 05-18-2007, 05:52 AM
  2. lock
    By siavoshkc in forum C# Programming
    Replies: 5
    Last Post: 10-13-2006, 08:46 AM
  3. What is the lock function?
    By siavoshkc in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2006, 05:50 PM
  4. computer lock down
    By scott27349 in forum Tech Board
    Replies: 13
    Last Post: 07-03-2003, 10:48 AM
  5. LPDIRECTDRAWSURFACE7->Lock()
    By Magos in forum Windows Programming
    Replies: 5
    Last Post: 03-18-2003, 05:53 PM

Tags for this Thread


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