2013-06-30 111 views
2

我正在制作一个简单的立方体根猜谜游戏,在该游戏中随机生成并显示多维数据集,然后用户输入多维数据集根目录。下面是我的程序:立方根猜谜游戏

int main() 
try { 
    int max, min; 
    max = 99; min = 1; // only cubes of 1-99 are displayed 

    // display the title 
    cout << "\n\t\t\t\tCube Root Game" << endl; 
    cout << "\t\t\t\t=============\n" << endl; 
    srand(time(0)); // seed for random number generator 

    // display 10 numbers for the user to guess the cube root 
    for (int i = 0; i < 10; i++) {  
     int answer; // answer inputted by the user 
     int temp = rand() % (max - min) + min; // random number 
     int t3 = temp * temp * temp; // cube of the random number 

     cout << "\tEnter the cube root for " << t3 << " : "; 
     cin >> answer; 

     if (answer == t3) { 
      cout << "\tCorrect answer!\n" << endl; 
     } 
     else { 
      cout << "\tIncorrect answer\n" << endl; 
     } 
    } 
    keep_window_open("q"); 
} 
catch (runtime_error& e) { 
    cerr << "Error: " << e.what() << endl; 
    keep_window_open("q"); 
    return 1; 
} 
catch(...) { 
    cerr << "Unexpected error.\n"; 
} 

的问题是,当我输入正确的立方根,它总是说,这是不正确的,但如果比较似乎还好我的,所以我不知道什么是错的。

回答

8
if (answer == t3) 

你不是这个意思:

if (answer == temp) 

(?你希望用户猜测的根源,而不是立方体,右):-)

+1

哦,这是正确的,谢谢傻我。 – Khalid