Thread: random number generator and function

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

    random number generator and function

    I know my code is very messy and there are alot of errors right now but i have about 3 more functions to create but im not quite sure about how to go about these. I am still learning C so any guidance would be great
    //Create random number generator for the number of moves
    //Create random number generator for the which vehile to move.
    //Create a fuctnion that writes to the txt file

    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 RCinfo[4];
    };
    
    
    //prototypes
    int car_pic(struct cars *carpointer, int size);
    int Modify(struct cars *modpointer, int size, int CarSelect);
    int readCarsTXT (struct cars *readpointer, int size);
    int manual (struct cars *manualpointer, int size);
    int automatic (struct cars *automaticpointer, int size);
    int movement (struct cars *movementpointer, int size);
    
    
     int main (void)
    {
        //declaring variables in array
        int i=0;
        int lower=0;
        int upper=10;
        int count=1;
        int automaticmovement;
        int manualmovement;
        int AutoOrManual;
        int automatic;
        int CarType;
        int number;
        int CarColor;
        char DriverName;
        int manualrace;
        int v1;
        int v2;
        int modifyorstart;
        int automatic2;
        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");
                int manualrace= manual();
            //CAll Function that runs that type of race
            break;
            case 2:
            printf("You have selected Automatic");
                int automaticrace= automatic();
            break;
            case 3:
                printf("EXIT");
                ExitFlag=1;
            default:
                printf("please enter a valid number");
            break;
          }
    
    
    
    
    
    
      }while(ExitFlag==0);
    
    
    
    
     return 0;
    }
    
    
    int automatic ()
    {
        struct cars AutoCars[4];
        struct cars ModifyCars[6];
        struct cars StartCars[0];
        int CarSelection=0;
        int CarModify=0;
        int CarStart=0
        
        readCarsTXT()
        readCarsTXT(AutoCars, CARS_ARRAY_SIZE);
        CarSelection=car_pic(AutoCars,CARS_ARRAY_SIZE);
        readCarsTXT(ModifyCars, CARS_ARRAY_SIZE);
        readCarsTXT(StartCars, CARS_ARRAY_SIZE);
        CarStart= 
        
        printf("press 1 if you would like to modify your car or press 2 if you would like to start the game");
        switch (modifyorstart)
        {
        case 1: 
            CarModify=Modify(ModifyCars,CARS_ARRAY_SIZE);
        break;
        case 2:
            //SOMETHING TO START THE GAME
        break;
        default:
            printf("please enter a valid number");
        break;
            
        }
        
        // Ask here whether they want to mod car or start
    
    
        //YOu can run the race in this function or create a new one
    
    
    
    
    }
    
    
    //Create random number generator for the number of moves
    //Create random number generator for the which vehile to move.
    //Create a fuctnio the writes to the txt file
    
    
     
    int manual()
    {
        struct cars ManualRace[2];
        int playerone=0;
        int playertwo=0;
        
        readCarsTXT()
        readCarsTXT(ManualRace, CARS_ARRAY_SIZE);
        playerone=car_pic(ManualRace,CARS_ARRAY_SIZE);
        playertwo=car_pic((ManualRace,CARS_ARRAY_SIZE);
        
     
    
    
    }
    
    
    
    
    
    
    int Modify(struct cars *modpointer, int size, int CarSelect)
    {
        int i = CarSelect;
        int CarType1;
        int DriverName;
        int numberA;
        int CarColor1;
        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", &CarType1);
       switch(CarType1)
       {
           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", &CarType1);
            strcpy ((modpointer+i)->make, CarType1);
            break;
        case 3:
            printf("\nplease enter the number you want");
            scanf("%d", &numberA);
            strcpy((modpointer+i)->car_number, numberA);
            break;
        case 4:
            printf("please enter the color you want your car");
            scanf("%s", &CarColor1);
            strcpy((modpointer+i)->color, CarColor1);
            break;
        default:
            printf("Wrong INput");
       }
    
    
    }
    
    
    
    
        //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)
    {
        // declaring variables
        int i=0;
        FILE *carFilePointer;
        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",readpointer-> name, readpointer-> make, &readpointer->car_number, readpointer-> color);
                // pointer arithmetic. point to the next array element
                readpointer++;
            }
    
    
            // call car_pic function
        }
        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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    A development process
    To be honest, you don't keep adding code to 200+ lines of code which is already broken.

    You need to make your development iterations much smaller.

    Write 10 lines, compile it.
    If it doesn't compile, fix it.
    When it compiles, test it - does it do what you want?
    If it doesn't, fix it until it does what you want.


    When you have something which works, it's a good idea to make a backup copy of it so you always have some 'last known good' code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator Stuck on 1 Number?
    By Dollydaydream in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2013, 08:43 AM
  2. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  3. C random number generator function...
    By towed in forum C Programming
    Replies: 6
    Last Post: 04-29-2011, 01:30 AM
  4. random number generator
    By begginer in forum C Programming
    Replies: 5
    Last Post: 03-08-2011, 12:12 AM
  5. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 07:34 AM

Tags for this Thread