2012-07-16 118 views
7

我有一個函數返回一個bool:C++布爾函數返回56

bool restart() 
{ 
    std::string answer; 
    bool answered = false; 
    while(answered == false) 
    { 
     cout << endl << endl << "Do you want to play again? y/n : "; 
     cin >> answer; 
     if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes") 
     {return true;} 
     if(answer == "n" || answer == "N" || answer == "0" || answer == "no") 
     {return false;} 
    } 
} 

當我把它用:

cout << restart(); 

我得到的輸出:

Do you want to play again? y/n : y 
56 

燦任何人看到如何解決這個奇怪的問題? 在此先感謝。

我WIP的代碼,因爲它現在是:

#include <iostream> 
#include <cstdlib> 

using namespace std; 

void drawScreen(int grid[3][3]);        // 
int movef(int grid[3][3], bool playersMove); 
int updateGrid(int grid[3][3], int move, bool playersMove); 
bool hasWon(int grid[3][3]); 
bool swapMover(bool playersMove);        // 
bool restart(); 
void endGame(); 

int main() 
{ 
    int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; 
    bool appRunning = true, gameRunning = true, playersMove = true; 
    int move; 

    // tests // 
    std::cout << restart(); 
    // tests // 

    //while(appRunning == true) 
    //{ 
    // while(gameRunning == true) 
    // { 
    //  drawScreen(grid); 
    //  move = movef(grid, playersMove); 
    //  grid[3][3] = updateGrid(grid, move, playersMove); 
    //  drawScreen(grid); 
    //  gameRunning = hasWon(grid); 
    //  playersMove = swapMover(playersMove); 
    // } 
    // appRunning = restart(); 
    //} 
    //endGame(); 
    } 

    void drawScreen(int grid[3][3]) 
    { 
     for(int i = 0; i < 3; i++) 
     { 
      for(int j = 0; j < 3; j++) 
      { 
      if(grid[i][j] == 10){cout << "X ";if(j == 2){cout << endl << endl;}} 
      if(grid[i][j] == 11){cout << "O ";if(j == 2){cout << endl << endl;}} 
      if(grid[i][j] != 10 && grid[i][j] != 11){cout << grid[i][j] << " "; 
      if(j == 2){cout << endl << endl;}} 
      } 
     } 
    } 

    int movef(int grid[3][3], bool playersMove) 
    { 
     return 0; 
    } 

    int updateGrid(int grid[3][3], int move, bool playersMove) 
    { 
     return 0; 
    } 

    bool hasWon(int grid[3][3]) 
    { 
     return false; 
    } 

    bool swapMover(bool playersMove) 
    { 
     if(playersMove == true){return false;} 
     if(playersMove == false){return true;} 
    } 

bool restart() 
{ 
    std::string answer; 
    bool answered = false; 
    while(answered == false) 
    { 
     cout << endl << endl << "Do you want to play again? y/n : "; 
     cin >> answer; 
     if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes") 
     {return true;} 
     if(answer == "n" || answer == "N" || answer == "0" || answer == "no") 
     {return false;} 
    } 
} 

void endGame() 
{ 

} 
+1

如何打印返回值? 'cout << restart()'? – RedX 2012-07-16 16:11:30

+0

是的。它被用作我主循環的一部分。一個布爾值gameRunning被設置爲它使用gameRunning = restart();我一直得到56.所以我使用cout << restart();測試了函數本身。我仍然得到56 – jw1294 2012-07-16 16:14:06

+1

在「否」的情況下它是否返回56? – 2012-07-16 16:14:43

回答

1

我使用了mingw32編譯器,當使用g ++ 4.8時它工作正常。這似乎是一個編譯器錯誤。 (感謝灰熊)

4

[這是我的評論的重新發布的有點,因爲OP說,它解決了他的問題]

你的外部定義沒有返回值while循環。如果不知爲何,你拿到它之外,你沒有返回值(雖然我不知道什麼是正常現象,或者即使任何行爲,在這種情況下預期)

爲了讓我的回答有點更徹底,我有發現這個: Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

+2

確實GCC警告這一點。儘管如此,我不知道它會如何脫身。 – chris 2012-07-16 16:47:04

3

好吧,我在猜測。你原來的代碼有行

grid[3][3] = updateGrid(grid, move, playersMove); 

而網格的定義是

int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; 

這意味着你編寫出數組邊界。這是未定義的行爲。 請更正此問題並檢查您的程序是否按預期工作。