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
|
#include <cstdlib> // c'mon! Use std::string instead
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
constexpr std::size_t MAX_FILENAME { 16 };
constexpr int MAX_MONTHS = 20; // max number of months to import per supervisor
char* askForFileName(char* const fname, const char* const info, std::size_t size);
int importRecords(const char* const fname, int max_recs, int* const records);
int calculateMinMaxAvg(const int* const records, int size,
int& min, int& max, double& avg);
void displayMinMaxAvg(int min, int max, double avg,
const int* const records, int size);
void sortArray(int* const records, int size);
int main()
{
char rerun {};
do {
std::cout << "This application will read the salary data from the file "
"and compute low, high, and average salary amounts.\n";
char fname[MAX_FILENAME];
askForFileName(fname, " - max characters: ", MAX_FILENAME - 1);
int records[MAX_MONTHS]; // use std::vector instead!
int reads = importRecords(fname, MAX_MONTHS, records);
int min {}, max {};
double average {};
calculateMinMaxAvg(records, reads, min, max, average);
sortArray(records, reads);
displayMinMaxAvg(min, max, average, records, reads);
std::cout << "Would you like to run this again (y/n) ?";
std::cin >> rerun;
} while (rerun == 'y' || rerun == 'Y');
std::cout << "Goodbye!\n";
return 0;
}
char* askForFileName(char* const fname, const char* const info, std::size_t size)
{
std::cout << "Please, enter the input file name" << info << size << ": ";
std::cin >> fname;
std::cin.ignore(1);
return fname;
}
int importRecords(const char* const fname, int max_recs, int* const records)
{
std::ifstream in_stream(fname);
if(!in_stream) { return 0; }
// First datum: number of existing records
int num_records;
in_stream >> num_records;
std::cout << "The file " << fname << " contains " << num_records
<< " months worth of salary data.\n";
if (num_records > max_recs)
{
std::cout << "File error.\nMaximum number of records to be imported: "
<< max_recs
<< ".\nPlease provide a different file name.\n";
return 0;
}
for(int i {}; i < num_records && in_stream >> records[i]; ++i) {}
in_stream.close(); // reduntant
return num_records; // assumes no errors inside file!!!
}
int calculateMinMaxAvg(const int* const records, int size,
int& min, int& max, double& avg)
{
double total {};
min = std::numeric_limits<int>::max();
max = std::numeric_limits<int>::min();
for (int i = 0; i < size; ++i)
{
total += records[i];
if (min > records[i]) { min = records[i]; }
if (max < records[i]) { max = records[i]; }
}
avg = total / static_cast<double>(size);
return 0; // for further improvements, e.g. returning error values
} // This is personal: you can safely make this function void
void displayMinMaxAvg(int min, int max, double avg,
const int* const records, int size)
{
std::cout.setf(std::ios::fixed);
std::cout.setf(std::ios::showpoint);
std::cout.precision(2);
std::cout << "The lowest monthly salary earned was $" << min
<< ".\nThe highest monthly salary earned was $" << max
<< ".\nThe average monthly salary earned was $" << avg
<< ".\n\nThe monthly salary data has been sorted in ascending "
"order for your review:\n";
for(int i {}; i < size; ++i) {
std::cout << records[i] << ", ";
}
std::cout << '\n';
}
void sortArray(int* const records, int size)
{
std::cout << "size: " << size << '\n';
for(int counter = size; counter > 0; --counter)
{
for (int index = 1; index < size; ++index)
{
if (records[index] > records[index + 1])
{
std::cout << "counter: " << counter << "; "
<< "swapping records[" << index + 1
<< "] (" << records[index + 1]
<< ") and record[" << index
<< "] (" << records[index] << ")\n";
int temp = records[index + 1];
records[index + 1] = records[index];
records[index] = temp;
}
} // end inner for-loop
size--;
} // end outer for-loop
}
|