C 若有,其他及;错误时(未激活/激活两次)

C 若有,其他及;错误时(未激活/激活两次),c,xcode,macos,osx-yosemite,C,Xcode,Macos,Osx Yosemite,当玩家选择攻击时,我遇到了这个问题。它一开始不会打印任何内容,但只会在玩家击中骑士时打印。另外,如果玩家选择了一个无效的性别选择,第一个错误将打印一次,但如果错误再次发生,它将打印两次。我会感激任何帮助,只是做我的lil'项目 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdbool.h> #include <unistd.h> #include

当玩家选择攻击时,我遇到了这个问题。它一开始不会打印任何内容,但只会在玩家击中骑士时打印。另外,如果玩家选择了一个无效的性别选择,第一个错误将打印一次,但如果错误再次发生,它将打印两次。我会感激任何帮助,只是做我的lil'项目

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char enter = 0;
    char pNAME[30];
    char pGENDER;
    char pWEAPON[20];
    char pARMOR [10];
    int  pWEAPONID;
    int choice1,choice2 = 0,choice3;
    int pAGE;
    int pATK = 0;
    int pHP = 30;
    int pATKS = 10;
    int pATKC = 0;
    int knightHP = 25;
    int knightATKS = 20;
    int knightATKC;
    int kingHP = 30;
    int kingATKC;
    srand((unsigned)time(NULL));


    printf("While you are enjoying your breakfast at your house, suddenly you get teleported to a different dimension and no one to be seen except an old man.\n");

    printf("\nOld Man: What is your name warrior?\n");
    printf("\nInsert name:");
    scanf("%s",pNAME);
    fpurge( stdin );

    printf("\nInsert gender (m/f):");
    scanf("%c",&pGENDER);
    fpurge( stdin );
    while ( pGENDER != 'f' &&pGENDER != 'm' )
    {
        printf("\nInvalid entry, please try again.\n");
        printf("\nInsert gender (m/f):");
        scanf("%c",&pGENDER);
    }

    printf("\nOld Man: Now how old are you warrior?\n");
    printf("\nInsert Age:");
    scanf("%d",&pAGE);
    fpurge( stdin );
    if ( pAGE < 18)
    {
        printf("\nOy mate! No stalker!\n");
        printf("\nTeleporting back to reality...\n");

        return 0;
    }

    if ( pAGE > 80)
    {
        printf("\nNo oldies allowed!\n");
        printf("\nTeleporting back to reality...\n");

        return 0;
    }

    printf("\nOld Man: Welcome warrior I fear the knights has taken over our kingdom, and you, %s, are the only one who can save us.\n",pNAME);
    printf("\n%s: How am I suppose to do that?[Enter]\n",pNAME);
    while (enter != '\r' && enter != '\n') { enter = getchar(); }


    printf("Old Man: Pick one of these three items\n");
    printf("\n[1] Axe : Damage = 7\t   Accuracy = 70%%  Critical = 7%%\n");   //
    printf("\n[2] Sword : Damage = 5\t Accuracy = 77%%  Critical = 8%%\n");
    printf("\n[3] Knife : Damage = 3\t Accuracy = 83%%  Critical = 9%%\n");
    printf("\nYou choose: ");
    scanf("%d",&pWEAPONID);

    if (pWEAPONID == 1)
    {
        pATK = 8;
        pATKS = 14;     // 10/14 chance
        strcpy(pWEAPON, "Axe");
    }
    if (pWEAPONID == 2)
    {
        pATK = 6;
        pATKS = 14;     // 10/13 chance
        strcpy(pWEAPON, "Sword");
    }
    if (pWEAPONID == 3)
    {
        pATK = 4;
        pATKS = 12;     // 10/12 chance
        strcpy(pWEAPON, "Knife");
    }
    if (pWEAPONID > 3)
    {
        printf("\nYou are gonna be fighting with your bare fist");
        pATK = 2;
        pATKS = 11;      // 10/11 chance
        strcpy(pWEAPON, "Fist");
    }

    printf("\nOld Man: Good choice, you chose the %s, now continue foward to destroy The Evil King\n",pWEAPON);
    printf("\nWhile you are continuing your journey to destroy The Evil King, you come face to face with a knight\n");
    printf("\nWhat do you want to do?\n");
    printf("\n[1] Run [2] Battle:");
    scanf("%d",&choice1);

    if (choice1 == 1) //Dead
    {
        printf("\nYou attempted to run, but the knight chases you with his horse and a spear.\n");
        printf("\nThe knight stabs you right through your body.\n");
        printf("\nYou are dead, thanks for playing.\n");
        return 0;
    }


    while (pHP != 0 && knightHP != 0) // Combat starts
    {   
        knightATKC = rand()%knightATKS;     // 15/20 chance
        sleep(1);
        // Knight's turn
        if ( knightATKC == 1 || knightATKC == 2 || knightATKC == 3 || knightATKC == 4 || knightATKC == 5 || knightATKC == 6 || knightATKC == 7 || knightATKC == 8 || knightATKC == 9 || knightATKC == 10 || knightATKC == 11 || knightATKC == 0 )
        {
            printf("\nThe knight swings his spear at %s\n",pNAME);
            printf("The knight hits %s for 4 HP\n",pNAME);
            pHP = pHP - 4;
            printf("%s has %d/30 HP\n",pNAME,pHP);
            if (pHP == 0 || pHP <0)
            {
                printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
                printf("\nYou are dead, thanks for playing.\n");
                return 0;
            }
            printf("\nWhat do you want to do?\n");
            printf("[1] Attack [2] Charge your attack [3] Heal:"); // Charge and Heal not activated yet
            scanf("%d",&choice1);

        }
        else if (knightATKC == 12 || knightATKC == 13 )
        {
            printf("\nThe knight slashes his spear at %s\n",pNAME);
            printf("The knight critically hit %s for 6 HP\n",pNAME);
            pHP = pHP - 6;
            printf("%s has %d/30 HP\n",pNAME,pHP);
            if (pHP == 0 || pHP <0)
            {
                printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
                printf("\nYou are dead, thanks for playing.\n");
                return 0;
            }
            printf("\nWhat do you want to do?\n");
            printf("[1] Attack [2] Charge your attack [3] Heal:"); // Charge and Heal not activated yet
            scanf("%d",&choice2);
        }
        else if (knightATKC == 14)
        {
            printf("\nThe knight's horse kicks %s\n",pNAME);
            printf("The knight's horse hit %s for 8 HP\n",pNAME);
            pHP = pHP - 8;
            printf("%s has %d/30 HP\n",pNAME,pHP);
            if (pHP == 0 || pHP <0)
            {
                printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
                printf("\nYou are dead, thanks for playing.\n");
                return 0;
            }
            printf("\nWhat do you want to do?\n");
            printf("[1] Attack [2] Charge your attack [3] Heal:");
            scanf("%d",&choice2);
        }
        else
        {
            printf("\nThe knight swings his spear at %s\n",pNAME);
            printf("You blocked his spear with your %s\n",pWEAPON);
            printf("%s has %d/30 HP\n",pNAME,pHP);
            printf("\nWhat do you want to do?\n");
            printf("[1] Attack [2] Charge your attack [3] Heal:"); // Charge and Heal not activated yet
            scanf("%d",&choice2);
        }

        // Player's turns
        if (choice2 == 1)
        {
            pATKC = rand()%pATKS;
            if (pATKC == 0 || pATKC == 1 || pATKC == 2 || pATKC == 3 || pATKC == 4 || pATKC == 5 || pATKC == 6 || pATKC == 7 || pATKC == 8 || pATKC == 9)
            {
                printf("\nYou swing your %s at the knight\n",pWEAPON);
                printf("You hit the knight with your %d\n",pATK);
                knightHP = knightHP - pATK;
                printf("The knight has %d/25\n",knightHP);
                if (knightHP == 0 || knightHP <0)
                {
                    printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
                    printf("\nYou are dead, thanks for playing.\n");
                }
                printf("\nWhat do you want to do?\n");
                printf("[1] Run [2] Attempt to block\n");
                scanf("%d",&choice3);
            }
            else if (pATKC == 10)
            {
                printf("\nYou swing your %s at the knight\n",pWEAPON);
                printf("You critically hit the knight for %f\n",pATK * 1.5);
                knightHP = knightHP - (pATK * 1.5);
                printf("The knight has %d/25\n",knightHP);
                printf("\nWhat do you want to do?\n");
                printf("[1] Run [2] Attempt to block\n");
                scanf("%d",&choice3);
            }
            else
            {
                printf("\nYou swing your %s at the knight\n",pWEAPON);
                printf("The knight intercept your %s with his spear\n",pWEAPON);
                printf("The knight has %d/25\n",knightHP);
                printf("\nWhat do you want to do?\n");
                printf("[1] Run [2] Attempt to block\n"); // Not activated yet
                scanf("%d",&choice3);
            }
        }
    }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
