if ( CurrentDate < 0 || Day < 0 || Month < 0 || Year < 0 )
{
cout << "Please enter positive numbers" << endl;
return 0;
}
cout << "The original YYYYMMDD date of " << CurrentDate << " has been converted to MM / DD / YY format below" <<endl;
cout << Month << "/" << Day << "/" << Year << endl;
#include <iostream>
#include <limits>
#include <string>
int main()
{
constexpr size_t LENGTH{ 8 };
int Day{}, Month{}, Year{}, currentDate{};
//std::string sCurrentDate{ "20190905" }; // <--- Set up for testing.
std::string sCurrentDate;
std::cout << "\n Please enter a date in YYYYMMDD format: ";
std::cin >> sCurrentDate; // <--- Put a comment on this line for testing.
if (sCurrentDate.size() < LENGTH || sCurrentDate.size() > LENGTH)
std::cout << "\n Error message\n" << std::endl;
else
currentDate = stoi(sCurrentDate);
Day = currentDate % 100;
Month = (currentDate / 100) % 100;
Year = (currentDate / 10000);
if (currentDate < 0 || Day < 0 || Month < 0 || Year < 0)
{
std::cout << "\n Please enter positive numbers" << std::endl;
return 0;
}
std::cout << "\n The original YYYYMMDD date of " << currentDate << " has been converted to MM / DD / YYYY format below\n\n ";
std::cout << (Month < 10 ? "0" : "") << Month << "/" << (Day < 10 ? "0" : "") << Day << "/" << Year << std::endl;
// The next line may not be needid. 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;
}