Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following is the code I have. When I put the file in nothing come out after typing the file name. What is happening? Thanks

The following is the code I have. When I put the file in nothing come out after typing the file name. What is happening?

Thanks

#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, 345, 212, 321, 182, 136};

// 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; };

// Function to load participants from a file int loadParticipants(char inputFileName[], Participant participants[]) { ifstream inputFile; inputFile.open(inputFileName);

int pCount = 0; while (inputFile >> participants[pCount].firstName) { inputFile >> participants[pCount].lastName; inputFile >> participants[pCount].height; inputFile >> participants[pCount].numActivities; for (int j = 0; j < participants[pCount].numActivities; j++) { inputFile >> participants[pCount].activities[j]; inputFile >> participants[pCount].minutes[j]; } pCount++; } return pCount; }

// Function to calculate total miles for each participant void calculateTotalMiles(Participant participants[], int pCount) { 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]]; } double feet_distance = ((0.413 * participants[i].height) / 12) * steps; participants[i].totalMiles = feet_distance / 5280; } }

// Function to sort participants by total miles void sortParticipants(Participant participants[], int pCount) { for (int i = 0; i < pCount - 1; i++) { for (int j = 0; j < (pCount - i); j++) { if (participants[j].totalMiles < participants[j + 1].totalMiles) { Participant temp = participants[j]; participants[j] = participants[j + 1]; participants[j + 1] = temp; } } } }

// Function to display the results void displayResults(Participant participants[], int pCount) { cout << setw(20) << "Name" << setw(10) << "Height" << setw(15) << "Total Miles" << endl; cout << "--------------------------------------------------------" << endl; for (int i = 0; i < pCount; i++) { cout << setw(10) << participants[i].firstName << " " << setw(10) << participants[i].lastName << setw(10) << participants[i].height << setw(10) << fixed << setprecision(2) <

// Main function 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.get() == EOF)

{ cout << "File " << inputFileName << " does not exit. Please contact the administrator." << endl; return 1; }

// if file is empty if (inputFile.peek() == ifstream::traits_type::eof()) { cout << "File " << inputFileName << " is empty. Please contact the administrator." << endl; return 1; }

// if file is empty if (inputFile.peek() == ifstream::traits_type::eof()) { cout << "File " << inputFileName << " is empty. Please contact the administrator." << endl; return 1; }

// 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 << "\t" << setw(40) << left << "Name" << "Miles" << endl; cout << "\t-----------------------------------------------------------" << endl; for (int i = 0; i < pCount; i++) { cout << "\t" << 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 << "\t============================================================" << endl; cout << "\t===================================" << endl; cout << setw(30) << 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Prove that u - v2 = u2 + v2 - 2u v.

Answered: 1 week ago