Thread: gets() function doesn't execute

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

    gets() function doesn't execute

    Hello everyone
    In the following code gets () function does not work and the program comes out without executing that.
    OF course if I write the gets () function in the first line
    It works. Why? Something wrongs with buffer
    Code:
    #include<stdint.h>
    #include<string.h>
    
    
    
    int main(void){
              
    				char names[4][10];
    				char city[10];
    				char revTemp;
    				int i=0;
    			
    				
    				puts("Enter four names :");
    				for(i=0;i<4;i++)
    					scanf(" %s" , names[i]);
    				printf("The orginal names are : %s %s\n" , &names[1][0], &names[2][0]);//before   exchane  names
    				for(i=0;i<=9;i++){
    									revTemp=names[1][i];
    									names[1][i]=names[2][i];
    									names[2][i]=revTemp;
    				}
    				printf("The names after the exchange :%s  %s \n" , &names[1][0] , &names[2][0]);//after  exchange the names
    				puts("Enter the city");
    				gets(city);//This line doesn't execute ERRORE
    	
      return 0;
    }
    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Do not use gets() -> SourceForge.net: Gets - cpwiki

    2. Do not mix input methods (like scanf with gets-soon-to-be-fgets)

    scanf typically leaves the \n at the end of a line of input behind (still in the input buffer).
    This is a problem, since a \n is exactly what gets/fgets is expecting to terminate its input.

    Ideally, you should use ONE input method (fgets) to read all the input, then use sscanf (or anything else appropriate) to parse the buffer in memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Thanks a lot salem
    How I should use fgets() in this program
    I try to use this but the program crashed.
    Code:
    fgets(city,3);
    puts(city);

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're missing a parameter. If you haven't already, read up on the "fgets()" function to understand how to use it properly.
    It also wouldn't hurt to create a small test program to play around with "fgets()" before implementing it in your current project.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Thanks guys
    Last edited by mohsen; 07-12-2013 at 02:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to execute a top priority function?
    By zenniz in forum C Programming
    Replies: 2
    Last Post: 03-14-2013, 06:25 AM
  2. cout doesn't execute
    By Cjof in forum C++ Programming
    Replies: 6
    Last Post: 03-10-2013, 05:55 PM
  3. scanf() doesn't execute in its 2nd time use
    By dnayak382 in forum C Programming
    Replies: 8
    Last Post: 08-16-2011, 09:56 AM
  4. Replies: 4
    Last Post: 04-26-2004, 06:31 PM
  5. Code before a line that Crashes doesn't execute
    By Diamonds in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2003, 09:03 PM