Thread: Compare String

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    6

    Compare String

    In the end, when the word is right it does not leave the program.
    Why?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <locale.h>
    #include <string.h>
    
    
    void login(int utilizador);
    void registo();
    int jogo();
    
    
    int main()
    {
        setlocale(LC_ALL,"");
        char op, utilizador[30];
        do {
            printf("|'''''''''''''''|\n");
            printf("| Jogo da Forca |\n");
            printf("|...............|\n\n");
    
    
            printf("E - Entrar\n");
            printf("R - Registar\n");
            printf("S - Sair\n\n");
            fflush(stdin);
            printf("Escolha uma opção: ");
            scanf(" %c", &op);
            op = toupper(op);
            if (op == 'E')
                {
                system("CLS");
                login(utilizador);
                }
            if (op == 'R')
                {
                system("CLS");
                registo();
                }
            }
        while (op == "S");
        system("EXIT");
    }
    
    
    void login(int utilizador){
        int id;
        char utiliza[30], pass[30];
    
    
        printf("|'''''''''''''''|\n");
        printf("| Jogo da Forca |\n");
        printf("|...............|\n\n");
    
    
        printf("LOGIN\n\n");
        printf("Introduza o seu ID: ");
        scanf(" %d", &id);
        printf("Introduza o Utilizador: ");
        scanf(" %s", &utiliza);
        printf("Introduza a Password: ");
        scanf(" %s", &pass);
        jogo();
        return 0;
    }
    
    
    void registo(){
        char novoutiliza[30], novapass[30];
    
    
        printf("|'''''''''''''''|\n");
        printf("| Jogo da Forca |\n");
        printf("|...............|\n\n");
        printf("REGISTO\n\n");
        printf("Introduza o Utilizador: ");
        scanf(" %s", &novoutiliza);
        printf("Introduza a Password: ");
        scanf(" %s", &novapass);
        printf("\nREGISTADO COM SUCESSO");
        getch();
        system("CLS");
        main();
        return 0;
    }
    
    
    int jogo(){
        char palavra[20], palav[20];
        int cont, i, j=0, certo=0;
        char letra, validar=0, verifica=7;
            system("CLS");
            printf("Introduza a palavra: ");
            scanf(" %s", &palavra);
            system("CLS");
            printf("|---| \n");
            printf("|   | \n");
            printf("|     \n");
            printf("|     \n");
            printf("|     \n");
            printf("|     \n");
            cont = strlen(palavra);
            printf("\nPalavra: ");
            for (i=0; i < cont; i++)
            {
                palav[i] = '_';
            }
            for (i=0; i<cont; i++)
            {
                    printf("%2c", palav[i]);
            }
            do
            {
            validar = 0;
            certo = 0;
            printf("\nLetra: ");
            scanf(" %c", &letra);
                for(i=0; i<cont; i++)
                    {
                        if(palavra[i] == letra)
                            {
                                palav[i] = letra;
                                validar = 1;
                            }
                    }
            if (validar != 1)
            {
                verifica--;
            }
            if (verifica==7)
                    {
                      system("CLS");
                      printf("|---| \n");
                      printf("|   | \n");
                      printf("|     \n");
                      printf("|     \n");
                      printf("|     \n");
                      printf("|     \n");
                    }
    
    
            if (verifica==6)
                    {
                     system("CLS");
                     printf("|---| \n");
                     printf("|   | \n");
                     printf("|   O  \n");
                     printf("|     \n");
                     printf("|     \n");
                     printf("|     \n");
                    }
    
    
            if (verifica==5)
                    {
                     system("CLS");
                     printf("|---|  \n");
                     printf("|   |  \n");
                     printf("|   O  \n");
                     printf("|  /   \n");
                     printf("|      \n");
                     printf("|      \n");
                    }
    
    
            if (verifica==4)
                    {
                     system("CLS");
                     printf("|---|  \n");
                     printf("|   |  \n");
                     printf("|   O  \n");
                     printf("|  /|  \n");
                     printf("|      \n");
                     printf("|      \n");
                    }
    
    
             if (verifica==3)
                    {
                     system("CLS");
                     printf("|---|  \n");
                     printf("|   |  \n");
                     printf("|   O  \n");
                     printf("|  /|\\ \n");
                     printf("|      \n");
                     printf("|      \n");
                    }
    
    
             if (verifica==2)
                    {
                     system("CLS");
                     printf("|---|  \n");
                     printf("|   |  \n");
                     printf("|   O  \n");
                     printf("|  /|\\ \n");
                     printf("|  /   \n");
                     printf("|      \n");
                    }
    
    
             if (verifica==1)
                    {
                     system("CLS");
                     printf("|---|  \n");
                     printf("|   |  \n");
                     printf("|   O  \n");
                     printf("|  /|\\  ");
                     printf("\n|  / \\");
                     printf("\n|    \n");
                     printf("PERDEU!");
                    }
             for(i=0; i<cont; i++)
             {
               if(palavra[i] == palav[i])
                    {
                       certo++;
                    }
             }
            if (verifica > 1)
                {
                    printf("\nPalavra: ");
                    for(i=0; i<cont; i++)
                    {
                        printf("%2c", palav[i]);
                    }
                }
            }
            while(verifica != 1 || certo==cont);
           }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Line #40:
    op is a char, "S" is a string. If you had your compiler's warnings enabled, it would warn you about comparing incompatible types.

    Don't flush stdin.

    Don't use system("EXIT"), it's pointless.

    Don't call main!

    Try to use standard functions whenever possible. For example, your getch calls can be replaced with getchar, with the single difference that the user has to press enter. Of course, you may want to filter out blank characters just in case, something like:
    Code:
    while (isspace(c = getchar()) && c != EOF)
        continue;
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-05-2014, 07:39 AM
  2. string compare
    By ingeniousreader in forum C Programming
    Replies: 7
    Last Post: 03-02-2012, 12:43 PM
  3. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  4. Compare 2 String
    By nitediver in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 07:48 AM
  5. need help on string compare
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-07-2002, 08:55 PM

Tags for this Thread