C语言 参数太少,无法正常工作,,这是什么意思,我哪里出错了?

mgdq6dx1  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(118)

下面是我的代码:

#include <stdio.h>

int validate(int low, int high);

int main ()
{
    //Declare Variables
    float strength, speed, defense, intelligence;
    float strengthRatio, speedRatio, defenseRatio, intelligenceRatio;
    int strength_F, speed_F, defense_F, intelligence_F;
    float sum;
    int luck;
    float PStrength=10;
    float PDefense=20;
    float PIntelligence=40;
    int PHP=10;
    float EStrength=30;
    float EDefense=40;
    float EIntelligence=25;
    int EHP=10;
    int sel;
    float attackp;
    float magicp;
    int low=1;
    int high=3;
    int Valid_Selection;

    //Clear Screen
    system("cls");

    //Display Logo
    printf("+----------------------+\n");
    printf("|                      |\n");
    printf("|     CODE QUEST       |\n");
    printf("|                      |\n");
    printf("+----------------------+\n\n");

    //Main Menu
    printf("--Main Menu--\n");
    printf("\n");
    printf("1 - New Game\n");
    printf("2 - Load Game\n");
    printf("3 - Exit\n");
    printf("\n");
    printf("Selection: \n\n");

    // Validate user input using a function
    Valid_Selection = validate(int low,int high);

    // Game_Selection = validate();

    printf ("Character Creation \n");
    printf ("Please enter your desired stats for your character:\n");
    printf ("\n");
    printf ("Enter strength: ");
    scanf  ("%f", &strength);
    printf ("Enter speed: ");
    scanf  ("%f", &speed);
    printf ("Enter defense: ");
    scanf  ("%f", &defense);
    printf ("Enter intelligence: ");
    scanf  ("%f", &intelligence);

    // Calculate the sum of all attributes
    sum = strength + speed + defense + intelligence;

    // Calculate ratios for each attribute
    strengthRatio = strength / sum;
    speedRatio = speed / sum;
    defenseRatio = defense / sum;
    intelligenceRatio = intelligence / sum;

    // Calculate the final Stats as whole number
    strength_F = strengthRatio * 100;
    speed_F = speedRatio * 100;
    defense_F = defenseRatio * 100;
    intelligence_F = intelligenceRatio * 100;

    // Calculate the player's luck
    luck = (int)sum % 30;

    // Create an empty line for beauty
    printf ("\n");

    // Display to the user the finalized player stats
    printf ("Your player's final stats are:");
    printf ("\n");
    printf ("Strength: %d\n", strength_F);
    printf ("Speed: %d\n", speed_F);
    printf ("Defense: %d\n", defense_F);
    printf ("Intelligence: %d\n", intelligence_F);
    printf ("Luck: %d\n", luck);

    // Display "Battle Starts"
    printf("Battle Start!\n\n");

    while (PHP >= 0 && EHP >= 0) {
        //Display user and enemy HP and asks user which attack execute
        printf("Your HP: %d, Enemy HP: %d\n", PHP, EHP);
        printf("1 - Attack Power\n");
        printf("2 - Magic Power\n");
        scanf("%d",&sel);

        //Option 1, Option 2
        if (sel == 1) {
            attackp = (PStrength / EDefense) * 5;
            EHP = EHP - attackp;
            printf("You attacked the enemy\n");
        }
        else {
            magicp = (PIntelligence / EIntelligence) * 5;
            EHP = EHP - magicp;
            printf("You bewitched the enemy!\n");
        }

        // Enemy reaction based on his own HP
        if (EHP <= 0)
            printf("You won!\n");
        else {
            attackp = (EStrength / PDefense) * 5;
            PHP = PHP - attackp;
            printf("The enemy attacked you\n");
        }

        // Indicates if user lost the game
        if (PHP <= 0)
            printf("You lost!\n");

        printf("\n");
    }

  return 0;

    }

    int validate(int low, int high) {
    int s;

    do{
    scanf("%d", s);
    if (s<1 || s>3)
        printf("invalid Input, try again:");
    } while (s<1 || s>3);

    return s;
    }

    int validate(int low, int high) {
        int selection;

        do {
        scanf("%d", selection);
        if (selection <0 || selection>3)
            printf("Invalid Input, try again: ");
        } while (0<selection<4);

        return selection;
    }

字符串
我想让用户输入一个1到3之间的数字,我需要验证功能来验证它。谁能解释一下我哪里出错了吗?什么是正确的方法?

50pmv0ei

50pmv0ei1#

您的validate必须被称为:

//Validate user input using a functuion
Valid_Selection = validate(1, 3);

字符串
因为你的菜单有1到3个选项,你的功能应该是:

int validate(int low, int high) {
int s=0;
char buf[128];

    do {
        if (fgets(buf,128,stdin)==0 || sscanf(buf, "%d", &s)!=1 || (s<low || s>high))
            printf("invalid Input, try again:");
        else
            return s;
    } while (1);
}


并且只能有一个函数具有该名称,因此删除第二个。顺便说一下,第二个中的while (0<selection<4)必须写成:while (0<selection && selection<4)
编辑:注意检查sscanf的返回值。它告诉我们sscanf是否可以读取指定的值类型%d,并注意传递整数的 address 以存储结果。将s初始化为零可以确保在有无效输入时不会退出while循环。
编辑:修正了Chux的评论,以消耗标准输入。

相关问题