-3

This is what I have so far. Everything works fine except for the fact that instead of displaying 06 or 07 it displays only 6 or 7. And the year only works properly above 2000. Under it displays three integers instead of two. Like for 1990 it would show 990 instead of 90.

#include <conio.h>
#include <string>
#include <iostream>
#include <cctype>
using namespace std;

bool GetDate(int* dptr, int* mptr, int* yptr)
{
    string date, day, month, year;

    cout << "\n Please enter a date: ";
    getline(cin, date);

    month = date.substr(0, 3);
    day = date.substr(4, 2);
    year = date.substr(8, 4);

    string month_array[12] = { "jan","feb","mar","apr","may","jun",
                               "jul","aug","sep","oct","nov","dec" };

    for (int i = 0; i < 12; ++i)
        if (month == month_array[i])
            *mptr = i + 1;

    *dptr = stoi(day);
    *yptr = stoi(year);

    return true;
}

int main()
{
    int day, month, year;
    int* d = &day, * m = &month, * y = &year;

    if (GetDate(d, m, y))
        cout << "\n\n\n date is:  " << *m << "/" << *d << "/" << *y;
    else
        cout << "incorrect date";

    _getch();

    return 0;
}
  • This might help https://stackoverflow.com/questions/1714515/how-can-i-pad-an-int-with-leading-zeros-when-using-cout-operator/1714538 – John3136 Nov 04 '21 at 22:35
  • I would check out the iostream modifiers for output, and double-check your substr expression for the year. – Mark Ransom Nov 04 '21 at 22:40
  • Unrelated: A user interface that says `Please enter a date` without specifying the exact format is doomed. There are so many ways to specify dates in the world that you need to inforrm the user. – Ted Lyngmo Nov 04 '21 at 22:44
  • Unless prevented, I'd simply use printf. `printf("%02d", 7)` will display `07` – enhzflep Nov 04 '21 at 23:42

1 Answers1

0

Use modulo to isolate the first '9' in "990," like this:

int i=990;
int iModulo=990%100; // will return 90
int year=iModulo;

as for the month, that can't be fixed; an integer cannot begin with 0.