0

I'm writing a program that converts a short date (mm/dd/yyyy) to a long date (March 12, 2014) and prints out the date.

The program has to work given the following user inputs: 10/23/2014 9/25/2014 12/8/2015 1/1/2016

I have the program working with the first user input but I'm not sure how to proceed with handling a user input that doesn't have a "0" in the first position of the string.

#include <iostream>
#include <string>

using namespace std;

int main() 
{
    string date; 
    cout << "Enter a date (mm/dd/yyyy): " << endl;
    getline(cin, date);

    string month, day, year;

    // Extract month, day, and year from date
    month = date.substr(0, 2);
    day = date.substr(3, 2);
    year = date.substr(6, 4);

    // Check what month it is 
    if (month == "01") {
        month = "January";
    }
    else if (month == "02") {
        month = "February";
    } 
    else if (month == "03") {
        month = "March";
    } 
    else if (month == "04") {
        month = "April";
    } 
    else if (month == "05") {
        month = "May";
    } 
    else if (month == "06") {
        month = "June";
    } 
    else if (month == "07") {
        month = "July";
    } 
    else if (month == "08") {
        month = "August";
    } 
    else if (month == "09") {
        month = "September";
    } 
    else if (month == "10") {
        month = "October";
    } 
    else if (month == "11") {
        month = "November";
    } 
    else {
        month = "December";
    }

    // Print the date
    cout << month << " " << day << "," << year << endl;
    return 0;
}

I'd greatly appreciate any help.

JimT
  • 99
  • 3
  • 3
  • 11
  • 4
    instead of getting substrings at pre-determined indexes. Look for the `/` index and cut from there. – Zaid Amir Sep 30 '14 at 07:57
  • I would want to use the find member function... something like date.find("/", 0) to look for the / character and search from position 0. Should I still parse out the month, day, and year in the date? – JimT Sep 30 '14 at 08:06
  • 1
    @JimT [`find`](http://en.cppreference.com/w/cpp/string/basic_string/find) will give you the offsets to use in your `date.substr` calls instead of fixed values. – zakinster Sep 30 '14 at 08:08
  • Also, you should consider that string convert to integer for supporting both '01' and '1'. Please use index = stoi(month) and change condition for if like if(index == 1) month = "January" – Junyoung LEE Sep 30 '14 at 08:35
  • The current program supports both '01' and '1' without changing the code from index = stoi(month). In my if statements, I use if (month == "1") – JimT Sep 30 '14 at 08:53
  • [How to split a string in C++?](http://stackoverflow.com/q/236129) – kotlomoy Sep 30 '14 at 13:12

2 Answers2

2

As Red Serpent wrote in the comments: Search for the / using std::string::find, e.g.

#include <iostream>

int main()
{
    std::string date = "09/28/1983";

    int startIndex = 0;
    int endIndex = date.find('/');
    std::string month = date.substr(startIndex, endIndex);

    startIndex = endIndex + 1;
    endIndex = date.find('/', endIndex + 1);
    std::string day = date.substr(startIndex, endIndex - startIndex);

    std::string year = date.substr(endIndex + 1, 4);

    std::cout << month.c_str() << " " << day.c_str() << "," << year.c_str() << std::endl;

    return 0;
}
TobiMcNamobi
  • 4,687
  • 3
  • 33
  • 52
0

You could also take advantage of stream conversion, for a less efficient but simpler solution:

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

int main() {

    string months[] = {"", "January", "February", "Mars", "April", "May", "June", "Jully", "August", "September", "October", "December"};

    cout << "Enter a date (mm/dd/yyyy): " << endl;
    char c;
    int day, month, year;
    cin >> day >> c >> month >> c >> year;
    // error handling is left as an exercice to the reader.
    cout << months[month] << " " << day << ", " << year << endl;
    return 0;
}
zakinster
  • 10,508
  • 1
  • 41
  • 52