参数太少的功能。什么意思我出问题在哪里? [英] Too few arguments to function.. What does it mean where do I go wrong?

查看:256
本文介绍了参数太少的功能。什么意思我出问题在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

#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 functuion
    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("innvalid 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之间输入一个数字,我需要的验证功能来验证。谁能给我解释一下我走到哪里错了吗?是什么做的正确方法?

I want the user to enter a number between 1 and 3 and I need the validate function to verify it. Can anyone explain me where I go wrong please? what is the proper way to do it?

推荐答案

验证必须称为:

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

作为你的菜单中有1和3,和你的函数之间的选择应该是:

as your menu has choices between 1 and 3, and your function should be:

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);
}

和可以有只有一个具有该名称的功能,所以删除第二个。顺便说一句,而(0℃,选择4;)中的第二个,必须写成:而(0℃;选种和放大器;&放大器;选择&LT; 4)

and there can be only one function with that name, so remove the second one. Btw, while (0<selection<4) in the second one, must be written as: while (0<selection && selection<4).

编辑:注意,的sscanf 的返回值的检查。它告诉我们是否的sscanf 可以读取指定的值类型,%d个,并注意要通过的地址的整数,其中存储的结果。 取值的零初始化确保虽然有无效输入while循环不会退出。

note the check for the return value of sscanf. It tells us whether sscanf could read the value type specified, %d, and note to pass the address of the integer where to store the result. The initialization of s to zero ensures the while loop will not be exited while there was invalid input.

编辑:固定Chux的评论消耗标准输入

fixed Chux's comment to consume stdin.

这篇关于参数太少的功能。什么意思我出问题在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