1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <string>
#include <fstream>
#include <limits> // For std::numeric_limits
struct playerData
{
std::string pName;
std::string tName;
double points;
double rebounds;
double assists;
};
int countRecords(std::ifstream&, std::string, bool&);
void readFile(std::ifstream&, playerData*, std::string, int);
void sort(playerData*, int);
void writeFile(std::ofstream&, playerData*, std::string, int);
int main()
{
std::ifstream inFile;
std::string inFileName{ "players.txt" };
bool goodRead{}; // Used to set the flag for data validation in the file.
int size = countRecords(inFile, inFileName, goodRead);
std::ofstream outFile;
std::string outFileName{ "sortedPlayers.txt" };
if (goodRead) // if there are no data validation issues, then proceed.
{
std::cout << "There are " << size << " records in " << inFileName << std::endl;
// Allocate the memory based on the count of records.
playerData *pList = nullptr;
pList = new playerData[size];
readFile(inFile, pList, inFileName, size);
sort(pList, size);
writeFile(outFile, pList, outFileName, size);
delete [] pList;
pList = nullptr;
}
else // Notify user of the first data validation fault is found.
{
std::cout << "Please, verify the validation data on " << inFileName << std::endl;
}
}
/*
This functions will open the reading file to validate the requested
data validation and count the number of records. If there is a violation of the
data validation, then the goodRead will be set to false and will notify the user
of the first data violation found.
*/
int countRecords(std::ifstream& inFile, std::string inFileName, bool &goodRead)
{
inFile.open(inFileName);
playerData temp;
goodRead = true;
int counter = 0;
if (!inFile)
{
std::cout << "Error opening " << inFileName << std::endl;
goodRead = false;
}
else
{
while (std::getline(inFile, temp.pName) && goodRead)
{
std::getline(inFile, temp.tName);
inFile >> temp.points;
if (inFile.fail())
{
inFile.clear();
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error reading points from "
<< temp.pName << std::endl;
goodRead = false;
}
inFile >> temp.rebounds;
if (inFile.fail())
{
inFile.clear();
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error reading rebounds from "
<< temp.pName << std::endl;
goodRead = false;
}
inFile >> temp.assists;
if (inFile.fail())
{
inFile.clear();
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error reading assists from "
<< temp.pName << std::endl;
goodRead = false;
}
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
counter++;
}
inFile.close();
}
return counter;
}
void readFile(std::ifstream& inFile, playerData* pList, std::string inFileName, int size)
{
inFile.open(inFileName);
if (!inFile)
{
std::cout << "Error opening " << inFileName << std::endl;
return;
}
else
{
std::cout << "File " << inFileName << " open.\n";
for (int count = 0; count < size; count++)
{
std::getline(inFile, pList[count].pName);
std::getline(inFile, pList[count].tName);
inFile >> pList[count].points;
inFile >> pList[count].rebounds;
inFile >> pList[count].assists;
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
inFile.close();
std::cout << "File " << inFileName << " closed.\n";
}
}
void sort(playerData *pList, int size)
{
bool swap{};
playerData temp;
std::cout << "Sorting the records by players first name...\n";
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
// Sorted by alphabetical name.
// Then move the rest of the players
// information with the sorted data.
if (pList[count].pName > pList[count + 1].pName)
{
temp.pName = pList[count].pName;
pList[count].pName = pList[count + 1].pName;
pList[count + 1].pName = temp.pName;
temp.tName = pList[count].tName;
pList[count].tName = pList[count + 1].tName;
pList[count + 1].tName = temp.tName;
temp.points = pList[count].points;
pList[count].points = pList[count + 1].points;
pList[count + 1].points = temp.points;
temp.rebounds = pList[count].rebounds;
pList[count].rebounds = pList[count + 1].rebounds;
pList[count + 1].rebounds = temp.rebounds;
temp.assists = pList[count].assists;
pList[count].assists = pList[count + 1].assists;
pList[count + 1].assists = temp.assists;
swap = true;
}
}
} while (swap);
std::cout << "Sorting completed.\n";
}
void writeFile(std::ofstream& outFile, playerData* pList, std::string outFileName, int size)
{
outFile.open(outFileName);
std::cout << "File " << outFileName << " open.\n";
for (int count = 0; count < size; count++)
{
outFile << pList[count].pName << '\n'
<< pList[count].tName << '\n'
<< pList[count].points << '\n'
<< pList[count].rebounds << '\n'
<< pList[count].assists << '\n';
}
outFile.close();
std::cout << "File " << outFileName << " closed.\n";
}
|