C编程strcmp in if语句未评估为true

时间:2019-02-27 00:04:54

标签: c

我是一个正在制作迷你游戏大厅的年轻编码员,如果if语句为true,则不管我做什么都没关系,它循环3次,然后继续不与循环交互。感谢所有的帮助,我正努力向朋友们展示。我已经查看了strcmp的内容,但仍然无法弄清楚自己在做什么错。每次从头开始重新启动时,我已经进行了几天的工作,这也是我第一次使用strcmp。

   for(i = 1; i <= 3; i++)
    {
    do{
    system("cls");
    printf("\t\t\t\t\tZack's Miraculus MiniGames\n\n\n");
    printf("What Is Your Choice? Rock, Paper or Scissors\n");
    printf("Enter Your Answer Here: ");
    scanf("%s", choice);
    }while (strcmp(choice,Rock) != 0 && strcmp(choice,Paper) != 0 && strcmp(choice,Scissors) != 0);


    do
    {
    computer = (rand() %3) + 1;

    if(computer == 1)
    {
        system("cls");
        printf("\t\t\t\t\tZack's Miraculus MiniGames\n\n\n");
        printf("Computer Chose Rock\n");
        char computerString[30] = "Rock";
    }

    else if(computer == 2)
    {
        system("cls");
        printf("\t\t\t\t\tZack's Miraculus MiniGames\n\n\n");
        printf("Computer Chose Paper\n");
        char computerString[30] = "Paper";
    }

    else
    {
        system("cls");
        printf("\t\t\t\t\tZack's Miraculus MiniGames\n\n\n");
        printf("Computer Chose scissors\n");
        char computerString[30] = "Scissors";
    }
    }while (thing != 0);


    if(strcmp(choice, computerString) == 0)
    {
        printf("It Is A Draw!\n");
        printf("Type Anything To Continue: ");
        scanf("%s", placeHolder);
    }

    else if(strcmp(choice, "Rock") == 0 && strcmp(computerString, "Scissors") == 0)
    {
        printf("Winner, Winner Chicken Dinner!\n");
        score++;
        printf("Type Anything To Continue: ");
        scanf("%s", placeHolder);
    }

    else if(strcmp(choice, "Paper") == 0 && strcmp(computerString, "Rock") == 0)
    {
        printf("Man Will Always Beat Machine! (You Win)\n");
        score++;
        printf("Type Anything To Continue: ");
        scanf("%s", placeHolder);
    }

    else if(strcmp(choice, "Scissors") == 0 && strcmp(computerString, "Paper") == 0)
    {
        printf("You Win!\n");
        score++;
        printf("Type Anything To Continue: ");
        scanf("%s", placeHolder);
    }

    else if(strcmp(choice, "Scissors") == 0 && strcmp(computerString, "Rock") == 0)
    {
        printf("Better Luck Next Time, Computer Wins!\n");
        printf("Type Anything To Continue: ");
        scanf("%s", placeHolder);
        computerScore++;
    }

    else if(strcmp(choice, "Rock") == 0 && strcmp(computerString, "Paper") == 0)
    {
        printf("Computer Beats %s\n", name);
        printf("Type Anything To Continue: ");
        scanf("%s", placeHolder);
        computerScore++;
    }

    else if(strcmp(choice, "Paper") == 0 && strcmp(computerString, "Scissors") == 0){
        printf("Computer Prevails!\n");
        printf("Type Anything To Continue: ");
        scanf("%s", placeHolder);
        computerScore++;
    }

    }

    printf("The Final Score Is %s %d and Computer %d", name, score, computerScore);

}

2 个答案:

答案 0 :(得分:1)

            else
            {
                system("cls");
                printf("\t\t\t\t\tZack's Miraculus MiniGames\n\n\n");
                printf("Computer Chose scissors\n");
                char computerString[30] = "Scissors";
            }
            }while (thing != 0);


            if(strcmp(choice, computerString) == 0)
            {
                printf("It Is A Draw!\n");
                printf("Type Anything To Continue: ");
                scanf("%s", placeHolder);
            }

char computerString[30] = "Scissors";创建一个变量,其作用域是创建该变量的块。以下}结束了该块,因此该变量不再存在。在代码的更下方,当您将computerString传递给strcmp时,在作用域中没有该名称的变量。

要修复此问题,请在代码的前面(包含所有需要访问它的代码的范围内)创建computerString。使用strcpy设置computerString的值。

您的编译器警告您,您为computerString分配了一个值,然后在不使用该值的情况下超出了范围。

答案 1 :(得分:0)

我继续创建了一个小样本,可以为您解决一些问题。它并不能做所有的事情,但是确实向您展示了如何正确比较字符串等。

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

int main(int argc, char *argv[])
{
    char choice[10];
    char *computerChoices[] = {"Rock", "Paper", "Scissors"};
    char *computerChoice = NULL;

    /* Use current time as seed for random generator */
    srand(time(0));     

    do
    {
        printf("\t\t\t\t\tZack's Miraculus MiniGames\n\n\n");
        printf("What Is Your Choice? Rock, Paper or Scissors\n");
        printf("Enter Your Answer Here: ");
        scanf("%s", choice);
    }while (strcmp(choice, computerChoices[0]) != 0 && strcmp(choice, computerChoices[1]) != 0 && strcmp(choice, computerChoices[2]) != 0);

    computerChoice = computerChoices[rand()%3];
    printf("Computer Chose %s\n", computerChoice);

    if(strcmp(choice, computerChoice) == 0)
    {
        printf("It Is A Draw!\n");
    }
    return(0);
}

希望这会有所帮助。