简体   繁体   中英

case label value exceeds maximum value for type c++

I'm having trouble with all the switches that I've made in this text, my question is what do I need to write, so that this problem doesn't occur? Which type should I wright?

#include <iostream>

using namespace std;

void tablero (int i, int j);
char matriz [3][3];
int comprueba ();
void jugador1 ();
void jugador2 ();



int main (){
int comp,i,j;
cout<<"Bienvenido al 3 en ralla!"<<endl;
cout<<" -----------------------"<<endl;

for (i=0;i<=2; i++){
    for(j=0;j<=2; j++){
        matriz [i][j]='-';
    }
}
do{
    jugador1();

    jugador2();

}
while(comp==0);

cout<<"GRACIAS POR JUGAR <3!";
return 0;
}

void tablero (int i,int j)
{
 cout << matriz[0][0] << " | " << matriz[0][1] << " | " << matriz[0][2] <<endl;
 cout << matriz[1][0] << " | " << matriz[1][1] << " | " << matriz[1][2] <<endl;
 cout << matriz[2][0] << " | " << matriz[2][1] << " | " << matriz[2][2] <<endl;

return;
}

int comprueba (){
  int comp;

    switch (matriz[0][0]==matriz[0][1]==matriz[0][2]){  //horizontal 1
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }

       switch (matriz[1][0]==matriz[1][1]==matriz[1][2]){  //horizontal 2
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }

        switch (matriz[2][0]==matriz[2][1]==matriz[2][2]){  //horizontal 3
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }

       switch (matriz[0][0]==matriz[1][0]==matriz[2][0]){  //vertical 1
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }
       switch (matriz[0][1]==matriz[1][1]==matriz[2][1]){  //vertical 2
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }
       switch (matriz[0][2]==matriz[1][2]==matriz[2][2]){  //vertical 3
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }

       switch (matriz[0][0]==matriz[1][1]==matriz[2][2]){  //oblícua 1
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }

       switch (matriz[0][2]==matriz[1][1]==matriz[2][0]){  //oblícua 2
        case 'X':
            cout<<"Jugador 1 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        case 'O':
            cout<<"Jugador 2 es el ganador! Enhorabuena!"<<endl;
            comp=1;
            break;
        default:
            comp=0;
       }

return comp;
}

void jugador1 (void){
int i, j;
cout<<"Jugador 1, introduce una posicion"<<endl<<"Fila:";
cin>>i;
cout<<endl<<"Columna:"<<endl;
cin>>j;
i=i-1;
j=j-1;

if (i>3 || j>3){
    cout<<"Esta cordenada no existe, introduce otra jugador 1";
}
else{
    matriz [i][j]='X';

    tablero(i,j);

}

comprueba ();
return ;
}

void jugador2 (void){
int i, j;
cout<<"Jugador 2, introduce una posicion"<<endl<<"Fila:";
cin>>i;
cout<<endl<<"Columna:"<<endl;
cin>>j;
i=i-1;
j=j-1;

if (i>3 || j>3){
    cout<<"Esta cordenada no existe, introduce otra jugador 2";
}
else{
    matriz [i][j]='O';

    tablero(i,j);

}

comprueba ();   
return ;
}

Thank you everyone that has reached here, if you have any idea on how to do all off this please explain it to and ill will apreceate it much <3!!

First matriz[1][0]==matriz[1][1]==matriz[1][2] don't do what you dream about. Since it is a == test it evaluates to a bool (and the second == would compare a bool -the result of matriz[1][0]==matriz[1][1] - with a char -the value of matriz[1][2] -; there is no implied conjunction like you want it to happen). You need to read more about C++ operators and their precedence (notably of comparison operators ).

Then a bool can never be equal to 'X' . It can be true or false (and even when that is converted to a char , it will never be 'X' ; on most systems, (char)false is the NUL character; with ASCII or UTF-8 used on nearly every computer, (char)true is the \\001 start-of-heading control character and you probably don't need that.

Don't forget to enable all warnings and debug info in your compiler. If using GCC , compile with g++ -Wall -Wextra -g . Improve your code to get no warnings.

Then read How to debug small programs

My recommendation: stop coding during a few days, and spend them reading more . Read first a good C++ programming book (in full). Look into some C++ reference site. Study how to use your compiler . Learn to use your debugger , and a tiny bit of some version control (like git , which has excellent tutorial videos; you just want to learn a tiny bit of git ). Then come back to your program (you'll better scrap it entirely and write again your code, when you have learnt the basics; you'll probably write a few dozen lines, debug them, and improve your code with another few dozen lines, debug it again, and repeat till satisfied; following an iterative and incremental development approach.).

BTW, it could help to study the source code of some existing small free software project (eg on github , gitlab or in a Linux distribution). That might be inspirational.

PS. Even if that takes you more than an hour, following all the hyperlinks here will be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM