lundi 26 juin 2017

How do you convert a string holding a date from DD-MM-YYYY to MM-DD-YYYY

I have been working on this for about a week and for some reason I just can't get past it. I am getting an out of range error when I am searching the elements of the array, and attempt to move the characters I need to a need array.

void showFileDateCleansed(string first[], string last[], string birthday[]) {

string tempHoldDD[10];
string tempHoldMM[10];
/*
The stuff below is working so  Ijust commented it out until I figure out the isssue I am having with the dates
for (int i = 0; i < 6; i++)
{
first[i].at(0) = toupper(first[i].at(0));
last[i].at(0) = toupper(last[i].at(0));
}

for (int i = 0; i < 10; i++)
{
cout << first[i] << " ";
cout << last[i] << "\n";
}*/

int d = 0; //Im using this to keep track of whether I have passed the delimiter in the text file "-"
bool done = false;
for (int i = 0; i < 10; i++)
{

    done = false;
    int c = 0;//this is used for the character within the element of the array, it increments at the bottom so that it moves to the next character.

    while (done != true)
    {

        // <TK> Check for invalid character
        if (c == 0)
        {
            std::cout << "Invalid character at index: " << i << std::endl;

        }

        if (birthday[i].at(c) == '-')
        {
            d += 1;
            done = true;
        }
        else
        {
            switch (d)
            {
            case 0:
            {

                //  Try catch to prevent the out of range exception from crashing.
                //try
                //{
                    //  Debug
                    std::cout << "C: " << c << std::endl;

                    // create a temporary variable for the value.
                    char temp = birthday[i].at(c);
                    tempHoldDD[i].at(c) = temp;
                //}
                /*catch (std::out_of_range const& exc)
                {
                    std::cout << exc.what() << '\n';
                }*/

                //cout << tempHoldMM[c] << "\n";

            }

            case 1:
            {
                // Try catch to prevent the out of range exception from crashing.
                try
                {
                    // Debug
                    std::cout << "C: " << c << std::endl;

                    // create a temporary variable for the value.
                    char temp = tempHoldMM[i].at(c);
                    birthday[i].at(c) = temp;
                }
                catch (std::out_of_range const& exc)
                {
                    std::cout << exc.what() << '\n';
                }

                //cout << tempHoldMM[c] << "\n";
                c += 1;
                break;
            }


            }



        }

    }

}

Aucun commentaire:

Enregistrer un commentaire