【问题标题】:Need Clarification On Return 0, 1, -1 (Not in Main)返回 0、1、-1 时需要澄清(不在主目录中)
【发布时间】:2019-02-24 23:49:02
【问题描述】:

我是 C++ 的初学者(或一般的所有编码)。 话虽如此,我试图了解 return 0/return 1/return -1 在 main() 函数之外定义的内容。

我对返回值的了解是它允许一个人结束一个语句并获得一个“返回”整数值。 (我的教授)告诉我 return 0 就像是“已达到”的状态。告诉用户一切正常。

但是让我感到困惑的是下面的代码。在“int checkwin()”函数中,它声明如果玩家获胜,则代码将输入“return 1”。如果没有人获胜,则“返回 0”。最后一个我不完全理解(假设它意味着继续),因为我还没有弄清楚return 0/1/-1的定义。

#include <iostream>

using namespace std;

char square[10] {'0','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

int main()

{ // main function OPEN

    int player;
    int i;
    int choice;

    char mark;

    do { // do while loop open

            board();

            player=(player%2)?1:2;

            cout << "Player " << player << ", enter a number: ";
            cin >> choice;

            mark =(player ==1) ? 'X' : 'O';

            if(choice==1 && square[1]=='1')

                    square[1] = mark;

            else if(choice==2 && square[2]=='2')

                    square[2] = mark;

            else if(choice==3 && square[3]=='3')

                    square[3] = mark;

            else if(choice==4 && square[4]=='4')

                    square[4] = mark;

            else if(choice==5 && square[5]=='5')

                    square[5] = mark;

            else if(choice==6 && square[6]=='6')


            else if(choice==7 && square[7]=='7')

                    square[7] = mark;

            else if(choice==8 && square[8]=='8')

                    square[8] = mark;

            else if(choice==9 && square[9]=='9')

                    square[9] = mark;
            else
            { // else statment OPEN

                    cout << "That is an invalid choice" <<endl;

                    player--;
                    cin.ignore();
                    cin.get();
            } // else statement CLOSED

            i=checkwin();
            player ++;

    } // do while loop CLOSED
    while (i==-1);

            board();
    if(i==1)

            cout << "==> Player " << --player << "win";
    else
            cout << "==> Game Draw" ;

    cin.ignore();
    cin.get();

    return 0;

} // main function CLOSED



 //****************************************************************
//**********************checkwin function *************************
//*****************************************************************

int checkwin()

{ // checkwin function bracket OPEN

   if(square[1] == square[2] && square[2] == square[3])

            return 1;

    else if(square[4] == square[5] && square[5] == square [6] )

            return 1;

    else if(square[7] ==  square[8] && square[8] == square [9] )

            return 1;

    else if(square[1] == square[4] && square[4] == square[7])

            return 1;

    else if(square[2] == square[5] && square[5] == square[8])

            return 1;

    else if(square[3] == square[6] && square[6] == square[9])

            return 1;

    else if(square[1] == square[5] && square[5] == square[9])

            return 1;

    else if(square[3] == square[5] && square[5] == square[7])

            return 1;

    else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
            && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
            && square[9] != '9' )

            return 0;

    else
            return -1;



} // checkwin function bracekt CLOSED

//*******************************************************************
//************************ board function ***************************
//*******************************************************************

void board()

{ // board function bracket OPEN

    system("cls");
    cout <<"\n\nTic Tac Toe\n\n";

    cout<< "Player 1 ( X )  -  Player 2 ( O )\n\n\n" ;

    cout<< "     |     |     " << endl;
    cout<< "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

    cout<< "_____|_____|_____" << endl;
    cout<< "     |     |     " << endl;

    cout<< "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

    cout<< "_____|_____|_____" << endl;
    cout<< "     |     |     " << endl;

    cout<< "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

    cout<< "     |     |     "  << endl;

} // board function bracket CLOSED

对不起,如果我的问题有点混乱,或者我不够清楚。

【问题讨论】:

  • 我想我的答案是在回答你的问题

标签: c++ return return-value


【解决方案1】:

checkwin 中的-1 表示并非所有棋盘字段都已填满,目前没有人获胜,因此游戏尚未结束

1) 在初始位置,任意正方形[i] != XO

char square[10] {'0','1','2','3','4','5','6','7','8','9'}; 或等价的

square[0] = '0'; // dummy value -- not used at all
square[1] = '1';
square[2] = '2';
square[3] = '3';
square[4] = '4';
square[5] = '5';
square[6] = '6';
square[7] = '7';
square[8] = '8';
square[9] = '9';

2) 他们将火星放在船上mark =(player ==1) ? 'X' : 'O';

3) 游戏继续,而游戏状态为 -1:

    do { // do while loop open
        ...
        i=checkwin();
        ...
    } while (i == -1)  

-1 表示目前棋盘区域未满,没有人获胜,所以游戏还没有结束

【讨论】:

  • 哦,好吧。这真的很有帮助!我一定错过了“i = checkwin()”。我会节省我一个小时左右试图找出代码哈哈。谢谢你,感谢你为我分解它。
【解决方案2】:

函数的返回值是它与用户的契约的一部分。从函数的文档中应该清楚每个返回值的含义。

通常,在基于 unix 的系统中,从程序的 main 返回 0 表示成功执行。但是,内部函数可以而且通常不会遵循这个约定。

在此代码中,返回 -1 表示游戏尚未结束(继续执行 do-while 循环)。返回 1 意味着游戏结束并且获胜。而返回 0 则意味着比赛以平局结束。

【讨论】:

    【解决方案3】:

    该值表示函数作者希望它表示的任何含义。就这么简单。

    有人写了checkwin,然后那个人决定在这些条件下返回这些值。然后,使用该函数的人可以期望能够根据这些潜规则解释该函数的结果。

    我可以编写一个函数urgleburgle,当天空为蓝色时返回数字 42,当天空为红色时返回数字 98。当天空是其他颜色时,它返回数字 11111。而你,作为我的功能的用户,将不得不接受这个选择。没有更深的含义。

    函数的作者应该添加解释每个可能返回值含义的文档 cmets,以便您可以理解该函数并将其实际用于有意义的事情。

    在您的特定情况下,它看起来像 -1 的意思是“游戏还没有结果;继续前进”,但由于没有可用的 cmets,我们只能猜测.您应该要求作者(例如您的教授)澄清他们希望每个返回值对这个函数意味着什么。

    main 有点特殊,因为它的用户是操作系统(过度简化警告!),所以必须有一个关于它的返回值含义的预定义规则,操作系统可以理解。尽管存在各种其他特定于平台的约定,但通常 0 表示成功,1 表示失败。

    【讨论】:

    • 有趣。我认为 return 总是在每个函数中预定义的。这真的清除了它。谢谢大佬!
    • 作为一个兴趣点,这是一个“对孩子撒谎”出错的例子。您的教授真诚地试图通过提供有关您可能希望如何设计功能的一般规则/约定来使事情变得简单......但忽略了告诉您该语言没有制定这些规则。结果:你的困惑。作为教育工作者,我们都有责任不误导,即使我们试图简化。
    • 好吧,公平地说,我的课要提前一点(所有未来的作业都将在整个学期中查看),这样我才能更好地理解讲座。除了 main() 之外,我的教授还没有讨论过其他函数。我相信他会(希望)在演讲时更详细地讨论返回值。
    • 好的,那么他可能专门在谈论main。如果你是自学,那么你应该从a book
    • 我会,但我的班级没有我们要学习的书。但我想我可以在 Amazon 上找到一本 c++ 书。
    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 2012-05-11
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2011-11-09
    相关资源
    最近更新 更多