Hi, I need help with a part of C++ formatting. I need to input a year, such as 2004, and display it in two ways: 2004 and 04
Most of the formatting I've tried hasn't worked at all... setw only sets a minimum, setprecision will apparently only work correctly with decimals... etc. So yeah, any help would be appreciated, thanks.
(by the way, the year can be input as anything from 1900 to 2010)
#include <cstdlib>
#include <iostream>
usingnamespace std;
int display(int y)
{
if (y < 2000) // If the number is over 2000, it takes 2000 off.
{
y = y - 1900;
}
else // If the number is not over 2000, it takes 2000 off.
{
y = y - 2000;
}
return y;
}
int main(int argc, char *argv[])
{
cout << "Enter a year:" << endl;
int year, yeary;
cin >> year;
if (year <= 2099 && year >= 1900) // If the year is valid
{
cout << year << " or ";
yeary = display(year); // Calls the function above
if (yeary < 10) // Explained below
{
cout << "0" << yeary << endl;
}
else
{
cout << yeary << endl;
}
}
else
{
cout << "Year not valid" << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
You should notice that i tested to see if the number was under 10, if it is under 10 it displays a 1 digit number, so you i made it stick a 0 in front of it.