Thread: Help with my Taxi Rank Code

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

    Help with my Taxi Rank Code

    Code:
     #include <stdio.h>
    #include<stdlib.h>
    
    #define MAX 6
    
    
    int add (const int t_array[], int f, int r, int max, int added_t);
    int leave (const int t_array[], int f, int r);
    int linearSearch (const int t_array[], int key, int max);
    int display (const int t_array[], int f, int r);
    int displayFront (const int t_array[], int f);
    int displayRear (const int t_array[], int r);
    int emptyDisplay (const int t_array[], int f);
    
    
    int main()
    
    {
        int taxi_array[MAX];
    	int menu = 0;
    	int rear = -1;
        int front = -1;
    	int searchKey;
    	int taxi_element;
    	int addedTaxi;
    
    
    // Loop for Menu
    while ( menu <= 8 )
    {
    
    	//Menu for User
    	printf("-------------------------------\n");
    	printf("Please Select an option from the menu.\n");
    		printf("1. arrive:	Add a taxi to the rear of the queue.\n");
    		printf("2. leave:	Remove the taxi at the front of the queue.\n");
    		printf("3. search:	Search for a taxi within the queue.\n");
    		printf("4. print:	Display the reg numbers of the taxis in the queue.\n");
    		printf("5. front:	Display the reg number of the taxi at the front of the queue.\n");
    		printf("6. back:	Display the reg number of the taxi at the back of the queue.\n");
    		printf("7. empty:	Display if the queue is Empty or not.\n");
    		printf("8. exit:	Exit the program.\n");
    		printf("-------------------------------\n");
    	scanf_s("%d",&menu);				
    	
    
    	switch (menu)                  // Menu System for the Taxi Queue
        {
        case 1:
    		                            //Enter Taxi Registration Number to the Queue 		
    		add(taxi_array, front, rear, MAX, addedTaxi);             
    		printf("The Following Taxi has joined the queue: %s \n", taxi_array[rear]);
            break;
        case 2:							// Remove the First Taxi from the Queue
    		printf("The taxi at the front of the queue will now be removed.\n");
    		leave(taxi_array, front, rear);
    		printf("The following taxi will be removed: %s \n", taxi_array[front]);
            break;
        case 3:							// Search list for a taxi registration Number 
            printf( "Enter the reg of the taxi you want to find :\n" );
            scanf_s( "%d", &searchKey );
    		taxi_element = linearSearch( taxi_array, searchKey, MAX );
    
            if ( taxi_element != -1 ) {
    			printf( "Found reg in queue postion: %d\n", taxi_element );
            } 
            else {
            printf( "Value not found\n" );
            } 
    
            return 0;
    
            break;
        case 4:							// Print the Taxi Queue in the Current State
    		printf("This will now display all of the taxis. \n");
            display(taxi_array, front, rear);
            break;
    	case 5:							// Which Taxi is at the Front of the Queue
            printf("This will now display the taxi at the front of the queue.\n");
    		displayFront(taxi_array, front);
    		break;
    	case 6:							// Which Taxi is at the Back of the Queue
    		printf("This will now display the taxi at the back of the queue.\n");
    		displayRear(taxi_array, rear);
    		break;
    	case 7:							// Display if the queue is Empty or Not
            printf("This will now show if the queue is empty or not. \n");
    		emptyDisplay(taxi_array, front);
    		break;
    	case 8:							// Exit the program
    		exit(0);					
    		break;
    
    	default:
    		
    		break;
    	}
    
    }
    
    
    int add(const int taxi_array[], int front, int rear, int MAX, int addedTaxi)
    
    {
    if (rear==MAX-1)
    printf("Taxi Rank Full\n");
    }
    else (front==-1){ //If queue is initially empty
    front=0;
    printf("Input the reg of the new taxi : ");
    scanf_s("%d", &addedTaxi);
    rear=rear+1;
    taxi_array[rear] = addedTaxi;
    }//End of add
    
    int leave(const int taxi_array[], int front, int rear)
    {
    if (front == -1 || front > rear)
    {
    printf("Space in Queue\n");
    
    }
    else
    {
    printf("Taxi reg removed : %d\n", taxi_array[front]);
    front=front+1;
    }
    }//End of Leave
    
    int linearSearch(const int taxi_array[], int searchKey, int MAX)
    {
       int n;
       for ( n = 0; n < max; ++n ) {
          if ( taxi_array[ n ] == key ) { 
             return n;
          } 
       } 
       return -1;
    
    }//End of Search
    
    
    int display(const int taxi_array[], int front, int rear)
    {
    int i;
    if (front == -1)
    printf("Queue is empty\n");
    else
    {
    printf("Queue is :\n");
    for(i=front;i<= rear;i++)
    printf("%d ",taxi_array[i]);
    printf("\n");
    }
    }//End of Display
    
    int displayFront(const int taxi_array[], int front)
    {
    printf("%d ",taxi_array[front]);
    }//End of displayFront
    
    
    
    
    int displayRear(const int taxi_array[], int rear)
    {
    printf("%d ",taxi_array[rear]);
    }//End of displayRear
    
    
    
    int emptyDisplay(const int taxi_array[], int front)
    {
    int i;
    if (front == -1)
    printf("Queue is empty\n");
    else
    printf("Queue has taxis in it.\n");
    }//End of Empty Display
    errors:

    : error C2143: syntax error : missing ';' before 'type'
    : error C2059: syntax error : 'else'
    : error C2143: syntax error : missing ')' before 'constant'
    : error C2143: syntax error : missing '{' before 'constant'
    : error C2059: syntax error : '<Unknown>'
    : error C2059: syntax error : ')'

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First, "missing" type errors can easily be avoided by having a decent approach to indentation.
    SourceForge.net: Indentation - cpwiki

    Second, what did you do to stop it compiling?
    A development process

    Write in small steps, compile as you go.
    If you write 5 new lines, and get some new compiler errors, you only have to consider the 5 lines you wrote.

    Writing 100 lines and then wondering what to do on the first compile is not a good strategy.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    #define MAX 6
    
    
    int add (const int t_array[], int f, int r, int max, int added_t);
    
    
    int main()
    
    {
        int taxi_array[MAX];
    	int menu = 0;
    	int front = -1;
        int rear = -1;
    	int addedTaxi;
    
    // Loop for Menu
    while ( menu <= 8 )
    {
    
    //Menu for User
    printf("-------------------------------\n");
    printf("Please Select an option from the menu.\n");
    	printf("1. arrive:	Add a taxi to the rear of the queue.\n");
    printf("-------------------------------\n");
    scanf_s("%d",&menu);				
    	
    
    
    switch (menu)                  // Menu System for the Taxi Queue
        {
        case 1:
    		                            //Enter Taxi Registration Number to the Queue 		
    		add(taxi_array, front, rear, MAX, addedTaxi);             
    		printf("The Following Taxi has joined the queue: %s \n", taxi_array[rear]);
            break;
        
    	default:
    		
    		break;
    	}
    }
    
    int add(const int taxi_array[], int front, int rear, int MAX, int addedTaxi)
    
    	if (rear==MAX-1)
    	{
        printf("Taxi Rank Full\n");
    	}
    	else if (front==-1) //If queue is initially empty
        {
        front=0;
    	printf("Input the reg of the new taxi\n");
    	scanf_s("%d", addedTaxi);
    	rear=rear+1;
    	taxi_array[rear] = addedTaxi;
    	}//End of add
    Started to rewrite my code and still getting these two errors please help

    (46) : error C2143: syntax error : missing ';' before 'type'
    (52) : error C2059: syntax error : 'else'

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is what your code looks like, indented.
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    #define MAX 6
    
    int add (const int t_array[], int f, int r, int max, int added_t);
    
    int main()
    {
        int taxi_array[MAX];
        int menu = 0;
        int front = -1;
        int rear = -1;
        int addedTaxi;
    
        // Loop for Menu
        while ( menu <= 8 )
        {
            //Menu for User
            printf("-------------------------------\n");
            printf("Please Select an option from the menu.\n");
            printf("1. arrive:    Add a taxi to the rear of the queue.\n");
            printf("-------------------------------\n");
            scanf_s("%d",&menu);                
            
            switch (menu)                  // Menu System for the Taxi Queue
            {
            case 1:
                //Enter Taxi Registration Number to the Queue         
                add(taxi_array, front, rear, MAX, addedTaxi);             
                printf("The Following Taxi has joined the queue: %s \n", taxi_array[rear]);
                break;
                
            default:
                
                break;
            }
        }
    
        int add(const int taxi_array[], int front, int rear, int MAX, int addedTaxi)
    
        if (rear==MAX-1)
        {
            printf("Taxi Rank Full\n");
        }
        else if (front==-1) //If queue is initially empty
        {
            front=0;
            printf("Input the reg of the new taxi\n");
            scanf_s("%d", addedTaxi);
            rear=rear+1;
            taxi_array[rear] = addedTaxi;
        }//End of add
    See how add() is indented - your main is missing a closing brace.

    See what is missing at the end of your add function definition - another brace pair.

    This is how you should type in code.
    Code:
    int main ( ) {
        return 0;
    }
    Then it becomes
    Code:
    int main ( ) {
        int taxi_array[MAX];
        int menu = 0;
        int front = -1;
        int rear = -1;
        int addedTaxi;
    
        // Loop for Menu
        while ( menu <= 8 )
        {
        }
        return 0;
    }
    Then say
    Code:
    int main ( ) {
        int taxi_array[MAX];
        int menu = 0;
        int front = -1;
        int rear = -1;
        int addedTaxi;
    
        // Loop for Menu
        while ( menu <= 8 )
        {
        }
        return 0;
    }
    
    int add(const int taxi_array[], int front, int rear, int MAX, int addedTaxi)
    {
        return 0;
    }
    At each stage, you compile and make sure it is OK.
    Also, at each stage, you make SURE that braces, parentheses etc are balanced from the START.

    Typing a {, then hoping at some later time you'll get the } in the right place first time, every time, is a waste of effort.
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, a few things that will help you strategically...

    1) Don't put code in switch statements. Write the desired action for each case: as a function and call it from the switch statement. This both compartmentalizes the code to make it easier to debug and simplifies the switch statement making it simper to debug.

    2) Put all function calls above main and do not use prototypes except as forward declarations. This will force you to organize your code in a rational manner, again making debugging easier.

    3) Your indentation strategy appears random. It does not visually represent the way your code works. If you take the time to indent your code according to a fixed strategy, you will find it a lot easier to fix when it breaks.

    4) The error is because you are missing the opening brace in your Add function. A good IDE with brace matching is a huge help here. Alternatively just enter the two braces together { } and insert your code between them as you work.
    Last edited by CommonTater; 03-06-2011 at 08:55 AM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    #define MAX 6
    
    
    int add (int t_array[], int f, int r, int max, int added_t);
    
    
    int main()
    
    {
        int taxi_array[MAX];
    	int menu = 0;
    	int front = -1;
        int rear = -1;
    	int addedTaxi;
    
    // Loop for Menu
    while ( menu <= 8 )
    {
    
    return 0;
    
    
    //Menu for User
    printf("-------------------------------\n");
    printf("Please Select an option from the menu.\n");
    	printf("1. arrive:	Add a taxi to the rear of the queue.\n");
    printf("-------------------------------\n");
    scanf_s("%d",&menu);				
    	
    
    
    switch (menu)                  // Menu System for the Taxi Queue
        {
        case 1:
    		                            //Enter Taxi Registration Number to the Queue 		
    		add(taxi_array, front, rear, MAX, addedTaxi);             
            break;
        
    	default:
    		
    		break;
    	}
    }
    }
    
    
    int add(int taxi_array[], int front, int rear, int MAX, int addedTaxi)
    {
    	return 0;
    }//End of add
    The add function might appear indented on here but it is not on my actual code, I have now started to compile and then write more like you have told but I get up to my add function everytime and get the same errors please help, they are all on line 50 which is the add function line. Tater Im not sure what you mean by saying put the call functions above main, because I need to call them in the switch statment. Thanks for the help so far

    Errors:

    (50) : error C2143: syntax error : missing ')' before 'constant'
    (50) : error C2143: syntax error : missing '{' before 'constant'
    (50) : error C2059: syntax error : '<Unknown>'

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    When I compiled your code I got about 20 warnings, mostly related to missing brackets and uninitialized values...

    Here's a cleaned up and working version from message #6 ... notice how much clearer it is when you format your code correctly...

    Code:
    #include <stdio.h>
    
    #define MAX 6
    
    
    int add(int taxi_array[], int front, int rear, int max)
    {
        return 0;
    }//End of add
    
    
    int main( void )
    {
       int taxi_array[MAX];
       int menu   = 0;
       int front  = -1;
       int rear   = -1;
       int addedTaxi;
    
      // Loop for Menu
      while ( menu <= 8 )
      {//Menu for User
        printf("-------------------------------\n");
        printf("Please Select an option from the menu.\n");
        printf("1. Arrive\tAdd a taxi to the rear of the queue.\n");
        printf("-------------------------------\n");
        scanf("%d",&menu);				
    	 
      // Menu System for the Taxi Queue
      switch (menu)                  
        {
          case 1:                   //Enter Taxi Registration Number to the Queue 
            add(taxi_array, front, rear, MAX);             
            break;
          default:
            break;
        }
      }
    
      return 0; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code style/formatting/organizing
    By nair in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2011, 11:31 AM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM