Thread: Weird

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    Exclamation Weird

    hie...i have a easy..but don't seem to be working code

    Code:
    while (check == 0)
    	{
    		printf("\nEnter a character:\n");
    		scanf("%c",&val);
    		curr->next=initLLNode(val);
    		curr=curr->next;
    		printf("Do you still want to enter more characters\n1. YES\n2. NO\nChoose either 1 or 2:");
    		scanf("%d",&chose);
    		num=num+1;
    		if (chose==2)
    		{
    			check = 1;
    		}
    	}
    somehow the second time this thing is looping,
    it does not stop and prompts me for "Enter a character"
    it displays ("Do you still want to enter more characters\n1. YES\n2. NO\nChoose either 1 or 2:" and only stop to ask me to
    choose yes or no there..

    anyone can help?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The first call to scanf reads a single character, but you most likely had to hit return for the buffer to be sent for processing. Because of this, the stream contains two characters, the one you typed and a newline. The newline then causes problems with further calls to scanf. The simplest solution is to call getchar and clear it out before it can cause mischief.
    Code:
    while (check == 0)
    	{
    		printf("\nEnter a character:\n");
    		scanf("%c",&val);
                    getchar();
    		curr->next=initLLNode(val);
    		curr=curr->next;
    		printf("Do you still want to enter more characters\n1. YES\n2. NO\nChoose either 1 or 2:");
    		scanf("%d",&chose);
    		num=num+1;
    		if (chose==2)
    		{
    			check = 1;
    		}
    	}
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    well...i tried this
    but it don't seem to work too..



    confused:
    Code:
    while (check == 0)
    	{
    		printf("Enter a character:");
    		val=getchar();
    		curr->next=initLLNode(val);
    		curr=curr->next;
    		printf("\nDo you still want to enter more characters\n1. YES\n2. NO\nChoose either 1 or 2:");
    		scanf("%d",&chose);
    		num++;
    		printf("\n");
    		if (chose==2)
    		{
    			check = 1;
    		}
    	}

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    edna, try putting getchar() after scanf("%d",&chose);
    Only by the cross are you saved...

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    Talking

    it works now

    tanx all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 11:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  4. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM