Thread: Please Help Me! Executing Problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Please Help Me! Executing Problem

    I already compile my C and it went well. But when it comes to execute it, it indicate error,..please help me with this,..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct ptr
    {int data;
    struct ptr *nextptr;
    };
    
    
    typedef struct ptr POINT;
    
    void printList (POINT *sptr);
    insert (POINT **startptr);
    deduct (POINT **startptr);
    
    void main()
    
    {
    	int choice;
    	POINT *startptr=NULL;
    
    	printf("Please key in your choice\n\n");
    	printf("1.INSERT\n");
    	printf("2.DEDUCT\n");
    	printf("3.DISPLAY SCREEN\n");
    	printf("4.EXIT\n");
    	printf("INPUT : ");
    	scanf("%d",&choice);
    	
    
    	while (choice!=4)
    	{
    
    		if (choice==1)
    		{
    			insert(&startptr);
    		}
    		else if (choice==2)
    		{
    			deduct(&startptr);
    		}
    		else if (choice==3)
    		{
    			printList(startptr);
    		}
    
    		printf("Please key in your choice\n\n");
    	printf("1.INSERT\n");
    	printf("2.DEDUCT\n");
    	printf("3.DISPLAY SCREEN\n");
    	printf("4.EXIT\n");
    	printf("INPUT : ");
    	scanf("%d",&choice);
    	
    	}
    
    }
    
    insert(POINT **startptr)
    {
    	int user_input;
    	char user_input2;
    
    	POINT *tptr=NULL, *nextptr=NULL, *lastptr=NULL, *newptr=NULL, *tempptr=NULL;
    	
    	tptr = *startptr;
    
    	printf("Enter Input : ");
    	scanf("%d",&user_input);
    	
    	while(user_input!=-1)
    	{
    		if (tptr==NULL)
    		{
    			tptr=(POINT *) malloc(sizeof(POINT));
    			tptr->data=user_input;
    			tptr->nextptr=NULL;
    			lastptr=tptr;
    			*startptr=tptr;
    		}
    
    		else
    		{
    			lastptr->nextptr = (POINT *) malloc(sizeof(POINT));
    			lastptr=lastptr->nextptr;
    			lastptr->data=user_input;
    			lastptr->nextptr=NULL;
    		}
    		
    		printf("Do you want to enter another number? ");
    		scanf("%c",&user_input2);
    
    		if (user_input2=='Y'||user_input2=='y')
    		{
    			printf("Please enter value: ");
    			scanf("%d",&user_input);
    		}
    		else if (user_input=='N'||user_input2=='n')
    		{
    			user_input=-1;
    		}
    	}
    
    	return (0);
    }
    
    void printList(POINT *sptr)
    {
    	POINT *tempptr=sptr;
    
    	while (tempptr!=NULL)
    		{
    
    		printf("Thank You.\n");
    		}
    
    	printf("NULL\n\n");
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So tell us what the error is, and when the error happens - does it happen immediately, or do you do something to make it happen?

    Code:
    void printList(POINT *sptr)
    {
    	POINT *tempptr=sptr;
    
    	while (tempptr!=NULL)
    		{
    
    		printf("Thank You.\n");
    		}
    
    	printf("NULL\n\n");
    
    }
    This function will loop forever printing "Thank You." if sptr is not NULL, as there is no changing of tempptr inside the loop.

    Code:
    tptr=(POINT *) malloc(sizeof(POINT));
    If you compile this with a C compiler, then it shouldn't need a cast for malloc().

    Also, why don't you start your while loop in main ABOVE the menu, rather than duplicating the menu inside the loop.

    These two chunks do almost the same thing
    Code:
    			tptr=(POINT *) malloc(sizeof(POINT));
    			tptr->data=user_input;
    			tptr->nextptr=NULL;
    			lastptr=tptr;
    and
    Code:
    			lastptr->nextptr = (POINT *) malloc(sizeof(POINT));
    			lastptr=lastptr->nextptr;
    			lastptr->data=user_input;
    			lastptr->nextptr=NULL;
    It could be coded differently, using tptr, such that you don't repeat almost identical code. Repeating almost identical code is bad practice, and can lead to subtle problems when you modify one but not the other variant of the almost identical code.
    The variables nextptr and newptr aren't used in that function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM