while loop

This is a discussion on while loop within the C Programming forums, part of the General Programming Boards category; why only the first Iteration workin alright ? Code: #include <stdio.h> #include <stdlib.h> struct Node { int val; struct Node ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    while loop

    why only the first Iteration workin alright ?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Node
    {
    	int val;
    	struct Node *next;
    } ;
    
    typedef struct Node Node ;
    
    
    	void addNode(Node *head,Node *tail)
    	{
    		Node *current, *temp;
    		char Continue='y';
    		head=(Node*)malloc(sizeof(Node));
    		head->val=1;
    		current=head;
    		do
    		{
    			printf("continue?\n");
    			scanf("%c",&Continue);
    			temp=(Node*)malloc(sizeof(Node));
    			printf("value=?\n");
    			scanf("%d",&(temp->val));
    			current->next=temp;
    			current=current->next;
    		}
    		while(Continue!='n');
    	}
    
    
    
    void main()
    {
    	Node *head=NULL, *tail=NULL;
    
    	addNode(head,tail);
    
    }

    TNX

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    #1
    Code:
    do
    {
        ...
        scanf("%c",&Continue);
        ...
        scanf("%d",&(temp->val));
    }
    while(Continue!='n');
    The problem is that %c in the scanf call will read a newline leftover in the input stream from the previous loops read of the integer value. Simplest solution there is to add a space in front of the %c format specifier which will tell the scanf call to ignore leading whitespace.
    Code:
    scanf(" %c",&Continue);
           ^ there's a space there now

    #2
    Code:
    head=(Node*)malloc(sizeof(Node));
    
    ...
    
    temp=(Node*)malloc(sizeof(Node));
    Avoid casting the return value of the malloc call.


    #3 You should be setting the next pointer of the newly allocated node to null after you create it, also you head node's next pointer.



    #4
    Code:
    void main()
    main returns int not void.



    #5
    Code:
    void addNode(Node *head,Node *tail)
    {
        ...
    }
    
    void main()
    {
    	Node *head=NULL, *tail=NULL;
    
    	addNode(head,tail);
    
    }
    If you expect to actually do anything with those head/tail pointers in main after the call to addNode then you'll need to change your addNode function to accept pointer-to-pointer-to-Node arguments rather than just simple pointer-to-Node arguments.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    ok... thank you very much !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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