Zugoldragon
Zugoldragon

Reputation: 15

comparing characters (letters) on c

Im making this program to compare letters. The goal of this is to make a hangman game. But i cant make the letter comparison to work

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

int c=0,x;
char a[50],b[50],n[50],j,h;
char a1[50],b1[50],y;
main()
{
    printf("Player 1:enter a word\n");
    gets(n);
    x=strlen(n);
    printf("%d letters\n",x);

    puts("now enter the word letter by letter");
    do
    {
        gets(a);
        strcat(b,a);
        c++;

    }
    while(c!=x);
    printf("%s",b);
    getch();
    system("cls");

    c=0;

    puts("Player 2:try to guess the word letter by letter");
    do
    {
        gets(a1);
        y=strcmp(a,a1);
        printf("%d",y);
        strcat(b1,a1);
        c++;
    }
    while(c!=x);

    printf("%s",b1);


    getch();
    return 0;

}

im having problems in particular with the player 2 section. This is not supposed to be the whole comparing letters program; this is just comparing letters to whatever is in the player 1 section (but it keeps trowing 1 and -1 and have no idea how to fix this).

Im sure with a little bit more programming knowledge, i should be able to fix this, but im a beginner trying to learn almost on my own. Any tip would be extremely helpful :)

Upvotes: 0

Views: 104

Answers (1)

Michael Chan
Michael Chan

Reputation: 11

In your code, a stores a temporary value of Player1's input, so if Player1 input "abide", the result will be a = "e" after Plyaer1's loop. In Player2's loop, you always compare "e" with user's input.

A solution may be that compares the first character instead of using strcmp

y = b[c] - a1[0];

Upvotes: 1

Related Questions