字符输入=0;
char-pNAME[30];
煤焦;
char-pWEAPON[20];
char-pARMOR[10];
int-pWEAPONID;
int choice1,choice2=0,choice3;
整版;
int-pATK=0;
int pHP=30;
int-pATKS=10;
int-pATKC=0;
int-knightHP=25;
int knightATKS=20;
国际骑士团;
int kingHP=30;
int kingATKC;
srand((无符号)时间(NULL));
printf(“当你在家里享用早餐时,突然你被传送到一个不同的维度,除了一个老人之外,没有人可以看到。\n”);
printf(“\nOld-Man:你叫什么名字?\n”);
printf(“\n插入名称:”);
scanf(“%s”,pNAME);
fpurge(stdin);
printf(“\n插入性别(男/女):”;
scanf(“%c”、&pGENDER);
fpurge(stdin);
而(pGENDER!=“f”&&pGENDER!=“m”)
{
printf(“\n无效条目,请重试。\n”);
printf(“\n插入性别(男/女):”;
scanf(“%c”、&pGENDER);
}
printf(“\nOld-Man:现在你多大了?\n”);
printf(“\n插入时间:”);
scanf(“%d”,第页(&P));
fpurge(stdin);
如果(第18页)
{
printf(“\nOy mate!无跟踪者!\n”);
printf(“\n回归现实…\n”);
返回0;
}
如果(第80页)
{
printf(“\n不允许使用旧版本!\n”);
printf(“\n回归现实…\n”);
返回0;
}
printf(“\n诺尔德人:欢迎勇士,我担心骑士们已经占领了我们的王国,而你,%s,是唯一能救我们的人。\n”,pNAME);
printf(“\n%s:我该怎么做?[输入]\n”,pNAME);
while(enter!='\r'&&enter!='\n'){enter=getchar();}
printf(“老人:从这三项中选择一项\n”);
printf(“\n[1]斧头:损坏=7\t精度=70%%临界=7%%\n”)//
printf(“\n[2]剑:伤害=5\t精度=77%%临界=8%%\n”);
printf(“\n[3]刀:损坏=3\t精度=83%%临界=9%%\n”);
printf(“\n您选择:”);
scanf(“%d”和&pWEAPONID);
if(pWEAPONID==1)
{
pATK=8;
pATKS=14;//10/14机会
strcpy(pWEAPON,“Axe”);
}
if(pWEAPONID==2)
{
pATK=6;
pATKS=14;//10/13机会
strcpy(pWEAPON,“剑”);
}
如果(pweApponid==3)
{
pATK=4;
pATKS=12;//10/12机会
strcpy(pWEAPON,“刀”);
}
如果(pWEAPONID>3)
{
printf(“\n你将赤手空拳地战斗”);
pATK=2;
pATKS=11;//10/11机会
strcpy(pWEAPON,“Fist”);
}
printf(“\nOld Man:不错的选择,你选择了%s,现在继续前进以摧毁邪恶的国王”,pWEAPON);
printf(“\n当你继续你的消灭邪恶国王的旅程时,你与一名骑士\n面对面”;
printf(“\n您想做什么?\n”);
printf(“\n[1]运行[2]战斗:”);
scanf(“%d”和选项1);
if(choice1==1)//死
{
printf(“\n您试图逃跑,但骑士用他的马和长矛追赶您。\n”);
printf(“\n骑士刺伤你的身体。\n”);
printf(“\n您已经死了,谢谢您的演奏。\n”);
返回0;
}
而(pHP!=0&&knightHP!=0)//战斗开始
{   
knightATKC=rand()%knightATKS;//15/20机会
睡眠(1);
//轮到骑士了
如果(knightATKC==1 | | knightATKC==2 | | knightATKC==3 | | knightATKC==4 | | knightATKC==5 | | knightATKC==6 | knightATKC==7 | knightATKC==8 | knightATKC==9 | knightATKC==10 | knightATKC==11 |
{
printf(“\n骑士在%s\n处挥舞长矛”,pNAME);
printf(“骑士命中%s获得4点生命\n”,pNAME);
pHP=pHP-4;
printf(“%s具有%d/30 HP\n”,pNAME,pHP);

如果(pHP==0 | | pHP发布的代码没有编译,那么它肯定不会运行

下面是代码中列出的许多问题中的一些

编译时始终启用所有警告

对于gcc,至少使用:
-Wall-Wextra-pedantic

line:column: message

35:5:  warning implicit declaration of function `fpurge`
224:5: error: expected declaration or statement at end of input
27:9:  warning: unused variable `kingATKC`
26:9:  warning: unused variable `kingHP`
15:10: warning: unused variable `pARMOR`
顺便说一句:
fpurge
在stdio.h头文件中不受支持

建议使用以下命令,而不是调用
fpurge()

while( '\n' != getchar() );
调用scanf()时,始终检查返回值,而不是参数值,以确保操作成功

输入“性别”时,将
tolower
应用于该值,以便用户可以输入大写或小写

此代码:在232行附近,当它应该杀死骑士时,它正在杀死英雄

            if (knightHP == 0 || knightHP <0)
            {
                printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
                printf("\nYou are dead, thanks for playing.\n");
            }
仅当HP==0时终止循环,它应为:

while (pHP > 0 && knightHP > 0) // Combat starts
建议,对于战斗序列,只有一个地方英雄可以输入他们下一步想做什么的决定

建议,将每个作战活动作为单独的子功能


以上两个建议将大大提高每个函数的简单性,并使代码更易于调试。

请提供一个调试:断点、单步执行、检查变量值。与基本相同代码上的其他基本相同。如果是重构,将使调试和理解代码更容易分解成几个函数,每个函数处理问题的一小部分。
while (pHP > 0 && knightHP > 0) // Combat starts