C++ Help !

Discussion in 'C++' started by steveo1980, Jan 26, 2007.

  1. steveo1980

    steveo1980 New Member

    Joined:
    Jan 26, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Ive gota complete a number of codes , i have the following one to complete ,

    The organiser of a car rally requires a program(s) that will allow him to enter the competitor details, Name, Car (Make and Model), Engine size and Registration number. The program should store this information in a file. The rally is divided into five stages at the end of each stage the time for each competitor is recorded for that stage, this data should also be stored in a file. At the end of the rally when all the stages have been completed, a table should be produced that shows the aggregate time for each of the competitors.

    Example data:

    Name Make Model Engine Size Registration No.
    Ray Andrews Ford Escort MkII 1600 HGY 273M
    John Hill Opel Manta 2000 TBD 498F
    Andrew Smith Austin Mini Cooper 1300 DVT 749G
    The event normally attracts between 15 and 25 competitors.


    Stage times can be entered against the Car Registration number:

    Registration No. Stage Time
    Mins Secs
    HGY 273M 14 45
    TBD 498F 15 56
    DVT 749G 16 04
    [Hint: It may be easier to store the time in seconds instead of minutes and seconds, then use this to perform the calculations, before doing the final output in Minutes and Seconds]

    The final Result will look as follows:

    Name Stage 1 Stage 2 Stage 3 Stage 4 Stage 5 Total
    Ray Andrews 14.33 23.43 12.12 34.56 etc
    John Hill etc etc
    Andrew Smith etc

    The organiser would also like the winner (the driver with the lowest time) to be identified automatically by the computer system.



    THATS THE QUERY, SO FAR I HAVE PUT SOME CODING TOGETHER BUT AM STUCK NOW , IS THEIR ANYONE WHO COULD DO THE LAST LITTLE BITS AND FINISH IT OFF OR GIVE SOME POINTERS PLEASE
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #define MAX 25 /* Maximum number Rally Competitors*/
    FILE *fp;
    char reg[MAX] [10];
    float results [MAX] [20];
     
    
    int menu (char *Option[], int size)
    //At the bottom of the Menu screen - enter choice
    
    {
    	int i, choice=0;
    	char buff [20];
    
    	do
    	{
    		printf ("\n\n\nMenu\n\n");
    		
    		for (i=0; i<=size;i++)
    		{
    			printf ("%d %s\n", i+1, Option [i]);
    		}
    		printf ("\n\nEnter option : ");
    		choice = atoi (gets (buff));
    	}while (choice<1 || choice >size+1);
    	return (choice);
    }
    
    
    int file_open (char *filename, char *mode)
    //	Opens file
    
    { 
    	int success = 0;
    
    	if ((fp = fopen (filename, mode)) == NULL)
    	{ 
    	printf ("Cannot open file");
    	}
    	else
    	{
    	success = 1;
    	printf ("\nFile %s opened", filename);
    	}
    	return success;
    
    }
    
    
    void input (void)
    //	Allows the Input of data
    //	Restricts the insertion of registration numbers to four
    //	and corresponding results for twelve months*/
    
    {
    	int month,minutes,i=0;
    	char buff [20];
    	char filename [25];
    	int open =0;
    	
    	printf ("\nInput\n\n");
    	
    	printf ("\nEnter Car registration numbers\n\nEnter x to finish \n");
             printf("Enter NAME (%3d):",i+1);
    	 gets(NAME[i]);
    	 printf("Enter CAR Make :",i+1);
    	 gets(CARMAKE[i]);
    	  printf("Enter CAR Model :",i+1);
    	 gets(CARmodel[i]);
    	  printf("Enter CAR Engine Size :",i+1);
    	 gets(CARenginesize[i]);
    	 
    	do
    	{
    		printf ("\nEnter reg (%3d) : ", i+1);
    		//Took (%3d) out originally and thought it looked more attractive
    		gets (reg[i]);
    		if (strcmp (reg[i],"x")!=0) i++;
    	} while (strcmp (reg[i], "x") !=0 && i < MAX);
    
    	for (minutes=0; minutes<i; minutes++)
    	{
    		printf ("\nEnter Stage Time 1:         \n\n", reg [minutes]);
       			minutes = atof (gets (buff));
    
    
    		printf ("\nEnter Stage Time 2:         \n\n", reg [minutes]);
    		         minutes = atof (gets (buff));
    
    		printf ("\nEnter Stage Time 3:         \n\n", reg [minutes]);
                    	 minutes = atof (gets (buff));
    
    		printf ("\nEnter Stage Time 4:         \n\n", reg [minutes]);
                      	minutes = atof (gets (buff));
    
    		printf ("\nEnter Stage Time 5:         \n\n", reg [minutes]);
                      	minutes = atof (gets (buff));
    	
    		
    }
    
    	printf ("\nData Entry complete\n\nCreate data file to store data\n");
    	printf ("\n\nEnter filename (including path):");
    	gets (filename);
    	open = file_open (filename, "wb");
    	
    	if (open)
    	{  
    	//store data (number of items followed by the items)
    	//close file 
    		printf ("\n\nWriting data");
    		fwrite (&i, sizeof(int), 1, fp);
    	//	fwrite (&cost, sizeof (float), 1, fp);
    		printf ("\n\nItems written: %1d ", i);//%3d changed
    		
    		for (minutes=0; minutes<i; minutes++)
    		{
    			fwrite (&reg[minutes], sizeof (reg[minutes]), 1,fp);
    		}
    
    		for (minutes=0; minutes<i; minutes++)
    		{
    			for (month=0; month<=11; month++)
    			{
    				fwrite (&results [minutes] [month], sizeof (float),1,fp);
    			}
    		}
    		fclose (fp);
    		printf ("\n\nData stored and file %s closed", filename);
    	}
    	else
    	{
    		printf ("Could not save data!");
    	}
    }
    
    
    int Get_data (void)
    {
    	char filename [25];
    	int i,minutes,month, open=0;
    
    	printf ("\nReport\n\nRetrieve data from specified file\n");
    	printf ("\n\nEnter filename (including path):");
    	gets (filename);
    	open = file_open(filename, "rb");
    	if (open)
    	{
    		fread (&i, sizeof (i),1,fp);
    	//	fread (&cost, sizeof (cost),1,fp);
    		for (minutes=0; minutes<i; minutes++)
    			fread(&reg [minutes], sizeof (reg[minutes]),1,fp);
    			for (minutes=0; minutes<i; minutes++)
    			for (month=0; month<=11; month++)
    				fread (&results [minutes][month], sizeof (float),1,fp);
    			fclose (fp);
    			return (i);
    	}
    	else
    	{
    		printf ("\nCould not get data!");
    		return (0);
    	}
    }
    
    //	Displays the information in a table
    
    void report (int i)
    
    {
    	int  minutes, month;
    
    	
    	float grandtotal = 0.0;			/* Total of all times */
    	
    
    
    
    	//	print headings
    	//	spacings would not work if the Registration number was of variable length
    	printf ("\n\n\nReg      Stage1 Stage2 Stage3 Stage4 Stage5 Average");
    
    	for (minutes=0; minutes<i; minutes++)
    	{
    		printf ("\n%8s", reg[minutes]);
    		subtotal [minutes]=0;
    		for (month=0; month<=4; month++)
    
    		{
    			printf ("%4.0f",results[minutes][month]);
    			subtotal[minutes] = subtotal[minutes] + results [minutes][month];
    		}
    
    //		This gives the subtotal and the average at the end of the first table
    		average[minutes] = (subtotal [minutes] /(float) month);
    		grandtotal += subtotal[minutes];
    		printf ("%4.0f %4.0f", subtotal [minutes],average[minutes]);
    		}
    
    
    
    	
    		//	Displays grand total of mileage
    		printf ("\n\nTotal Time:%4.0f", grandtotal);
    
    
    		
    
    
    	//	This is the final table displaying results from calculations
    	
    	printf ("\n\n\n\nReg      Total ");
    
    	for (minutes=0; minutes<i; minutes++)
    	{
    		printf ("\n%8s", reg[minutes]);
    		printf ("%4.0f", subtotal[minutes]);
    
    	}		
    				
    }
    
    void main (void)
    //Displays and selects the Options in the Menu program
    
    {
    	char *Options [] = {"Enter data", "Display results", "Exit"};
    	int choice=0;
    	int size, minutes, i=0;
    
    	printf ("\nPlease insert data disk to store or retrieve data\n");
    
    	for (minutes=0;minutes<=i;minutes++);
    	
    	do
    	{ 
    	choice = menu (Options, 2);
    	printf("%d", choice);
    	if (choice==1)
    	{
    		printf ("\nEnter data\n\n");
    		input();
    		printf ("\n\nEntry Complete\n");
    	}
    	if (choice==2)
    	{
    		printf ("\nProducing report\n");
    		size = Get_data();
    		report(size);
    	}
    	}while (choice!=3);
    	printf ("\n\nThank you, remove your data disk now\n\n");
    }
     
    Last edited by a moderator: Jan 26, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    This is your assignment, not ours. I doubt that a person that can't be bothered to check the "Before you make a query" thread can be expected to complete an assignment which they have put off ad infinitum, because of their busy schedule conducting panty raids. You are certainly wecome to try. Post your attempts, if you get around to them, and we'll try to explain any errors, presuming that you can be bothered to delineate them intelligently.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    This forum is not for assignment help but is for help on your assignment.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice