2017-06-26 54 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