good day need a answer from problem solving with c++ 9th edition (world edition)
ID: 3848114 • Letter: G
Question
good day need a answer from problem solving with c++ 9th edition (world edition). programming projects- 2 p642
the question as follows
a marathon society wants a c++ programto keep a runner's records.It wants to store names of tournaments and then the names of each runner,lenght of the marathon,and time spent to finish each cycle.The record should help the community to produce statistics about each run.The statistics is concerned with data on the time taken to finish the race such as the longest time and shortest time and average timefor all runners.The class should be able to store the outstanding record of all tournaments consisting of the name of the runner who made a record and the time taken.Whenever a new runner happens to break a record of a given tournament,the program should note this,and produce an alert that the given record has been broken.It should display the name of the record-breaker together with the new record made.
have tried it,but not sure how to approach the question..is this a file question ?
can you please assist
Explanation / Answer
Hello
Coming to your first question, yes this is a file handling question.
Since you said to assist me, I am going to walkthrough the coding flow but not the code itself.
First of all, create a class "marathon" and instantiate following variables and methods. class member functions we require are
marathon:marathon()
{//leave it empty because nothing need to be initiated after class creation
}
marathon::~marathon()
{//leave it empty for some advanced reasons
}
void marathon:::enter_tournament_details()
{
//ask for tournament name and number of runners participating in tournament and store them in defined variables.
while(no_of_runners--)//loops no_of_runners times
{
//take the input from the users into variables runner_name, lenght and time and pass them to Insert_record() function
Insert_record(tournment_name,runner_name,length,time_in_seconds) ;
}
record_break_check()//check for any record break
}//end enter_tournament_details()
void insert_record(tounament_name,runner_name,length,time_in_seconds)
{
//open the file in append mode
std::ofstream fileptr("marathondata.dat", std::ios_base::app | std::ios_base::out);
fileptr<<tournament_name+','+runner_name','+','+length+','+time_in_seconds+' ';//comma acts as delimiter at the time of read
fileptr.close()//close the file
}//end this function
int marathon::shortest_run(tournament_name)
{
ifstream filefptr("marathondata.dat")// open to read a file
initiate a string "line"
while (getline( fileptr, line )) // get every line until you reach the end of the file and this line will be stored in the string "line"
{
// some how bring out the string after first comma and if it is equal to tournament_name continue to next step, else do nothing
//some how bring out the string after last comma and convert into integer using stoi()
//do the above for record in the file and find the min in all of them.
}
fileptr.close();
return min;
}//end the function
int marathon::average_run(tournament_name)
{
ifstream filefptr("marathondata.dat")// open to read a file
initiate a string "line"
int sum=0,count=0;
while (getline( fileptr, line )) // get every line until you reach the end of the file and this line will be stored in the string "line"
{
// some how bring out the string after first comma and if it is equal to tournament_name continue to next step, else do nothing
//some how bring out the string after last comma and convert into integer using stoi() and add it to sum
count++ //increase the count by one
}
int average = sum/count;
fileptr.close();
return average;
}//end the function
int marathon::longest_run(tournament_name)
{
ifstream filefptr("marathondata.dat")// open to read a file
initiate a string "line"
//intiate variables to store the record breaker details
string record_tournment_name,record_runner_name;
int record_length,record_ time_in_seconds.
//open a new file to store record details of every tournament
ofstream fileptr1("marathonrecord.dat", std::ios_base::app | std::ios_base::out);
//From all lines bring out the record with maximum time_in_seconds having tournament_name equal to that of tournament name passed in the arguments of the function.
//now write the maximum record into file marathonrecord.dat file as in insert_record()
fileptr1.close()
fileptr.close();
return max;
}//end the function
Now most of the part is done except record break checking function
for that we use longest_run funciton
void record_break_checker(tournament_name)
{
ifstream filefptr("marathonrecords.dat")// open to read a file
initiate a string "line"
//intiate variables to store the record breaker details for that tournament
string record_tournment_name,record_runner_name,present_tournment;
int record_length,record_time_in_seconds.
while (getline( fileptr, line )) // get every line until you reach the end of the file and this line will be stored in the string "line"
{
// some how bring out the string after first comma and if it is equal to tournament_name continue to next 5 steps, else do nothing and read newline.
//store the first part of line in record_tournment_name
//store second part of line in ,record_runner_name;
//extract third part convert into integer using stoi() and store it in record_length
//extract last part of the line and convert into integer using stoi()
Store it in record_time_in_seconds
//break
}
ifstream filefptr("marathondata.dat")// open to read a file
initiate a string "line"
while (getline( fileptr, line )) // get every line until you reach the end of the file and this line will be stored in the string "line"
{
// some how bring out the string after first comma and if it is equal to tournament_name continue to next 5 steps, else do nothing and read newline.
//store the first part of line in present_tournament;
//store second part of line in , runner_name;
//extract third part convert into integer using stoi() and store it in length
//extract last part of the line and convert into integer using stoi()
Store it in time_in_seconds
//if(record_tournament_name==presnt_tournament and record_runner_name==runner_name and record_length==length and recird_time_in_seconds==time_in_seconds)
Print record details
return
//break
}
fileptr.close();
****NOTE: This does not cover error handling such as filedeosnotexist exception and main function******
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.