Here is question:
Write a program that reads a string from the user containing a date in the form
mm/dd/yyyy. It should print the date in the form March 12, 2012.

#include<iostream>
using namespace std;

int main()
{
int mon;
int day;
int year;
string month[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

//the array is for another part of the program that i have not worked out yet

cout << "Enter a date in the form MM/DD/YYYY: ";
cin >> mon >> day >> year;
cout << mon << "/" << day << "/" << year << endl;
system ("pause");
return 0;
}

my output when i enter date 12/12/2009:

Enter a date in the form MM/DD/YYYY: 12/12/2009
12/-858993460/-858993460 

Edited 2 Years Ago by chubbyy.putto

line 14 won't work because you have to also enter the / between the numbers. Get the input as a string then parse the string to extract each of the individual parts.

Here I am using the function atoi() to convert a string to an integer. First extract the substring of the digits I need for the integer then pass a pointer to the character array to atoi().

string date;
int month, day, year;

cout << "Enter a data in format mm/dd/yyyy\n";
cin >> date;
// no error checking here, assumes you entered date in correct format
// But ... your program probably needs to verify the format of the string.

month = atoi(date.substr(0,3).c_str());
day = atoi(date.substr(3,3).c_str());
year = atoi(date.substr(6).c_str());
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int mon;
    int day;
    int year;
    string month[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    //the array is for another part of the program that i have not worked out yet
    cout << "Enter a date in the form MM/DD/YYYY: ";
    cin >> mon >> day >> year;
    cout << mon << "/" << day << "/" << year << endl;
    month = atoi(month.substr(0,3).c_str());
    day = atoi(month.substr(3,3).c_str());
    year = atoi(month.substr(6).c_str());
    return 0;
}

ERROR: MEMBER REFERENCE BASE TYPE 'STRING [12] IS NOT A STRUCTURE OR UNION

And ...

you could also ... simply do input like this:

// monthDayYear.cpp //

#include <iostream>
#include <string>

using namespace std;



int main()
{
    int mon;
    int day;
    int year;
    char dummy;

    const string month[] =
        {
            "January", "February", "March",
            "April", "May", "June",
            "July", "August", "September",
            "October", "November", "December"
        };


    cout << "Enter a date in the form MM/DD/YYYY: ";
    cin >> mon >> dummy >> day >> dummy >> year;

    cout << mon << "/" << day << "/" << year << endl;

    cout << month[mon-1] << " " << day << ", " << year << endl;


    return 0;
}

Also note: substr length below should probably be coded as a 2 (not a 3, even though atoi will ignore the trailing '/' char each time)

//////////////////////////////////0123456789//
cout << "Enter a date in the form MM/DD/YYYY: ";
string date;
getline( cin, date );

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

cout << mon << "/" << day << "/" << year << endl;

Edited 2 Years Ago by David W: fixed index

This article has been dead for over six months. Start a new discussion instead.