2013-06-20 80 views
0

我從來沒有經歷過這樣的事情。我正在使用一個cout語句來幫助我調試一個小程序,一旦我對代碼感到滿意,我就註釋掉了cout。現在,代碼不再有效。以下是cout註釋的代碼。評論cout語句導致不同(和不正確)的輸出

該程序的目的是測試兩個硬編碼布爾二維數組的每行有奇數個真正的語句。因此,第一個數組應該返回true,第二個數組應該返回false。隨着cout聲明註釋掉,而是返回false。

#include <iostream> 

using namespace std; 

template <size_t size_y> 
bool findEvenDegrees(bool mapArray[][size_y]) 
{ 
    bool returnValue; 
    for (int x=0; x<size_y; x++) 
    { 
     int result = 0; 
     for (int y=0; y<size_y; y++) 
     { 
      result = result + mapArray[x][y]; 
      //the line below causes the problem 
      cout << mapArray[x][y] << "\t" << result << "\t" << x << endl; 
     } 
     if (result%2 == 1) 
     { 
      returnValue = false; 
      break; 
     } 
    } 
    if (returnValue== false) 
    { 
     return returnValue; 
    } 
    else 
    { 
     return true; 
    } 
} 

int main() 
{ 
    bool array1[][6] = 
    { 
     {false,true,true,false,false,false}, 
     {true,false,false,true,false,false}, 
     {true,false,false,true,false,false}, 
     {false,true,true,false,true,true}, 
     {false,false,false,true,false,true}, 
     {false,false,false,true,true,false} 
    }; 
    bool array2[][8] = 
    { 
     {false,true,true,false,false,false,false,false}, 
     {true,false,false,true,false,false,false,false}, 
     {true,false,false,true,false,false,false,false}, 
     {false,true,true,false,true,false,false,false}, 
     {false,false,false,true,false,true,true,false}, 
     {false,false,false,false,false,true,false,true}, 
     {false,false,false,false,true,false,false,true}, 
     {false,false,false,false,false,true,true,false} 
    }; 
    bool answer1 = findEvenDegrees(array1); 
    bool answer2 = findEvenDegrees(array2); 
    if (answer1 == true) 
    { 
     cout << "Array 1 has a even degree for every switch." << endl; 
    } 
    else 
    { 
     cout << "Array 1 has a odd degree for at least one switch." << endl; 
    } 
    if (answer2 == true) 
    { 
     cout << "Array 2 has a even degree for every switch."; 
    } 
    else 
    { 
     cout << "Array 2 has a odd degree for at least one switch."; 
    } 
    return 0; 
} 
+0

[評論出'cout'一部分。](http://coliru.stacked-crooked.com/view?id=94e5c41fe17a9a5f86815122c813b6a8-f674c1a6d04c632b71a62362c0ccfc51) –

+0

任'size_y!= X || size_y!= y'。它們的大小與您在主要布爾數組中看到的大小不同。 – mr5

回答

2

您從未初始化returnValue。如果碰巧開始爲false,它將保持這種狀態,該功能將返回false

1

首先,我清理你的代碼一點點,到達:

#include <iostream> 

template <size_t S> 
bool findEvenDegrees(bool (&themap)[S][S]) { 
    for(bool(&row)[S]: themap) { 
    bool is_degree_odd = false; 
    for(auto col: row) 
     is_degree_odd ^= col; 
    if(is_degree_odd) 
     return false; 
    } 
    return true; 
} 

int main() 
{ 
    using std::cout; 
    using std::endl; 

    bool array1[6][6] = { 
    {false,true,true,false,false,false}, 
    {true,false,false,true,false,false}, 
    {true,false,false,true,false,false}, 
    {false,true,true,false,true,true}, 
    {false,false,false,true,false,true}, 
    {false,false,false,true,true,false} 
    }; 
    cout << "Array 1 has an " 
    << (findEvenDegrees(array1) ? "even degree for every" : "odd degree for at least one") 
    << " switch." << endl; 

    bool array2[8][8]= { 
    {false,true,true,false,false,false,false,false}, 
    {true,false,false,true,false,false,false,false}, 
    {true,false,false,true,false,false,false,false}, 
    {false,true,true,false,true,false,false,false}, 
    {false,false,false,true,false,true,true,false}, 
    {false,false,false,false,false,true,false,true}, 
    {false,false,false,false,true,false,false,true}, 
    {false,false,false,false,false,true,true,false} 
    }; 
    cout << "Array 2 has an " 
    << (findEvenDegrees(array2) ? "even degree for every" : "odd degree for at least one") 
    << " switch." << endl; 

    return 0; 
} 

在清除它的過程中,我消除了if(result%2 == 1) { resultValue = true; break; },通過有效地返回時,我找到的第一個奇數度排。當我消除了resultValue變量時,我還殺死了@sth提到的「單一化」錯誤。

+0

+1 - 更好:-) –