2012-07-16 14 views
7

que tienen una función para devolver un bool:función de C++ de Boole regresar 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;} 
    } 
} 

Cuando lo llamo usando:

cout << restart(); 

que obtener la salida:

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

Puede Alguien ve cómo solucionar este extraño problema? Gracias de antemano.

Mi código WIP como lo es ahora:

#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

¿Cómo se imprime el valor devuelto? 'cout << restart()'? – RedX

+0

Sí. Fue utilizado como parte de mi ciclo principal. Un valor de bool gameRunning se configuró usando gameRunning = restart(); y seguí obteniendo 56. Así que probé la función en sí usando cout << restart(); y sigo teniendo 56 – jw1294

+1

¿Devuelve 56 en el caso de "no" también? –

Respuesta

1

Utilicé el compilador mingw32, al usar g ++ 4.8 funciona bien. Parece ser un error de compilación. (gracias a grizzly)

4

[Esto es algo de un repost de mi comentario ya OP dijo que resuelto su problema]

No tiene valor de retorno definida fuera de la while loop Si de alguna manera se sale de ella, no devuelve un valor (aunque no tengo idea de qué comportamiento se espera o incluso si se espera algún comportamiento en este caso)

Para que mi respuesta sea un poco más completa, tengo encontró esto: Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

+2

De hecho, GCC advierte sobre esto. No tengo idea de cómo saldría, sin embargo. – chris

3

OK, estoy haciendo una conjetura. El código original tiene la línea

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

Y su definición de la retícula es

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

Esto significa que su escriben fuera de los límites de la matriz. Este es un comportamiento indefinido. Corrija esto y verifique si su programa funciona como se espera.

Cuestiones relacionadas