Thread: C programing Switch statement

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    8

    C programing Switch statement

    I am trying to make this code but i can not figure out how to call the functions i made within my switch statements. I have commented somethings out that I can not figure out what to do
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define CARS_ARRAY_SIZE 4
    struct cars
    {
        char name[20];
        char make[20];
        int car_number;
        char color[20];
        int distance;
    };
    
    
    
    
    
    
     int main (void)
    {
        //declaring variables in array
        int i=0;
        int AutoOrManual;
        int automatic;
        int CarType;
        int number;
        int CarColor;
        char DriverName;
        int ExitFlag=0;
        //opening file for arrayos
      FILE *fPointerCars;
      srand(time(NULL));//
      //Print name and explain this is a racing game
      printf("RoseEllen Hoke\n This is a race car game a text base game to race 4 cars across the screen and determine the first second and third place. \n A text file (car.txt) with the name of the driver, type of the race car, car number, and the color of the car is given to you.");
      do
      {
          struct cars AutoManual[CARS_ARRAY_SIZE];
          printf("enter 1 if you want race against another user or \n enter 2 if you want to race against 4 cars automatically or \n enter 3 to exit");
          scanf("%d",&AutoOrManual);
          switch(AutoOrManual)
          {
            case 1:
            // i do not know how to get it to move to the next thing
            printf("You have selected Manual");
            //CAll Function that runs that type of race
            break;
            case 2:
            printf("You have selected Automatic");
            AutoRace();
            break;
            case 3:
                printf("EXIT");
                ExitFlag=1;
            default:
    
    
            break;
          }
    
    
    
    
    
    
      }while(ExitFlag==0)
    
    
    
    
    //  return 0;
    //}
    //i was going to enter another switch statement for manuel mode but i dont see why its uses all the same
    //componets as autoomatic i feel like
    
    
    
    
    //int AutoRace()
    int car_pic(struct cars *carpointer, int size)
    {
    //CALL the function to read the file and pass it the local array of structures
    
    
        //CALL pick car function and pass it the local array of structures
    
    
        printf("enter 1 if you want to customize your car or 2 if you want start the game");
        scanf("%d", &automatic);
        switch(automatic)
            {   case 1:
                printf("how do you want customize your car?");
                //CAll function to modify the car, pass it the array and the car selection
                break;
                case 2:
                printf("the game will begin into 3 seconds\n");
                //Function that runs the movements
                break;
                case 3:
                    printf("error please enter a valid number");
                break;
            }
    }
    
    
    int Modify(struct cars *modpointer, int size, int CarSelect)
    {
        int i = CarSelect-1;
        printf("Please select your modification\n enter 1 for name\n enter 2 for a make\n enter 3 for a number\n enter 4 for a color");
        scanf("%d", &CarType);
       switch(CarType)
       {case 1:
            printf("\n please enter driver name");
            scanf("%s", &DriverName);
            strcpy((modpointer+i)->name, DriverName);
            break;
        case 2:
            printf("please enter the type of car you want");
            scanf("s", &CarType);
            strcpy ((modpointer+i)->make, CarType);
            break;
        case 3:
            printf("\nplease enter the number you want");
            scanf("%d", &number);
            strcpy((modpointer+i)->car_number, number);
            break;
        case 4:
            printf("please enter the color you want your car");
            scanf("%s", &CarColor);
            strcpy((modpointer+i)->color, CarColor);
            break;
       }
    
    
    }
      //user selects what type of car they want
        printf("Please select your car\n enter 1 for a ford\n enter 2 for a chevy\n enter 3 for a kia\n enter 4 for a porsche");
        scanf("%d", &CarType);
       switch(CarType)
       {case 1:
    
    
           break;
        case 2:
    
    
            break;
        case 3:
    
    
           break;
        case 4:
    
    
            break;
       }
    
    
        //this reads cars.txt file and then prints each statement in the text file
        fPointerCars= fopen("cars.txt","r");
        char singleline[150];
        struct RCinfo Cars [4];
    
    
        while(!feof(fPointerCars))
        {
            fscanf(fPointerCars, "%s %s %d %s",&Cars[i].DriverName,&Cars[i].CarModel,&Cars[i].CarNumber,&Cars[i].CarColor);
            printf("%s %s %d %s", Cars[i].DriverName, Cars [i]. CarModel,  Cars[i]. CarNumber,  Cars [i].CarColor);
    
    
            i++;
        }
        fclose(fPointerCars);
    
    
    
    
        //Chanfe this main to a function that just reads the cars. txt file
        //Pass this function a structure
    int readCarsTXT (struct cars *readpointer, int size)
    {
        // prototype
        //int car_pic(struct cars *, int size);
       // int auto_or_manual (struct auto_or_manual *, int size);
    
    
        // declaring variables
        int i=0;
        int Auto_array_size;
        int cararrayindex;
        int autoormanualindex;
        struct AutoOrManual auto1 [Auto_array_size];
        struct cars carlist[CARS_ARRAY_SIZE];
        FILE *carFilePointer;
        struct cars *carStructPointer;
        struct auto1 *autoStructPointer;
    
    
        // assign pointer to array of structure
        carStructPointer = carlist;
    
    
        carFilePointer = fopen("car.txt", "r");
    
    
        // check  existence of the file
        if (carFilePointer == NULL)
        {
            printf("file does not exist");
        }
        else
        {
            // read the file and assign data to array of structures
            for (i=0; i < CARS_ARRAY_SIZE; i++)
            {
                fscanf(carFilePointer,"%s %s %d %s",carStructPointer-> name, carStructPointer-> make, &carStructPointer->car_number, carStructPointer-> color);
                // pointer arithmetic. point to the next array element
                carStructPointer++;
            }
            // rest pointer to structure
            carStructPointer = carlist;
    
    
            // call car_pic function
            cararrayindex = car_pic (carStructPointer, CARS_ARRAY_SIZE);
            // display the chosen car
            printf("\nyour car: ");
            printf("\t%s %s %d %s\n", carlist[cararrayindex].name, carlist[cararrayindex].make,carlist[cararrayindex].car_number, carlist[cararrayindex].color);
        }
        close(carFilePointer);
    }
    
    
    // function definition
    int car_pic(struct cars *carpointer, int size)
    {
    
    
        int i=0, chosen_car=0;
        // display cars
        for (i=0; i < size; i++)
        {
    
    
            printf("%d- %-12s %-12s %-3d %-12s\n", i+1, carpointer-> name, carpointer-> make, carpointer->car_number, carpointer-> color);
            // pointer arithmetic. point to the next array element
            carpointer++;
        }
        printf("\nEnter the line number for your car  ");
        scanf("%d", &chosen_car);
        // assign array index to chosen_car and return
        chosen_car --;
        return chosen_car;
    }
    
    
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Several problems. First implementing functions inside other functions is not allowed. Second all variables and functions must be declared somewhere before you try to call the function or use the variable.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Several problems. First implementing functions inside other functions is not allowed. Second all variables and functions must be declared somewhere before you try to call the function or use the variable.
    so would you recommend that i declare everything and then for each switch statement only have one function for each one or what would be your best suggestion at attacking this problem, as I am trying to build a race car game where the user has an option to either race against another person or against 4 other people. I know this is probably something simple for you but I am still trying to learn and navigate c so any guidance would be beneficial thank you! I am think that I coded line 109-135 properly is that how I should go about formatting all my other switch statements?
    Last edited by carolbaskin; 04-16-2020 at 06:32 PM.

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    8
    so would you recommend that i declare everything and then for each switch statement only have one function for each one or what would be your best suggestion at attacking this problem, as I am trying to build a race car game where the user has an option to either race against another person or against 4 other people. I know this is probably something simple for you but I am still trying to learn and navigate c so any guidance would be beneficial thank you! I am think that I coded line 109-135 properly is that how I should go about formatting all my other switch statements?

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by jimblumberg View Post
    Several problems. First implementing functions inside other functions is not allowed.
    It is allowed, as an extension, using GCC... But, of couse, it is not advisable:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
      void show( void ) { puts( "Hello" ); }
    
      show();
    }
    Compiling and linking:
    Code:
    $ cc -O2 -o test test.c
    $ ./test
    Hello

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Else If vs Switch statement
    By whiskybrah in forum C Programming
    Replies: 8
    Last Post: 04-11-2016, 12:54 PM
  2. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  3. Switch statement help
    By Tim Yap in forum C++ Programming
    Replies: 27
    Last Post: 10-25-2013, 01:34 AM
  4. Replies: 10
    Last Post: 12-09-2012, 08:17 AM
  5. help with switch statement
    By volk in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2003, 05:53 AM

Tags for this Thread