简体   繁体   English

在C中调用一个没有输出变量的函数

[英]Calling a function in C with variables not being outputted

This is how it looks now but it's telling me that the variable 'result' is unused. 这就是现在的样子,但它告诉我变量'result'未被使用。 Why is this? 为什么是这样? I think I've figured out the use of the function in this program but I can't configure everything quite yet. 我想我已经弄清楚在这个程序中使用了这个函数,但我还是无法完成所有的配置。 Still need some help, thanks. 还需要一些帮助,谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

double computeSomething(unsigned int maxDamage, unsigned int minDamage,
                        double playerArmor, double actualDamage);

int main(void){

    double playerHealth, playerArmor, actualDamage, result = 0;
    unsigned int minDamage, maxDamage;

    printf("Enter Player Health:     ");
    scanf("%lf", &playerHealth);
    printf("Enter Player Armor (0.0 - 1.0):   ");
    scanf("%lf", &playerArmor);
    printf("Enter Minimum Damage Dealt:  ");
    scanf("%u", &minDamage);
    printf("Enter Maximum Damage Dealt:   ");
    scanf("%u", &maxDamage);

    srand((unsigned int) time(NULL));

    while (playerHealth > 0) {

        double result = computeSomething(maxDamage, minDamage, playerArmor,     actualDamage);
        playerHealth -= actualDamage;

        printf("Current Player Health:%.2lf\tDamage Dealt: %.2lf\t",      playerHealth, actualDamage);
        if (playerHealth > 0){
            printf("Alive: 1\n");
        } else {
            printf("Alive: 0\n");
        }
    }

    return 1;
}

double computeSomething(unsigned int maxDamage, unsigned int minDamage,
                        double playerArmor, double actualDamage)
{
     actualDamage = ((rand() % (maxDamage-minDamage + 1)) + minDamage) * (1 -     playerArmor);

    return actualDamage;
}

Edit 3 编辑3

Change your prototype from: 从以下位置更改原型:

double computeSomething(unsigned int maxDamage, unsigned int minDamage,
                        double playerArmor, double actualDamage);

To: 至:

double computeSomething(unsigned int maxDamage, unsigned int minDamage,
                        double playerArmor, double *actualDamage);  

Call it like this: 像这样称呼它:

float res;
computeSomething(34, 15, 30.5, &res);  

See my latest example below for how to modify the result in the actual function. 有关如何在实际功能中修改结果,请参阅下面的最新示例。

I can't create a function to call for. 我无法创建一个要求的功能。 That's literally all I need to do. 这就是我需要做的全部。

This defines a function that is called from main() ; 这定义了一个从main() 调用的函数;
(For something more specific, describe it specifically) (对于更具体的内容,请具体描述)

EDIT 编辑

Added a function that is passed values, computes something, and returns results. 添加了一个传递值,计算内容并返回结果的函数。 Perhaps you can apply the principles here to your problem. 也许您可以将这些原则应用于您的问题。 (I do not fully understand your needs from the description you have provided) (我从你提供的描述中并不完全了解你的需求)

//prototypes
void PrintSomething(char *str);
float compute_something(float x, float y, float z);//per clarification in comments above  
void return_through_param(float x, float y, float *result);
int main(void)
{
    PrintSomething("Hello World\n");
    //function that can return something
    float result = compute_something(10.0, 3.5, .002);
    printf("Here are the results: %f\n", result); 

    //for Edit 2:
    return_through_param(10, .0001, &result);
    printf("This is the new result: %f\n", result);

    return 0;
}  

void PrintSomething(char *str)
{
     printf(str);
}
//Edit - Added this function
float compute_something(float x, float y, float z)
{
    return x*y/z;
}  

//Edit 2 - added this function that passes pointer, for output
void return_through_param(float x, float y, float *result)
{
     return *result = x/y;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM