Hello ddaniel10,
As I worked with your program I found:
"fstream" will include the header files "ifstream" and "ofstream" therefore in "main" you could write the line
ifstream file;
and then in the open statement you would not need the "ios::in" to describe it as an input file.
Including the program inside an if/else statement may work, but does not look very nice. your if statement would work better as:
1 2 3 4 5 6 7 8
|
if (!file)
{
std::cout << "your error message" <<std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1); // <--- Because there is no reason to continue. The (1) means there is a problem.
}
// <--- Continue with rest of program.
|
"system("pause"); should be avoided. It is OK for your own personal use, but not a good choice for production code. I use this in place of "system("pause")":
1 2 3 4 5 6
|
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
|
This next part refers to the code below.
First I changed the return type to "void" because there is nothing to return. Also removed the bool from "main" because I was using old version. In your latest version line 12 has your prototype as an "extern" and it should not be an "extern".
I removed the if/else statement because by the time you reach this function the file stream should be open and checked. If it had failed you should never have reached this part. This would be different if you had opened the file in the function.
In the "cout" statement to print the header line, starting at line 11, the "setw()" must come before what it needs to control. Coming after as you have it has no effect.
On line 26 I used "stoi" from the "string" class to convert the string to an "int". Starting at line 42 I used "stod" to convert the string to a "double".
Lines 36 and 38 I used to convert the string to a "char". This could have been done with:
1 2
|
file >> person.sector;
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>
|
You need the second line to remove the "\n" from the input buffer before the next "getline". This also shows you how to get the first character from a string.
Line 48 is there to give you two decimal places in your output. "std::showpoint" allows you to print ".00" if needed.
Lines 52 to 71 will give you an idea of how I set up the output.
Lines 58 and 59, which work with lines 55 and 56, have an extra bit of code to add a zero to the date so it looks more uniform in the output. I had to change the "int" to a string before I could use the ternary operator which did not like adding an "int" to a string, but adding two strings together is not a problem.
Anything that you do not understand let me know.
Hope that helps,
Andy
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
|
void DisplayLogFile_CSV(std::ifstream& file)
{
WaterLog person; // one data record
std::string sectorlog; //holds string for sector
std::string input; //holds input of file as string
std::string month, day;
int number;
double avg{}, total{};
//display report header
std::cout << std::left << std::setw(15) << "NAME"
<< std::setw(12) << " ID" // <--- Sometimes adding spaces is easier to start with to get everything to line up. "stew" can be changed later.
<< std::setw(9) << " DATE"
<< std::setw(6) << "SEC"
<< std::setw(10) << "MONTH_1"
<< std::setw(10) << "MONTH_2"
<< std::setw(10) << "MONTH_3"
<< std::setw(10) << " TOTAL"
<< std::setw(10) << " AVERAGE" << std::endl;
std::getline(file, input, '\n');
std::cout << std::endl;
//until reach end-of-file, read and display each record
while (std::getline(file, input, ','))
{
person.year = stoi(input);
std::getline(file, input, ',');
person.month = stoi(input);
std::getline(file, input, ',');
person.day = stoi(input);
std::getline(file, person.name, ',');
std::getline(file, person.ID, ',');
std::getline(file, sectorlog, ',');
person.sector = sectorlog[0];
std::getline(file, input, ',');
person.firstM = stod(input);
std::getline(file, input, ',');
person.secondM = stod(input);
std::getline(file, input);
person.thirdM = stod(input);
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Sets the floating point numbers to output two decimal places.
std::cout << std::left << std::setw(15) << person.name; // <--- "std::left" to put the strings to the left side.
std::cout << std::setw(8) << person.ID << ' ';
month = std::to_string(person.month); // <--- Changes the "int" to a string for the next "cout" to work.
day = std::to_string(person.day); // <--- Changes the "int" to a string for the next "cout" to work.
std::cout << (person.month < 10 ? "0" + month : month) << '/'
<< (person.day < 10 ? "0" + day : day)
<< '/' << person.year;
std::cout << std::right << std::setw(3) << ' ' << person.sector; // <--- "std::right" to put the numbers to the right so the decimal points line up.
std::cout << std::setw(10) << person.firstM
<< std::setw(10) << person.secondM
<< std::setw(10) << person.thirdM << " ";
avg = (person.firstM + person.secondM + person.thirdM) / 3;
total = person.firstM + person.secondM + person.thirdM;
std::cout << std::setw(10) << total << ' ' << std::setw(10) << avg << std::endl;
}
}
|
NAME ID DATE SEC MONTH_1 MONTH_2 MONTH_3 TOTAL AVERAGE
Franklin 000-N01 03/03/2017 N 15.60 198.20 469.01 682.81 227.60
Watson 387-W99 03/28/2017 E 178.30 243.07 719.46 1140.83 380.28
Newton 297-E22 03/27/2017 N 148.20 285.18 702.50 1135.88 378.63
Beelzebub 666-S77 03/28/2017 S 2666.66 1666.66 666.66 4999.98 1666.66
Ford 123-W99 04/01/2017 W 234.90 709.10 856.43 1800.43 600.14
Cray 621-S32 04/02/2017 S 290.70 460.67 711.47 1462.84 487.61
Moore 553-E17 03/27/2017 E 310.00 393.10 248.36 951.46 317.15
Bell 111-N02 03/29/2017 N 38.90 157.12 100.14 296.16 98.72
Marconi 201-E33 03/30/2017 E 49.00 556.84 91.71 697.55 232.52
Thompson 793-S39 03/30/2017 S 199.20 508.70 397.77 1105.67 368.56
Press Enter to continue
|