function论证太少..这是什么意思我哪里出错了?

这是我的代码:

#include  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 (s3) printf("innvalid Input, try again:"); } while (s3); return s; } int validate(int low, int high) { int selection; do { scanf("%d", selection); if (selection 3) printf("Invalid Input, try again: "); } while (0<selection<4); return selection; } 

我希望用户输入1到3之间的数字,我需要validationfunction来validation它。 任何人都可以解释我哪里出错了吗? 这样做的正确方法是什么?

您的validate必须被称为:

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

因为你的菜单有1到3之间的选择,你的function应该是:

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

并且只能有一个具有该名称的函数,因此请删除第二个函数。 顺便说一句, while (0第二个中的while (0必须写为: while (0

编辑:注意检查sscanf的返回值。 它告诉我们sscanf是否可以读取指定的值类型%d ,并注意传递存储结果的整数的地址 。 将s初始化为零可确保在输入无效时不会退出while循环。

编辑:修复Chux的注释以消耗stdin。