如何将保存日期的字符串从DD-MM-YYYY转换为MM-DD-YYYY

时间:2017-06-26 23:20:16

标签: arrays c++11

我一直在研究这个问题大约一个星期,由于某种原因我无法超越它。当我搜索数组的元素时,我得到一个超出范围的错误,并尝试将我需要的字符移动到需要的数组。

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;
            }


            }



        }

    }

}

1 个答案:

答案 0 :(得分:0)

您的case 1声明中的switch没有break声明,因此它会落到case 2,其中c是递增。这会让c超出范围太快吗?