Hello AshtoKream,
To answer your final question. Yes, that is what I did.
What I did was to create a struct I called "Date".
The structs look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
struct Date
{
size_t s_month{};
size_t s_day{};
size_t s_year;
};
struct Stock
{
std::string date;
Date sDate;
double open{};
double high{};
double low{};
double close{};
double adjClose{};
double volume{};
double percentChange{};
};
|
In "main" I added the variable
std::string tempDate
then changed the while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
while (std::getline(myFile, tempDate, ','))
{
std::istringstream iss(tempDate);
iss >> temp.sDate.s_month >> junk >> temp.sDate.s_day >> junk >> temp.sDate.s_year;
temp.sDate.s_month < 10 ? temp.date = std::to_string(0) + std::to_string(temp.sDate.s_month) : temp.date = std::to_string(temp.sDate.s_month);
temp.date += '/';
temp.sDate.s_day < 10 ? temp.date += std::to_string(0) + std::to_string(temp.sDate.s_day) : temp.date += std::to_string(temp.sDate.s_day);
temp.date += '/';
temp.date += std::to_string(temp.sDate.s_year);
|
In the end it makes the output look nicer. One of my little pet peeves.
After reading the rest of the line I used an if/else statement to figure the percent change. I will admit I am not sure if this is correct or even the best way to accomplish this task, but it does seem to work, if I understand the formula correctly.
1 2 3 4 5 6
|
if (temp.close - temp.open > 0)
temp.percentChange = (((temp.close - temp.open) / temp.open) * 100);
else
temp.percentChange = -((temp.open - temp.close) / temp.open) * 100;
|
After a thought and testing I found that line 3 is all you should need. I started with the if/else statements because the formulas for gain and loss are different. So far the formula for gain will word both ways.
I created this function to print a report based on the range of dates. I do not know what the full input file looks like, but you might want a variable for month.
This is something you can use as an example to work from:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void Report(std::vector<Stock> data, size_t sRange, size_t eRange)
{
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << std::setw(28) << ' ' << "Percent" << std::endl;
std::cout << " Date Open Close Change" << std::endl;
for (auto& lc : data)
{ // Date Open Close Percent Change
if (lc.sDate.s_day >= sRange && lc.sDate.s_day <= eRange)
std::cout << std::setw(10) <<lc.date
<< std::setprecision(2) << ' ' << lc.open << " "
<< lc.close << " " << std::setw(5) << std::setprecision(2) << lc.percentChange << " %" << std::endl;
}
}
|
Most of the time I just need the setprecision of line 4.
I put the "setprecision"s on line 14 and 15 because I once changed line 15 to 4 and needed line 14 to set it back to 2.If everything will contain the same number of decimal places then all you need is line 4 and you can loose the "setprecision"s from lines 14 and 15.
Just remember the "setw" on line 15 is for two decimal places. If you change the "setprecision" you will need to increase the "setw' by the same amount.
Hope that helps,
Andy