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 

Recommended Answers

All 7 Replies

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.

I dont understand can you show me?

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());

but I need to transfer from number to world

#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

You didn't do it like I showed you. line 13 is wrong. Re-read all the code I posted.

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.