C++ Formatting Help

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)
1
2
string year = atoi(2008);
cout << year.substr(2, 2) << endl;
hmm im new to c++, but i'll try and help.
I checked it on my compiler, it seems to work.
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
#include <cstdlib>
#include <iostream>

using namespace 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.
If you can impove this code please tell me how =)
I'm only 2 weeks old in c++ and still learning.
That helped alot and I got it working, thanks!
Topic archived. No new replies allowed.