0

When taking input from the user as a date, I would like be able to extract the numbers whether the format is mm/dd/yy or m/d/yy. I can only figure out how to do one or the other, but not both. How might I go about this? I am new to C++ coming from python. Here is my code.

#include <iostream>
#include <string>
using namespace std;



bool isMagicDate(string year, string month, string day)

{       
int int_month = stoi(month);
int int_day  = stoi(day);
int int_year = stoi(year);

if (int_month * int_day == int_year)
    return true;

else return false;
}   


int main()
{
string date;

cout << "enter a date in the format mm/dd/yy: " << endl;
cin >> date;


string month = date.substr(0,2);
string day = date.substr(3,2);
string year = date.substr(6,2);

cout << "the month is " << month  << endl 
     << "the day is " << day  << endl
     << "the year is " << year << endl;

cout << "the date you entered is " << date << endl;

bool magic = isMagicDate(year, month, day);

cout << "Is the date magic? " << magic << endl;


return 0;
}
Riley Hughes
  • 772
  • 1
  • 8
  • 19
  • You could use a delimiter function that utilizes find and substr. That way you can handle both date formats. Let me know if you need an example, but here is one http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c – IsakBosman May 04 '17 at 17:18

4 Answers4

1

If your input will have exactly mm/dd/yy or m/d/yy format. You can do a simple check with string .find() like so:

if(date.find("/") == 2)
{
    // do what you need if first "/" is at position 2
}
else
{
    // do what you need if the first "/" is at position 1
}
Robert C. Holland
  • 1,283
  • 13
  • 42
1

You can use std::istringstream to parse the date string, eg:

#include <iostream>
#include <string>
#include <sstream>

bool isMagicDate(int year, int month, int day)
{       
    return ((month * day) == year);
}   

int main()
{
    std::string date;

    std::cout << "enter a date in the format mm/dd/yy: " << std::endl;
    std::cin >> date;

    int month, day, year;
    char slash1, slash2;

    std::istringstream iss(date);
    if ((iss >> month >> slash1 >> day >> slash1 >> year) &&
        (slash1 == '/') && (slash2 == '/'))
    {
        std::cout << "the date you entered is " << date << std::endl;

        std::cout << "the month is " << month << std::endl 
             << "the day is " << day  << std::endl
             << "the year is " << year << std::endl;

        bool magic = isMagicDate(year, month, day);

        std::cout << "Is the date magic? " << magic << std::endl;
    }
    else
        std::cout << "invalid date entered!" << std::endl;

    return 0;
}

Alternatively, in C++11 and later, you can use the std::get_time I/O manipulator instead:

#include <iostream>
#include <string>
#include <iomanip>

int main()
{
    std::cout << "enter a date in the format mm/dd/yy: " << std::endl;

    std::tm t = {};
    if (std:cin >> std::get_time(&t, "%m/%d/%y"))
    {
        std::cout << "the date you entered is " << std::put_time(&t, "%c") << std::endl;

        std::cout << "the month is " << tm.tm_mon+1 << std::endl 
             << "the day is " << tm.tm_mday << std::endl
             << "the year is " << tm.tm_year << std::endl;

        // use tm as needed ...
    }
    else
        std::cout << "invalid date entered!" << std::endl;

    return 0;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
1

Split your date into month, date & year with '/' as a delimiter

// func to split the date
vector<string> split_date(const string &s, char delim) {
    stringstream ss(s);
    string item;
    vector<string> tokens;
    while (getline(ss, item, delim)) {
        tokens.push_back(item);
    }
    return tokens;
}

Note that this solution does not skip empty tokens.

Now you can easily extract the values of month, date & year from vector returned by split_date.

// splitting
string date = "04/05/2017"; // mm/dd/yy or m/d/yy

vector<string> splitted_date = split_date(date, '/');
if(splitted_date.size() == 3) {
    string month = splitted_date[0];
    string day = splitted_date[1];
    string year = splitted_date[2];
}
Rajeev Singh
  • 3,207
  • 2
  • 16
  • 27
0

Here is a solution using a struct to give you another idea.

#include <stdio.h>
#include <conio.h>  // for clrscr() function
#include <iostream>
#include <string>

using namespace std;

struct Date
{
    string month;
    string day;
    string year;
};

void setDate(Date &d);
void printDate(Date &d);
bool isMagicDate(Date &d);

 int main(int argc, char* argv[])
{
    Date mydate;

    setDate(mydate);

    cout << "The Month is: " << mydate.month << endl;
    cout << "The Day is: " << mydate.day << endl;
    cout << "The Year is: " << mydate.year << endl;

    cout << "The date you entered is: ";

    printDate(mydate);

    bool magic = isMagicDate(mydate);

    cout << "Is the date magic? " << isMagicDate(mydate) << endl;


    getch();
    return 0;
}


void setDate(Date &d)
{
    string date;
    printf("Enter date mm/dd/yy: ");

    getline(cin,date,'/');
    d.month = date;

    getline(cin,date,'/');
    d.day = date;

    getline(cin,date,'\n');
    d.year = date;

}


bool isMagicDate(Date &d)
{
    return stoi(d.month) * stoi(d.day) == stoi(d.year);
}


void printDate(Date &d)
{
    cout << d.month << "/" << d.day << "/" << d.year << endl;
}
dmaelect
  • 141
  • 1
  • 10