Like Tree2Likes
  • 2 Post By Matticus

Code Keeps Exiting With 0

This is a discussion on Code Keeps Exiting With 0 within the C Programming forums, part of the General Programming Boards category; hi was wondering can anyone help me whenever i run this on Visual Studio, it keeps exiting with code 0 ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    3

    Code Keeps Exiting With 0

    hi was wondering can anyone help me
    whenever i run this on Visual Studio, it keeps exiting with code 0

    Code:
    #include "stdafx.h"
    #include "stdio.h" //  NULL defined here 
    #include "stdlib.h" //  malloc function here 
    #include <string.h> //for handling strings
    #include <ctype.h> 
    
    
    char reg_num[21]; //used in get_new_value
    
    
    struct TAXI_STRUCT
    struct TaxiNode
    {
        char* pszRegistrationNo;
        TaxiNode* pNext;
        TaxiNode* pPrevious;
    };
     
    struct TaxiQueue
    {
        TaxiNode* pFirstTaxi;
        TaxiNode* pLastTaxi;
    };
    
    
    typedef struct node *Listpointer;
    
    
    /*typedef struct node
    { 
        char value[21]; 
        struct node *next; 
    }*Listpointer; */
    
    
    //global variables
    
    
    
    
    //prototypes
    void menu(char choice[21]);
    void read_strings(char s [21], int n);
    void get_new_value(Listpointer ptr);
    void arrive(Listpointer ptr);
    void print(Listpointer ptr);
    int search(Listpointer ptr, char search_string[]);
    void leave(Listpointer ptr);
    void front(Listpointer ptr);
    void back(Listpointer ptr);
    void empty(Listpointer ptr);
    c.:
    
    
    #include "linkedlist.h"
    
    
    int main()
    {
        //variable declarations
        char choice[21];
        char search_string[21];
        int diff; //for strcmp()
        int done;
        Listpointer list;
        
        //initialise head of list
        list = (Listpointer)malloc(sizeof(struct node));    
        list -> next = NULL;
        strcpy(list -> value, "EMPTY");
        
        //call the menu
        menu(choice);
        
        diff = strcmp(choice, "exit");
        while(diff != 0)
        {
            done = 0;
            diff = strcmp(choice, "arrive");
            //arrive choice
            if(diff == 0)
            {
                get_new_value(list);
                arrive(list);
                done = 1;
            }
            
            diff = strcmp(choice, "leave");
            //leave choice
            if(diff == 0)
            {
                leave(list);
                done = 1;
            }
            
            diff = strcmp(choice, "search");
            //search choice
            if(diff == 0)
            {
                printf("Enter a registration number:\n");
                read_strings(search_string, 1);
                 search(list,search_string);
                done = 1;
            }
            
            diff = strcmp(choice, "print");
            //print choice
            if(diff == 0)
            {
                print(list);
                done = 1;
            }
            
            diff = strcmp(choice, "front");
            //front choice
            if(diff == 0)
            {
                front(list);            
                done = 1;
            }
            
            diff = strcmp(choice, "back");
            //back choice
            if(diff == 0)
            {
                back(list);
                done = 1;
            }
            
            diff = strcmp(choice, "empty");
            //empty choice
            if(diff == 0)
            {
                empty(list);
                done = 1;
            }
            //default choice
            if(done == 0)
                printf("Choice not recognised. ");
            
            //recall menu
            menu(choice);
            diff = strcmp(choice,"exit");
        }
        
        return (1);
    }
    
    
    //system menu
    void menu(char choice[21])
    {
        //display the menue
        printf("Please choose from one of the following\n");
        printf("1.Arrive\n");
        printf("2.Depart\n");
        printf("3.Print\n");
        printf("4.Find\n");
        printf("0.exit\n");
        read_strings(choice, 1);
        printf("\n");
    }
    
    
    //read in a string to a given array
    void read_strings(char s [21], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            gets(s[i]);
        }
    }
    
    
    //validates the value to be entered into the list
    void get_new_value(Listpointer ptr)
    {
        int done = 1;
        int diff = 0; //for strcmp()
        int i;
        
        while(done == 1)
        {
            done = 0;
            printf("enter the registration number, 'empty' to abort\n");
            read_strings(reg_num, 1);
            
            //convert to lower case
            for(i = 0; reg_num[i]; i++)
            {
                reg_num[i] = toupper(reg_num[i]);
            }
            
            //clear any symbols
            
            //check for abort entry
            diff = strcmp(reg_num,"EMPTY");
            if(diff != 0)
            {
                //if abort entry found, set done to 1
                done = 1;
            }
            
            if(done == 1)
            {
                done = 0;
                while(ptr != NULL) 
                { //  not end 
                    diff = strcmp(ptr -> value, reg_num);
                    if((diff == 0) && (done != 1)) 
                    { 
                        done = 1; //found reg
                    }
                     
                    ptr = ptr -> next; 
                } //  end while 
                
                if(done == 1)
                {
                    //error message for car found in array.
                    printf("That car is already in the list\n");
                }
            }
        }
    }
    
    
    void arrive(Listpointer ptr)
    {
        Listpointer new; //now declared here
        int diff; //for strcmp()
        diff = strcmp(reg_num, "EMPTY");
        if(diff != 0)
        {
            while(ptr -> next != NULL)
            {
                ptr = ptr -> next;
            }
            
            new = (Listpointer)malloc(sizeof(struct node));
            strcpy(new -> value, reg_num);
            ptr -> next = new;
            new -> next = NULL;
        }
        else
        {
            printf("Car not added\n");
        }
    }
    
    
    void leave(Listpointer ptr)
    {    
        Listpointer delete; //now declared here
        if(ptr -> next != NULL)
        {
            printf("taxi %s has left the rank\n", ptr -> next -> value);
            delete = (Listpointer)malloc(sizeof(struct node));
            delete -> next = ptr -> next;
            if(ptr -> next == NULL)
            {
                free(ptr -> next);
            }
            else
            {
                ptr -> next = (ptr -> next -> next);
            }
            
            free(delete -> next);
            free(delete);
        }
        else
        {
            printf("the list is currently empty\n");
        }
    }
    
    
    int search(Listpointer ptr, char search_string[])
    {
        int diff; //for strcmp()
        int i;
        int taxiNumber = 0;
        //convert to lower case
        for(i = 0; search_string[i]; i++)
        {
            search_string[i] = toupper(search_string[i]);
        }
            
        while(ptr != NULL)
        {
            diff = strcmp(ptr -> value, search_string);
            if(diff == 0)
            {
                printf("taxi %s is %d from the top of the list\n", search_string, taxiNumber);
                return (1);
            }
            ptr = ptr -> next;
            taxiNumber ++;
        }
        printf("taxi %s not found\n", search_string);
        return (0);
    }
    
    
    void print(Listpointer ptr)
    {
        int taxiNumber = 0;
        int diff; //for strcmp()
        while(ptr != NULL)
        {
            diff = strcmp(ptr -> value, "EMPTY");
            if(diff != 0)
            {
                printf("%d. %s\n", taxiNumber, ptr -> value);
            }
            taxiNumber ++;
            ptr = ptr -> next;
        }
    }
    
    
    void front(Listpointer ptr)
    {
        if(ptr -> next != NULL)
        {
            ptr = ptr -> next;
            printf("The taxi at the front of the list is %s\n", ptr -> value);
        }
        else
        {
            printf("The queue is currently empty\n");
        }
    }
    
    
    void back(Listpointer ptr)
    {
        if(ptr -> next != NULL)
        {
            while(ptr -> next != NULL)
            {
                ptr = ptr -> next;
            }
            printf("The taxi at the back of the queue is %s\n", ptr -> value);
        }
        else
        {
            printf("The list is currently empty\n");
        }
    }
    
    
    void empty(Listpointer ptr)
    {
        if(ptr -> next != NULL)
        {
            printf("The list is not empty\n");
        }
        else
        {
            printf("The list is empty\n");
        }
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    I don't know how it returns anything. It doesn't even compile for me.

    main.c|14|error: unknown type name ‘TaxiNode’|
    main.c|15|error: unknown type name ‘TaxiNode’|
    main.c|11|error: two or more data types in declaration specifiers|
    main.c|20|error: unknown type name ‘TaxiNode’|
    main.c|21|error: unknown type name ‘TaxiNode’|
    main.c|51|error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token|
    main.c|54|fatal error: linkedlist.h: No such file or directory|
    ||=== Build finished: 7 errors, 0 warnings ===|
    Are you compiling this as C or C++?

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    im compiling this with C

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Code:
    #include "stdafx.h"
    #include "stdio.h" //  NULL defined here 
    #include "stdlib.h" //  malloc function here 
    #include <string.h> //for handling strings
    #include <ctype.h> 
    
    
    char reg_num[21]; //used in get_new_value
    
    
    struct node
    { 
    	char value[21]; 
    	struct node *next; 
    }; 
    typedef struct node *Listpointer;
    
    
    /*typedef struct node
    { 
    	char value[21]; 
    	struct node *next; 
    }*Listpointer; */
    
    
    //global variables
    
    
    typedef struct node *Listpointer;
    
    
    /*typedef struct node
    { 
    	char value[21]; 
    	struct node *next; 
    }*Listpointer; */
    
    
    //global variables
    
    
    
    
    //prototypes
    void menu(char choice[21]);
    void read_strings(char s [21], int n);
    void get_new_value(Listpointer ptr);
    void arrive(Listpointer ptr);
    void print(Listpointer ptr);
    int search(Listpointer ptr, char search_string[]);
    void leave(Listpointer ptr);
    void front(Listpointer ptr);
    void back(Listpointer ptr);
    void empty(Listpointer ptr);
    c.:
    
    
    #include "linkedlist.h"
    
    
    int main()
    {
    	//variable declarations
    	char choice[21];
    	char search_string[21];
    	int diff; //for strcmp()
    	int done;
    	Listpointer list;
    	
    	//initialise head of list
    	list = (Listpointer)malloc(sizeof(struct node));	
    	list -> next = NULL;
    	strcpy(list -> value, "EMPTY");
    	
    	//call the menu
    	menu(choice);
    	
    	diff = strcmp(choice, "exit");
    	while(diff != 0)
    	{
    		done = 0;
    		diff = strcmp(choice, "arrive");
    		//arrive choice
    		if(diff == 0)
    		{
    			get_new_value(list);
    			arrive(list);
    			done = 1;
    		}
    		
    		diff = strcmp(choice, "leave");
    		//leave choice
    		if(diff == 0)
    		{
    			leave(list);
    			done = 1;
    		}
    		
    		diff = strcmp(choice, "search");
    		//search choice
    		if(diff == 0)
    		{
    			printf("Enter a registration number:\n");
    			read_strings(search_string, 1);
     			search(list,search_string);
    			done = 1;
    		}
    		
    		diff = strcmp(choice, "print");
    		//print choice
    		if(diff == 0)
    		{
    			print(list);
    			done = 1;
    		}
    		
    		diff = strcmp(choice, "front");
    		//front choice
    		if(diff == 0)
    		{
    			front(list);			
    			done = 1;
    		}
    		
    		diff = strcmp(choice, "back");
    		//back choice
    		if(diff == 0)
    		{
    			back(list);
    			done = 1;
    		}
    		
    		diff = strcmp(choice, "empty");
    		//empty choice
    		if(diff == 0)
    		{
    			empty(list);
    			done = 1;
    		}
    		//default choice
    		if(done == 0)
    			printf("Choice not recognised. ");
    		
    		//recall menu
    		menu(choice);
    		diff = strcmp(choice,"exit");
    	}
    	
    	return (1);
    }
    
    
    //system menu
    void menu(char choice[21])
    {
    	//display the menue
    	printf("Please choose from one of the following\n");
    	printf("1.Arrive\n");
    	printf("2.Depart\n");
    	printf("3.Print\n");
    	printf("4.Find\n");
    	printf("exit\n");
    	read_strings(choice, 1);
    	printf("\n");
    }
    
    
    //read in a string to a given array
    void read_strings(char s [21], int n)
    {
    	int i;
    	for(i = 0; i < n; i++)
    	{
    		gets(s[i]);
    	}
    }
    
    
    //validates the value to be entered into the list
    void get_new_value(Listpointer ptr)
    {
    	int done = 1;
    	int diff = 0; //for strcmp()
    	int i;
    	
    	while(done == 1)
    	{
    		done = 0;
    		printf("enter the registration number, 'empty' to abort\n");
    		read_strings(reg_num, 1);
    		
    		//convert to lower case
    		for(i = 0; reg_num[i]; i++)
    		{
    			reg_num[i] = toupper(reg_num[i]);
    		}
    		
    		//clear any symbols
    		
    		//check for abort entry
    		diff = strcmp(reg_num,"EMPTY");
    		if(diff != 0)
    		{
    			//if abort entry found, set done to 1
    			done = 1;
    		}
    		
    		if(done == 1)
    		{
    			done = 0;
    			while(ptr != NULL) 
    			{ //  not end 
    				diff = strcmp(ptr -> value, reg_num);
    				if((diff == 0) && (done != 1)) 
    				{ 
    					done = 1; //found reg
    				}
    				 
    				ptr = ptr -> next; 
    			} //  end while 
    			
    			if(done == 1)
    			{
    				//error message for car found in array.
    				printf("That car is already in the list\n");
    			}
    		}
    	}
    }
    
    
    void arrive(Listpointer ptr)
    {
    	Listpointer new; //now declared here
    	int diff; //for strcmp()
    	diff = strcmp(reg_num, "EMPTY");
    	if(diff != 0)
    	{
    		while(ptr -> next != NULL)
    		{
    			ptr = ptr -> next;
    		}
    		
    		new = (Listpointer)malloc(sizeof(struct node));
    		strcpy(new -> value, reg_num);
    		ptr -> next = new;
    		new -> next = NULL;
    	}
    	else
    	{
    		printf("Car not added\n");
    	}
    }
    
    
    void leave(Listpointer ptr)
    {	
    	Listpointer delete; //now declared here
    	if(ptr -> next != NULL)
    	{
    		printf("taxi %s has left the rank\n", ptr -> next -> value);
    		delete = (Listpointer)malloc(sizeof(struct node));
    		delete -> next = ptr -> next;
    		if(ptr -> next == NULL)
    		{
    			free(ptr -> next);
    		}
    		else
    		{
    			ptr -> next = (ptr -> next -> next);
    		}
    		
    		free(delete -> next);
    		free(delete);
    	}
    	else
    	{
    		printf("the list is currently empty\n");
    	}
    }
    
    
    int search(Listpointer ptr, char search_string[])
    {
    	int diff; //for strcmp()
    	int i;
    	int taxiNumber = 0;
    	//convert to lower case
    	for(i = 0; search_string[i]; i++)
    	{
    		search_string[i] = toupper(search_string[i]);
    	}
    		
    	while(ptr != NULL)
    	{
    		diff = strcmp(ptr -> value, search_string);
    		if(diff == 0)
    		{
    			printf("taxi %s is %d from the top of the list\n", search_string, taxiNumber);
    			return (1);
    		}
    		ptr = ptr -> next;
    		taxiNumber ++;
    	}
    	printf("taxi %s not found\n", search_string);
    	return (0);
    }
    
    
    void print(Listpointer ptr)
    {
    	int taxiNumber = 0;
    	int diff; //for strcmp()
    	while(ptr != NULL)
    	{
    		diff = strcmp(ptr -> value, "EMPTY");
    		if(diff != 0)
    		{
    			printf("%d. %s\n", taxiNumber, ptr -> value);
    		}
    		taxiNumber ++;
    		ptr = ptr -> next;
    	}
    }
    
    
    void front(Listpointer ptr)
    {
    	if(ptr -> next != NULL)
    	{
    		ptr = ptr -> next;
    		printf("The taxi at the front of the list is %s\n", ptr -> value);
    	}
    	else
    	{
    		printf("The queue is currently empty\n");
    	}
    }
    
    
    void back(Listpointer ptr)
    {
    	if(ptr -> next != NULL)
    	{
    		while(ptr -> next != NULL)
    		{
    			ptr = ptr -> next;
    		}
    		printf("The taxi at the back of the queue is %s\n", ptr -> value);
    	}
    	else
    	{
    		printf("The list is currently empty\n");
    	}
    }
    
    
    void empty(Listpointer ptr)
    {
    	if(ptr -> next != NULL)
    	{
    		printf("The list is not empty\n");
    	}
    	else
    	{
    		printf("The list is empty\n");
    	}
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,229
    That's the problem with just copying code and not understanding anything about it.

    It appears from the original link that lines 1 - 51 are in a separate header file - this was clearly not taken into account in the code you posted.

    That's certainly not the only issue with this code, but that's as far as I'm willing to go on this one.
    rags_to_riches and Salem like this.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    I wonder if MSVC complains about all the things wrong with gets().

    I noticed that the original poster on the other forum made a complete pigs-ear of read_strings(), but your attempt at fixing it hasn't improved things.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing something before exiting.
    By samus250 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 06:33 PM
  2. exiting
    By MOH123 in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2005, 12:23 PM
  3. Exiting ?
    By slx47 in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2003, 10:19 AM
  4. Exiting
    By C-Struggler in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 03:09 PM
  5. exiting
    By bart in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2001, 01:25 PM

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