简体   繁体   中英

Debugging my c program, number guessing game

I need help debugging this program and I don't know what is wrong. I am using putty and the Vi editor to run my program. This is my code:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>

int main(void) { 

    int playerNumber = 0;
    int number = 0;
    int playerInput = 0;    
    int guess = 0;
    char input;
    char str[6] = {0};
    int playerA = 0;
    int playerB = 0;
    int passA = 3;
    int passB = 3;
    int i = 0;
    int playerTurn = 0;


    srand(time(NULL));
    playerNumber = 1 + rand() % 2; /* Random number is generated */

    printf("\nPlayer %d goes fist\n", playerNumber);

    printf("Player Number?\n");  


    while (playerNumber != playerInput) {
        scanf("%d", &playerInput);

        if (playerNumber != playerInput) printf("You Have to wait your turn.\nPlayer number?\n");

        playerNumber = playerA;

        if (playerA = 1) playerB = 2;
        else playerB = 1;

        srand(time(NULL));
        number = 0 + rand() % 100; /* Random number is generated */

        printf("Enter Your Guess, 0 - 100 or Pass: "); /* Input your guess */

        while(number != guess) {

        for(i = 1; i < 1000; i++) {
            if (i%2 == 1) playerTurn = playerA;
            else PlayerTurn = playerB;
            scanf("%s", str);

            if (strcmp(str, "pass") == 0) printf("Player Number?\n");
            else {
                guess = atoi(str);
                if(guess < number) /* if the guess is lower, output: the guess is to low */
                    printf("Your guess was to low.\n Player Number:\n ");

                else if(guess > number) /* if the guess is higher, output: the guess is to high */
                    printf("Your guess was to high.\n Player Number:\n ");

                else /* is the guess is equial to the random number: Success!! */
                    printf("Yes!! you got it!\n");
                return 0;
            }
        }
    }
}

}

This is what I get as an error: project2total.c: In function main': project2total.c:49: error: PlayerTurn' undeclared (first use in this function) project2total.c:49: error: (Each undeclared identifier is reported only once project2total.c:49: error: for each function it appears in.)

C is case-sensitive. In your function, there is no PlayerTurn declared, but you seem to have declared playerTurn . Simply correcting the upper-case P to lower-case will work, assuming that this is actually the variable that you want to refer to. :)

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