簡體   English   中英

在C中調用一個沒有輸出變量的函數

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

這就是現在的樣子,但它告訴我變量'result'未被使用。 為什么是這樣? 我想我已經弄清楚在這個程序中使用了這個函數,但我還是無法完成所有的配置。 還需要一些幫助,謝謝。

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

編輯3

從以下位置更改原型:

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

至:

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

像這樣稱呼它:

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

有關如何在實際功能中修改結果,請參閱下面的最新示例。

我無法創建一個要求的功能。 這就是我需要做的全部。

這定義了一個從main() 調用的函數;
(對於更具體的內容,請具體描述)

編輯

添加了一個傳遞值,計算內容並返回結果的函數。 也許您可以將這些原則應用於您的問題。 (我從你提供的描述中並不完全了解你的需求)

//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