苏雨辙 2021-11-26 16:29 采纳率: 66.7%
浏览 156
已结题

new后面出错显示应输入标识符

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


int main() {

    char WORDS[10][20] = { "Apple","Banana","Cucumber","Dumpling","Eggplant","Fish","Garlic","Ham","Icecream","Jellies" };
   

    int num;
    printf("Enter the number of words to guess (<=10):");
    scanf("%d", &num);
    int countnum[20];
    int i;

    srand(time(NULL));
   
    for (i = 0; i < num; i++)
    {
        countnum[i] = strlen(WORDS[i]);
        printf("%s\n", WORDS[i]);
    }
    printf("\n");
   

    char again = 'y';
    
    while (again == 'y') {

        int x = rand() % (num);
        int life = 5;
        char guess[20];

        printf("  +------------------+\n");
        printf("  |                  |\n");
        printf("  |                  |\n");
        for (i = 0; i < 10; i++)
        {
            printf("  |\n");
        }
        printf("================================\n");
       


        printf("Word:");

        char new[20];
//这里出错显示应输入标识符
        for (i = 0; i < countnum[x]; i++)
        {
            new[i] = '_';
        }


        for (i = 0; i < countnum[x]; i++)
        {
            printf(" %c", new[i]);
        }
        printf("\n\n");
      


        while (life > 0)
        {
            printf("Guess (%d lives): ", life);
            scanf("%s", guess);
            printf("\n");

            if (strlen(guess) != 1)
               
            {
                if (strlen(guess) == strlen(WORDS[x]))
                {
                    int judge = 0;
                    for (i = 0; i < countnum[x]; i++)
                    {
                        if (guess[i] != WORDS[x][i])
                        {
                            break;
                        }
                        judge++;
                    }
                    if (judge == countnum[x])
                    {
                        printf("Correct!\n");
                        printf("Do you want to play again (y/n)? ");
                        scanf(" %c", &again);
                        break;
                    }
                }
                printf("Word:");
                for (i = 0; i < countnum[x]; i++)
                {
                    printf(" %c", new[i]);
                }
                printf("\n\n");
                life--;
            }
            else
                
            {

                printf("Word:");
                if (strstr(WORDS[x], guess) != NULL)
                  
                {
                    for (i = 0; i < countnum[x]; i++)
                    {
                        if (guess[0] == (char)WORDS[x][i])
                        {
                            new[i] = guess[0];
                        }
                    }
                    int correct = 0;
                    for (i = 0; i < countnum[x]; i++)
                    {
                        if (new[i] != '_')
                        {
                            correct++;
                            continue;
                        }
                        break;
                    }
                    for (i = 0; i < countnum[x]; i++)
                    {
                        printf(" %c", new[i]);
                    }
                    printf("\n\n");
                    if (correct == countnum[x]) {
                        printf("Correct!\n");
                        printf("Do you want to play again? ");
                        scanf("%c", &again);
                        break;
                    }
                }
                else
                {
                    for (i = 0; i < countnum[x]; i++)
                    {
                        printf(" %c", new[i]);
                    }
                    printf("\n\n");
                }
                life--;

                
                if (life == 4)
                {
                    printf("  +------------------+\n");
                    printf("  |                  |\n");
                    printf("  |                  |\n");
                    printf("  |                  O\n");
                    for (i = 0; i < 9; i++)
                    {
                        printf("  |\n");
                    }
                    printf("================================\n");
                }

                if (life == 3)
                {
                    printf("  +------------------+\n");
                    printf("  |                  |\n");
                    printf("  |                  |\n");
                    printf("  |                  O\n");
                    printf("  |                  |\n");
                    for (i = 0; i < 8; i++)
                    {
                        printf("  |\n");
                    }
                    printf("================================\n");
                }

                if (life == 2)
                {
                    printf("  +------------------+\n");
                    printf("  |                  |\n");
                    printf("  |                  |\n");
                    printf("  |                  O\n");
                    printf("  |                 /|\n");
                    for (i = 0; i < 8; i++)
                    {
                        printf("  |\n");
                    }
                    printf("================================\n");
                }

                if (life == 1)
                {
                    printf("  +------------------+\n");
                    printf("  |                  |\n");
                    printf("  |                  |\n");
                    printf("  |                  O\n");
                    printf("  |                 /|\\ \n");
                    for (i = 0; i < 8; i++)
                    {
                        printf("  |\n");
                    }
                    printf("================================\n");
                }
            }
        }
        if (life == 0) {
            printf("  +------------------+\n");
            printf("  |                  |\n");
            printf("  |                  |\n");
            printf("  |                  O\n");
            printf("  |                 /|\\ \n");
            printf("  |                 / \\ \n");
            for (i = 0; i < 7; i++)
            {
                printf("  |\n");
            }
            printf("================================\n");
        }
        printf("You have no more lives!\n");
        printf("Do you want to play again (y/n)? ");
        scanf(" %c", &again);

    }

    exit(1);
}

  • 写回答

1条回答 默认 最新

  • Cat_贰龄依旧 2021-11-26 16:56
    关注

    new 是 C++ 的关键字, 不可以用它作为变量名, 所以编译出错;
    可以将 "char new[20]" 改为 "char New[20]" 或 "char ch_new[20]" 等, 后面的 new 也做相应修改.
    朋友想用动态内存的话, 可以写成:

    char* ch_new = new char[20];
    // 动态分配内存, 变量名叫 ch_new.
    // 最后还要加上:
    delete[] ch_new;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月5日
  • 已采纳回答 11月27日
  • 创建了问题 11月26日

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题