Thread: automatic carriage return?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176

    automatic carriage return?

    I'm using DJGPP C compiler. Trying sample code. I don't know why this code when run will not let me input the second string to compare. It just seems to automatically hit enter on it and go to asking for input on # of characters to compare.

    Code:
    include <stdio.h>
    #include <string.h>
    
    main()
    {
    	char	string1[20];
    	char	string2[20];
    	int	length;
    
    	printf("\nEnter first string to compare   :");
    	scanf("%s",string1);
    	
    	printf("\nEnter second string to compare  :");
    	scanf("%s",string2);
    
    	printf("\nEnter the number of characters to compare  :");
    	scanf("%d",&length);
    
    	if(!strncmp(string1,string2,length))
    	  printf("The first %d characters of String 1 equals String 2\n",length);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    ok, here's an example of it running:

    Code:
    Microsoft Windows 2000 [Version 5.00.2195]
    (C) Copyright 1985-2000 Microsoft Corp.
    
    C:\DJGPP\Mystuff>string
    Cannot load VDM IPX/SPX support
    
    Enter first string to compare   :Hello world!
    
    Enter second string to compare  :
    Enter the number of characters to compare  :8
    
    C:\DJGPP\Mystuff>

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    To know why study this

    And call this function after your first call to scanf()

    Code:
    void clear_buffer(void)
    {
         int ch;
    
        while( (ch = getchar() ) != '\n' && ch != EOF );
    }
    ssharish

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    LOL! ok book was written around/before 94. No Win2k back then.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Thanks for the help! This problem seem to not be present on my Windows 95 machine. Must be an NT/2K/XP OS problem.

    And after trying it it appears I will have to call it after every scanf read/call/whatever.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here's a tip: avoid scanf for user input. Instead prefer fgets possibly followed by sscanf or newline trimming of a string.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM