Help with string

I think i got it wrong and 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.

Can someone correct it or show me to how to correct it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstring>
using namespace std;

void readDate (char []);
void convertDate (char []);

const int SIZE = 11;

int main ()
{
    char original[SIZE];
    
    readDate(original); // call readDate
    convertDate(original); // call convertDate
    
    return 0;
}

void readDate ( char arr[])
{
    cout << "Enter a date in the MM/DD/YYYY format (e.g. 07/19/2002): ";
    cin.getline (arr, SIZE); // read SIZE characters into the character array
}
void convertDate (char arr[])
{
    cout << "Date in converted format is: ";
    
    // output written month according to numeric month
    if (arr[0] == '0' && arr[1] == '1')
    {
        cout << "January";
    }
    else if (arr[0] == '0' && arr[1] == '2')
    {
        cout << "February";
    }
    else if (arr[0] == '0' && arr[1] == '3')
    {
        cout << "March";
    }
    else if (arr[0] == '0' && arr[1] == '4')
    {
        cout << "April";
    }
    else if (arr[0] == '0' && arr[1] == '5')
    {
        cout << "May";
    }
    else if (arr[0] == '0' && arr[1] == '6')
    {
        cout << "June";
    }
    else if (arr[0] == '0' && arr[1] == '7')
    {
        cout << "July";
    }
    else if (arr[0] == '0' && arr[1] == '8')
    {
        cout << "August";
    }
    else if (arr[0] == '0' && arr[1] == '9')
    {
        cout << "September";
    }
    else if (arr[0] == '1' && arr[1] == '0')
    {
        cout << "October";
    }
    else if (arr[0] == '1' && arr[1] == '1')
    {
        cout << "November";
    }
    else if (arr[0] == '1' && arr[1] == '2')
    {
        cout << "December";
    }
    // output remaining portion of converted date
    
    cout << " " << arr[3] << arr[4] << ", " << arr[6] << arr[7] << arr[8] << arr[9] << endl;
}
converting to numbers would make life easier.

1
2
3
4
5
6
7
8
9
int dd = ((ar[0]-'0')*10) + ar[1];
int mm = ((ar[2]-'0')*10) + ar[2];

switch (mm)
{
case 1:
    cout << January";
    break;
case ... 



better still use have a look at string management functions
http://www.cplusplus.com/reference/cstring/strtok/
Last edited on
I change my code but I still dont get it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    #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;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

int main()
{
    const int NMONTHS = 12 ;
    const std::string month[NMONTHS] = { "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

    std::cout << "Enter a date in the form MM/DD/YYYY: ";
    int mon;
    int day;
    int year;
    char slash, slash2 ;

    if( std::cin >> mon >> slash >> day >> slash2 >> year &&
        slash == '/' && slash2 == '/' &&
        mon > 0 && mon <= NMONTHS && day > 0 && day < 32 /* more validations */ )
    {
       std::cout << month[mon-1] << ' ' << day << ", " << year << '\n' ;
    }

    else std::cerr << "error in input\n" ;
}
Use C++ strings. They'll make your life easier. And use regex to validate the input.

Here is how to solve this:
-> Have an array with the months.
-> Get the input, validate it and if its in "mm/dd/yyyy" then continue else exit.
-> Take out the month from the input, convert it to int, subtract 1 and get the month in word representation from the array. (Subtract 1 because array index starts at 0)
-> Take out the day and year from the input string and keep them in their own variables.
-> Output your result.

Here is what I'm talking about in code representation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Note: The following code requires a compiler with C++ 11 support
#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    string months[] = {"January", "February", "March", "April",
    "May", "June", "July", "August", "September", "October", "November", "December"};
    string dateString;
    regex dRegex("([0][1-9]|[1][0-2])/([0-2][0-9]|[3][0-1])/\\d{4}");
    
    cout << "Enter a date in the form mm/dd/yyyy: ";
    cin >> dateString;
    
    if(!regex_match(dateString, dRegex))
    {
        cout << "Invalid input!" << endl;
        return 1;
    }
    
    string month = months[stoi(dateString.substr(0,2))-1];
    string day = dateString.substr(3,2);
    string year = dateString.substr(6,4);
    
    cout << "Output: "+month+" "+day+", "+year << endl;
    
    return 0;
}
Topic archived. No new replies allowed.