Question
can you break my program into 3 functions besides the main function? Can you also fix the coding part for empty File part for me
can you break my program into 3 functions besides the main function?
Can you also fix the coding part for empty File part for me please?
#include
#include
#include
using namespace std;
// size of various arrays
const int MAX_NAME_LENGTH = 21;
const int MAX_ACTIVITES = 365;
const int MAX_PARTICIPANTS = 40;
const int NUM_ACITIVITIES = 27;
// Activity codes
const int ASSEMBLY_LINE = 0;
const int BASKETBALL_SHOOTING = 1;
const int BASKETBALL_GAME = 2;
const int BICYCLING_10MPH = 3;
const int BICYCLING = 4;
const int COOKING = 5;
const int DANCE = 6;
const int FOOTBALL = 7;
const int HIKING = 8;
const int HOUSE_CLEANING = 9;
const int GARDENING = 10;
const int MINIATURE_GOLF = 11;
const int RACQUETBALL = 12;
const int ROWING = 13;
const int RUNNING_6MPH = 14;
const int RUNNING_7MPH = 15;
const int RUNNING_8MPH = 16;
const int SHOPPING = 17;
const int SOCCER = 18;
const int SOFTBALL = 19;
const int STAIRS = 20;
const int SWIMMING_LAPS = 21;
const int TENNIS = 22;
const int WALKING_3MPH = 23;
const int WALKING_4MPH = 24;
const int WEIGHT_LIFTING = 25;
const int YOGA = 26;
// steps associated with each activity
const int ACTIVITY_STEPS[NUM_ACITIVITIES] = {85, 136, 242, 121, 242, 61, 167,
242, 182, 91, 99, 91, 212, 212,
303, 348, 409, 70, 212, 152, 273,
212, 212, 100, 152, 121, 76};
// participant structure
struct Participant {
char firstName[MAX_NAME_LENGTH];
char lastName[MAX_NAME_LENGTH];
double height;
int numActivities;
int activities[MAX_ACTIVITES];
int minutes[MAX_ACTIVITES];
double totalMiles;
};
int main()
{
// file name
char inputFileName[MAX_NAME_LENGTH]; // I've assumed file name length
// print the welcome message
cout << "Welcome to the PSCC Fitness Tracker." << endl;
// prompting user for file name
cout << "Please enter the name of input file: ";
cin >> inputFileName; // read file name
// opening the input file
ifstream inputFile;
inputFile.open(inputFileName);
// if file does not exist
if (inputFile.fail())
{
cout << "File " << inputFileName << " does not exit. Please contact the administrator." << endl;
cout<<"Thank you for using the PSCC Fitness Tracker."< return 0; } // if file is empty if (inputFile.eof()) { cout << "File " << inputFileName << " is empty. Please contact the administrator." << endl; cout<<"Thank you for using the PSCC Fitness Tracker."< return 0; } // participants array struct Participant participants[MAX_PARTICIPANTS]; int pCount = 0; // count of participants in the array while (inputFile >> participants[pCount].firstName) // reading first name, if end of file reached it wouldn't loop { // reading last name inputFile >> participants[pCount].lastName; // reading height inputFile >> participants[pCount].height; // reading number of activities inputFile >> participants[pCount].numActivities; // for each activity reading it's code and minutes for (int j = 0; j < participants[pCount].numActivities; j++) { inputFile >> participants[pCount].activities[j]; inputFile >> participants[pCount].minutes[j]; } // incrementing the array count pCount++; } // calculating total miles for each for individual for (int i = 0; i < pCount; i++) { int steps = 0; for (int j = 0; j < participants[i].numActivities; j++) { steps += participants[i].minutes[j]*ACTIVITY_STEPS[participants[i].activities[j]]; } // calculating distance in feets according the feet of each individual double feet_distance = ((0.413*participants[i].height)/12)*steps; // converting feets to miles participants[i].totalMiles = feet_distance/5280; } // sorting the array of participants in descending order of total miles for (int i = 0; i < pCount-1; i++) // using simple bubble sort { for (int j = 0; j < (pCount - i); j++) { if (participants[j].totalMiles < participants[j+1].totalMiles) { struct Participant temp = participants[j]; participants[j] = participants[j+1]; participants[j+1] = temp; } } } // printing the results double total = 0.0; // total miles of all participants cout << " Tracking Results" << endl; cout << setw(42) << left << "Name" << "Miles" << endl; cout <<"-----------------------------------------------------------" << endl; for (int i = 0; i < pCount; i++) { cout << setw(MAX_NAME_LENGTH) << left << participants[i].firstName; cout << setw(MAX_NAME_LENGTH) << left << participants[i].lastName; cout << setw(8) << setprecision(2) << fixed << participants[i].totalMiles << endl; total += participants[i].totalMiles; } cout <<" -----------------------------------------------------------" << endl; cout << setw(42) << left << " TOTAL" << setprecision(2) << fixed << total << endl; // goodbye message cout << "Thank you for using the PSCC Fitness Tracker." << endl; inputFile.close(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started