-
Anyone Look At This For Us . . . C++ Problem
Just stuck on a particular question, i have to write code for this problem if anyone can help . . . .
The organiser of a local classic 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 organiser would also like the winner (the driver with the lowest time) to be identified automatically by the computer system.
-
there are built in time manipulation functions in c++. do a search in your help on 'time'
-
Dont recomend using this, but i was bored and i had ago at the first part of the program. Do not copy it as it has bugs, but you could write somethign similar to this.
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main()
{
ofstream write;
write.open("compdetails.txt");
int racer = 1;
string name;
string make;
string model;
string eSize;
string regNo;
do
{
cout << "Enter Competitor Details (quit to end)" << endl;
cout << "Name: ";
getline (cin,name);
if ( name == "quit" )
{
break;
}
cout << "Make: ";
getline (cin,make);
cout << "Model: ";
getline (cin,model);
cout << "Engine Size: ";
getline (cin,eSize);
cout << "Registration No.: ";
getline (cin,regNo);
cout << endl;
write << "Racer: " << racer << "\nName: " << name << endl;
write << "Make: " << make << "\nModel: " << model << endl;
write << "Engine Size: " << eSize << "\nRegistration No.: " << regNo << "\n" << endl;
racer++;
} while(name, make, model, eSize, regNo != "quit");
write.close();
system("PAUSE");
}
Bugs:
When writing to the text file, it screws up like this:
Racer: 1
Name: Ray Andrews
Make: Ford Escort
Model: MkII
Engine Size: 1600
Registration No.: HGY 273M
Racer: 2
Name:
Make: John Hill
Model: Opel
Engine Size: Manta
Registration No.: 2000
Racer: 3
Name: TBD 498F
Make: Andrew Smith
Model: Austin
Engine Size: Mini Cooper
Registration No.: 1300
Racer: 4
Name: DVT 749G
And also doesnt quit properly when you type quit.
Will :D
-
Can Anyone finish it off ? ? ? ? ?
Ive managed to do most code i think, needs some looking at at going over if anyone could help ?
#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 (®[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(® [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");
}
Similar Threads
-
By Irina in forum ASP.NET
Replies: 0
Last Post: 11-29-2002, 10:47 PM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Roseta in forum VB Classic
Replies: 0
Last Post: 11-14-2001, 03:24 AM
-
By Ayman in forum VB Classic
Replies: 0
Last Post: 04-03-2000, 01:08 AM
-
By Jason Bock in forum VB Classic
Replies: 0
Last Post: 03-21-2000, 06:48 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks