为什么 if else 结构在 C++ 中不起作用?

Ala*_*ker 2 c++ if-statement

我一直在尝试用 C++ 编写 tictactoe,但我不知道为什么 if else 语句没有被执行。

    #include <iostream>
#include <conio.h>
#include <cstdlib>

using namespace std;

char XorO;

int n;

char a = '1';
char b = '2';
char c = '3';
char d = '4';
char e = '5';
char f = '6';
char g = '7';
char h = '8';
char i = '9';

int charchange[] = {79, 80, 81, 575, 6786, 75, 7879, 67697, 689698};
void Welcome() // FUNCTION1
{
    cout << "\nWelcome to Tic-Tac-toe!!!\n";
    cout << "You are going to play with the computer!!\n";
    cout << "Press X to choose X or press O to choose O for the game!!: ";
}

void framework() //FUNCTION2
{
    cout << " _ _ _ " << endl;
    cout << "| | | |" << endl;
    cout << "|"<< a <<"|"<< b <<"|"<< c <<"|" << endl;
    cout << "|"<< d <<"|"<< e <<"|"<< f <<"|" << endl;
    cout << "|"<< g <<"|"<< h <<"|"<< i <<"|" << endl;
    cout << "|_|_|_|" << endl;
}

int main()
{
    aiyeee:
    Welcome();
    cin >> XorO;  ////////////GETS EXCECUTED TILL HERE
    if (XorO == 'X')          ////////////////////////DSEN'T EXECUTE
    {
        for(n=1;n>=9;n++)
     {
        if(n%2 != 0){
        framework();
        cout << "\nOk:)";
        aeneas:
        cout << " Your turn!! Which number to change to X: ";
        cin >> charchange[(n-1)];
        for (int ty = 1; ty <= 9; ty++)
        {
            if(charchange[(n-1)] != ty)
            {
                cout << "INVALID INPUT!!\n";
                goto aeneas;
            }
        }
        switch(charchange[(n-1)])
        {
            case 1:
            if (a == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            a = 'X';
            framework();
            break;
            case 2:
            if (b == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            b = 'X';
            framework();
            break;
            case 3:
            if (c == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            c = 'X';
            framework();
            break;
            case 4:
            if (d == 'O')
            {
                cout << "\nAlready Changed to O!!\n";
                goto aeneas;
            }
            d = 'X';
            framework();
            break;
            case 5:
            if (e == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            e = 'X';
            framework();
            break;
            case 6:
            if (f == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            f = 'X';
            framework();
            break;
            case 7:
            if (g == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            g = 'X';
            framework();
            break;
            case 8:
            if (h == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            h = 'X';
            framework();
            break;
            case 9:
            if (i == 'O')
            {
                cout << "\nAlready Changed to 'O'!!\n";
                goto aeneas;
            }
            i = 'X';
            framework();
            break;
        }
        if(n>4)
        {
            if ((a==b && b==c)||(d==e && e==f)||(g==h && h==i)||(a==d && d==g)||(b==e && g==h)||(c==f && f==i)||(a==e && e==i)||(c==e && e==f))
            {                   
                cout << "YOU WIN!!!\n";
                goto allagulla;
            }
        }
        }

        else if(n%2 == 0)
        {
            aghhg:
            charchange[n-1] = ((rand()%9) + 1);
            switch (charchange[n-1])
            {
                case 1:
                if(a == 'X') 
                    {
                        goto aghhg;
                    }
                a = 'O';
                framework();
                break;
                case 2:
                if(b == 'X') 
                    {
                        goto aghhg;
                    }
                b = 'O';
                framework();
                break;
                case 3:
                if(c == 'X') 
                    {
                        goto aghhg;
                    }
                c = 'O';
                framework();
                break;
                case 4:
                if(d == 'X') 
                    {
                        goto aghhg;
                    }
                d = 'O';
                framework();
                break;
                case 5:
                if(e == 'X') 
                    {
                        goto aghhg;
                    }
                e = 'O';
                framework();
                break;
                case 6:
                if(f == 'X') 
                    {
                        goto aghhg;
                    }
                f = 'O';
                framework();
                break;
                case 7:
                if(g == 'X') 
                    {
                        goto aghhg;
                    }
                g = 'O';
                framework();
                break;
                case 8:
                if(h == 'X') 
                    {
                        goto aghhg;
                    }
                h = 'O';
                framework();
                break;
                case 9:
                if(i == 'X') 
                    {
                        goto aghhg;
                    }
                i = 'O';
                framework();
                break;
            }
            if(n>4)
        {
            if ((a==b && b==c)||(d==e && e==f)||(g==h && h==i)||(a==d && d==g)||(b==e && g==h)||(c==f && f==i)||(a==e && e==i)||(c==e && e==f))
            {                   
                cout << "COMPUTER WINS!!\n";
                break;
            }
        }
        }
    }
    }
    else if (XorO == 'O')
    {

    }

    else
    {
        cout << "\nINVALID INPUT!!!\n";
        goto aiyeee;
        cin >> XorO;    
    }

    allagulla:
    cout << "\nWanna play again?(y/n) ";/////////////////////COMES DOWN STRAIGHT HERE AFTER THE PREV COMM
    char yesno;
    yesno = getche();
    if (yesno == 'y')
        {
            goto aiyeee;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在 main() 启动后,它输入了 XorO 但后来归结为想再玩一次吗?说真的,我不知道为什么:(对不起,程序很长,实际上我正在学习 C++。

Dav*_*eri 8

了解如何使用调试器

编译-g

g++ -Wall -pedantic -g -o demo demo.cpp
Run Code Online (Sandbox Code Playgroud)

运行调试器:

gdb demo
Run Code Online (Sandbox Code Playgroud)

在可疑行中放置一个断点:

(gdb) break 44
Punto de interrupción 1 at 0x1502: file demo.cpp, line 44.
(gdb) run
Run Code Online (Sandbox Code Playgroud)

询问调试器:

Breakpoint 1, main () at demo.cpp:44
44      if (XorO == 'X')          ////////////////////////DSEN'T EXECUTE
(gdb) print XorO
$1 = 88 'X'
(gdb) step
46          for(n=1;n>=9;n++)
(gdb) step
259     cout << "\nWanna play again?(y/n) ";/////////////////////COMES DOWN STRAIGHT HERE AFTER THE PREV COMM
(gdb) print n
$2 = 1
(gdb)
Run Code Online (Sandbox Code Playgroud)

你有看到?n永远不会大于或等于 9,因此跳过整个for循环。

另一方面,尝试为那些审查您的代码的人简化任务,我们大多数人都没有conio库,我不得不替换getchegetc(stdin),顺便说一下getche返回的是int,而不是char

char yesno;
Run Code Online (Sandbox Code Playgroud)

应该

int yesno;
Run Code Online (Sandbox Code Playgroud)