I attempted to write a program for this assignment:
https://filebox.ece.vt.edu/~ece1574/fall14/project4.html
I am running into an issue where when I search the array for an item that should be there, I am always returning that the item wasn't found... please help!
Here is the main file:
#include "runner.h"
using namespace std;
void runRace(istream& in, ostream& out)
{
string filename, command;
int count = 0;
int size = 10;//this is how much I can store
Runner* runners = new Runner[size];
in.ignore(200, ':');
in >> filename;
readRunners(runners, count, size, filename);
sorter(runners, count);
in >> command;
while (!in.fail())
{
if (command == "find-name")
{
string findName;
in >> findName;
name(runners, count, in, out, findName);
}
else if (command == "find-place")
{
int findPlace;
in >> findPlace;
place(runners, count, in, out, findPlace);
}
else if (command == "find-bib")
{
int findBib;
in >> findBib;
bib(runners, count, in, out, findBib);
}
in >> command;
}
}
void readRunners(Runner runners[], int &count, int& size, string filename)
{
ifstream in(filename.c_str());
int hour, minute, seconds;
string colon, colon2, firstName2;
char c;
Runner R;
count = 0;
in >> R.lastName;
in.ignore(20, ',');
in >> R.firstName;
c = in.peek();
if (isspace(c))
{
in >> firstName2;
R.firstName = R.firstName + firstName2;
}
in.ignore(200, ',');
in >> R.bib;
in.ignore(200, ',');
in >> R.gender;
in.ignore(200, ',');
in >> hour >> colon >> minute >> colon2 >> seconds;
R.clockTime = hour * 3600 + minute * 60 + seconds;
hour, minute, seconds = 0;
colon, colon2 = "";
in.ignore(200, ',');
in >> hour >> colon >> minute >> colon2 >> seconds;
R.chipTime = hour * 3600 + minute * 60 + seconds;
while (!in.fail())
{
if (count < size)
{
runners[count] = R;
count++;
}
else if (count == size)
{
grower(runners, size);
runners[count] = R;
count++;
}
in >> R.lastName;
in.ignore(200, ',');
in >> R.firstName;
c = in.peek();
if (isspace(c))
{
in >> firstName2;
R.firstName = R.firstName + firstName2;
}
in.ignore(200, ',');
in >> R.bib;
in.ignore(200, ',');
in >> R.gender;
in.ignore(200, ',');
in >> hour >> colon >> minute >> colon2 >> seconds;
R.clockTime = hour * 3600 + minute * 60 + seconds;
hour, minute, seconds = 0;
colon, colon2 = "";
in.ignore(200, ',');
in >> hour >> colon >> minute >> colon2 >> seconds;
R.chipTime = hour * 3600 + minute * 60 + seconds;
}
}
void grower(Runner* &runners, int &size)
{
Runner * temp = new Runner[size * 2];
for (int i = 0; i < size; i++)
temp[i] = runners[i];
size *= 2;//size = size * 2;
delete[] runners;//give back x's memory
runners = temp;
temp = NULL;
}
void sorter(Runner runners[], int count)
{
for (int L = 0; L < count - 1; L++)
{
for (int j = L + 1; j < count; j++)
{
if (runners[L].chipTime > runners[j].chipTime)//this is ascending order
{
//swap
Runner temp = runners[L];
runners[L] = runners[j];
runners[j] = temp;
}
}
}
for (int k = 0; k < count; k++)
{
runners[k].place = k + 1;
}
}
void name(Runner runners[], int count, istream& in, ostream& out, string& findName)
{
bool found = false;
for (int m = 0; m<count; m++) // loop through all records on "runners"
{
if (runners[m].lastName == findName) // anytime match found
{
found = true;
out << "find-name " << findName << endl;
out << setw(4) << runners[m].place << runners[m].lastName << ", " << runners[m].firstName << " " << runners[m].bib << " " << runners[m].gender << " ";
int hours = runners[m].clockTime / 3600; //1
int remainder = runners[m].clockTime % 3600; // 65
int minutes = remainder / 60; //1
minutes = remainder / 60; //1
int seconds = remainder % 60; //5
out << hours << ":" << minutes << ":" << seconds << " ";
hours, minutes, seconds, remainder = 0;
hours = runners[m].chipTime / 3600; //1
remainder = runners[m].chipTime % 3600; // 65
minutes = remainder / 60; //1
minutes = remainder / 60; //1
seconds = remainder % 60; //5
out << hours << ":" << minutes << ":" << seconds << endl;
}
}
if (!found)
{
out << "Sorry, no runners with last name " << findName << " were found." << endl;
}
}
void place(Runner runners[], int count, istream& in, ostream& out, int& findPlace)
{
bool found = false;
for (int n = 0; n<count; n++) // loop through all records on "runners"
{
if (runners[n].place == findPlace) // anytime match found
{
found = true;
out << "find-place " << findPlace << endl;
out << setw(4) << runners[n].place << runners[n].lastName << ", " << runners[n].firstName << " " << runners[n].bib << " " << runners[n].gender << " ";
int hours = runners[n].clockTime / 3600; //1
int remainder = runners[n].clockTime % 3600; // 65
int minutes = remainder / 60; //1
minutes = remainder / 60; //1
int seconds = remainder % 60; //5
out << hours << ":" << minutes << ":" << seconds << " ";
hours, minutes, seconds, remainder = 0;
hours = runners[n].chipTime / 3600; //1
remainder = runners[n].chipTime % 3600; // 65
minutes = remainder / 60; //1
minutes = remainder / 60; //1
seconds = remainder % 60; //5
out << hours << ":" << minutes << ":" << seconds << endl;
}
}
if (!found)
{
out << "Sorry, no runners with place " << findPlace << " were found." << endl;
}
}
void bib(Runner runners[], int count, istream& in, ostream& out, int& findBib)
{
bool found = false;
for (int p = 0; p<count; p++) // loop through all records on "runners"
{
if (runners[p].bib == findBib) // anytime match found
{
found = true;
out << "find-bib " << findBib << endl;
out << setw(4) << runners[p].place << runners[p].lastName << ", " << runners[p].firstName << " " << runners[p].bib << " " << runners[p].gender << " ";
int hours = runners[p].clockTime / 3600; //1
int remainder = runners[p].clockTime % 3600; // 65
int minutes = remainder / 60; //1
minutes = remainder / 60; //1
int seconds = remainder % 60; //5
out << hours << ":" << minutes << ":" << seconds << " ";
hours, minutes, seconds, remainder = 0;
hours = runners[p].chipTime / 3600; //1
remainder = runners[p].chipTime % 3600; // 65
minutes = remainder / 60; //1
minutes = remainder / 60; //1
seconds = remainder % 60; //5
out << hours << ":" << minutes << ":" << seconds << endl;
}
}
if (!found)
{
out << "Sorry, no runners with bib " << findBib << " were found." << endl;
}
}
Here's the header:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using std::cin;
using std::cout;
using std::istream;
using std::ostream;
using std::string;
struct Runner
{
int place;
string lastName;
string firstName;
int bib;
string gender;
int clockTime;
int chipTime;
};
void runRace(istream& in, ostream& out);
void grower(Runner* &runners, int &size);
void sorter(Runner runners[], int count);
void readRunners(Runner runners[], int &count, int& size, string filename);
void name(Runner runners[], int count, istream& in, ostream& out, string& findName);
void place(Runner runners[], int count, istream& in, ostream& out, int& findPlace);
void bib(Runner runners[], int count, istream& in, ostream& out, int& findBib);