2017-06-26 28 views
0

我一直在为此工作大约一周,并且出于某种原因,我无法克服它。我在搜索数组的元素时遇到了超出范围的错误,并尝试将需要的字符移动到需要的数组中。如何将日期从DD-MM-YYYY转换为MM-DD-YYYY的字符串

无效showFileDateCleansed(字符串第[],字符串最后[],字符串生日[]){

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


      } 



     } 

    } 

} 

回答

0

case 1switch声明没有break声明,所以它会落空到case 2,其中c递增。难道这会让c超出范围吗?

+0

否以前是这样做的。我的意思是把这个突破放回去。我一直在试图弄清楚发生了什么。我可能应该把它清理一下。 – user658070

+0

它打破tempHoldDD [i] .at(c)= temp在情况下0. – user658070

+0

你为什么操作字符串数组?那个正在打破的声明说:“将'tempHoldDD'中'第i个'字符串的'c'字符设置为'temp'。”由于您的日期字符串长度为10个字符,我不禁想到您对数据的错误思考。只有一个字符串是一个字符数组。 – Alex