Hangman Program in Qt

0

Could you help me and tell me what is wrong with the following exercise and how to improve it?

The statement is:

  

In this exercise we will program the game of the simplified hangman. At the beginning we will ask the first player for the secret word and then we will erase the screen with the command system ("cls"); (so that the second player does not see it).

     

The second player will have three opportunities to hit the floor. If in any of the three successful attempts we will congratulate you and the program will end. If it fails all three attempts we will tell you that it has lost and we will show the hidden word. In each attempt we will give the user a clue: in the first attempt we will show the first letter of the chain, in the second attempt the last and in the third the central letter.

#include<stdio.h>
#include<string.h>


int main(void){

    char pj1[50],pa[50],pb[50],pc[50];
    printf("Ha comenzado el juego del ahorcado\n");
    printf ("jugador 1, introduzca la palabra secreta:");
    scanf("%s",pj1);
    system("cls");
    printf("Jugador 2,solo tienes 3 intentos\n");
    printf("Primer intento:(Pista)Empieza por la letra %c:",pj1[0]);
    scanf("%s",pa);

    int a=strcmp(pj1,pa);
    int n=strlen(pj1);

    if( a!=0)
    {
      printf("Segundo intento:(Pista)Termina por la letra %c:",pj1[n-1]);
      scanf("%s",pa);

      a=strcmp(pj1,pa);
    }

    if( a!=0)
    {
      printf("Tercer intento:(Pista)Contiene la letra %c:",pj1[(n-1)/2]);
      scanf("%s",pa);

      a=strcmp(pj1,pa);
    }

    if( pa==0){printf("¡Felicidades, acertaste\n");
    }else{printf("Lo siento, has perdido. La palabra era %s\n",pj1);}

    return 0;
}
    
asked by isabel 13.10.2016 в 20:14
source

1 answer

-1
int a=strcmp(pj1,pa);

printf("Primer intento:(Pista)Empieza por la letra %c:",pj1[0]);
scanf("%s",pa);

In the first line you compare what the no has not yet entered with the word to find and in the third line you ask the user for the word. You are doing it the other way around. First ask the user for the word and then compare that word. So:

printf("Primer intento:(Pista)Empieza por la letra %c:",pj1[0]);
scanf("%s",pa);

int a=strcmp(pj1,pa);

On the other hand, you will not ask the player for a second word if he has hit the first one. And by the same rule you're not going to ask for a third if you've hit the second or first. This means that you do not need a variable for each word:

printf("Primer intento:(Pista)Empieza por la letra %c:",pj1[0]);
scanf("%s",pa);

int a=strcmp(pj1,pa);

if( a!=0)
{
  printf("Segundo intento:");
  scanf("%s",pa); // podemos sobreescribir pa tranquilamente

  a=strcmp(pj1,pa);
}

if( a!=0)
{
  printf("Tercer intento:");
  scanf("%s",pa); // podemos sobreescribir pa tranquilamente

  a=strcmp(pj1,pa);
}

if( a==0)
  // Ha acertado la palabra
else
  // No ha acertado la palabra tras tres intentos

More little things:

printf("Segundo intento:(Pista)Termina por la letra %c:",pj1[-1]);

The vectors range from index 0 to n-1, where n is the size of the vector. -1 is not a valid value because it is out of range (0, n-1). To retrieve the last character of a string you have to locate the end of it (remember that the strings always end with the null character strlen() .) This can be achieved using %code% (which tells you the length of the string) or iterating the vector until finding the end of the string:

int len = strlen(pj1);
char c=pj1[len-1];

int pos=0;
for(; pj[pos+1]!='
int a=strcmp(pj1,pa);

printf("Primer intento:(Pista)Empieza por la letra %c:",pj1[0]);
scanf("%s",pa);
'; ++pos); char c=pj1[pos];

And the same thing happens to you with the character of the middle of the chain ... Let's see if you can take it out on your own.

Pd .: If I edit several times it's because I'm on my mobile phone

    
answered by 13.10.2016 / 20:47
source