我到处都看,但无法解决这个问题:输出总是0.000000

时间:2016-03-14 02:15:28

标签: c floating-point

我是编程新手,最近决定学习C.每当我在Dev C ++中编译和运行我的程序时,输出总是0.000000。我做错了什么?

顺便说一下,我看过类似的帖子,我已经尝试了一切,但它无法解决。请帮忙:(

#include <stdio.h>
#include <conio.h>

void printHeader();
float basicFee();
float additionalCharges();
float printFees(float monthlyFee, float addFee);

int main() {
    float monthlyFee;
    float addFee;
    printHeader();
    basicFee();
    additionalCharges();
    printFees(monthlyFee, addFee);
    return 0;
}

void printHeader() {
    printf("Welcome to Tolba Health Care\nWe care about you!");
    printf("\nMonthly fees are:\n\t$59.99 for a single membership\n\t$69.99 for a couple membership\n\t$89.99 for a family membership");
    printf("\n\nWe also offer:\n\tTennis for $18.99 a month\n\tGlf for $21.99 a month\n\tRacquetball for $15.99 a month");
    printf("\n\nWe are running a special -- if you pay\nfor six months in advance, you receive 10%% off!");
}

float basicFee() {
    char typeMemb;
    float monthlyFee;

    printf("\nWould you like a:\n(S) Single Membership\n(C) Couple Membership\n(F) Family Membership");
    scanf(" %c", &typeMemb);
    if (typeMemb == 'S' || typeMemb == 's') {
        monthlyFee = 59.99;
    } else
    if (typeMemb == 'C' || typeMemb == 'c') {
        monthlyFee = 69.99;
    } else
    if (typeMemb == 'F' || typeMemb == 'f') {
        monthlyFee == 89.99;
    } else {
        printf("\nPlease enter a valid form of membership.");
    }
    return monthlyFee;
}

float additionalCharges() {
    char tennisOpt, golfOpt, racOpt;
    float tennisFee, golfFee, racFee;
    printf("\nWould you like to add any of the following? (Y) for yes, (N) for no");
    printf("\n\tTennis?: ");
    scanf(" %c", &tennisOpt);
    if (tennisOpt == 'Y' || tennisOpt == 'y') {
        tennisFee = 18.99;
    } else
    if (tennisOpt == 'N' || tennisOpt == 'n') {
        tennisFee = 0.0;
    } else {
        printf("Please enter a valid answer (Yes or No)");
    }

    printf("\n\tGolf?: ");
    scanf(" %c", &golfOpt);

    if (golfOpt == 'Y' || golfOpt == 'y') {
        golfFee = 21.99;
    } else
    if (golfOpt == 'N' || golfOpt == 'n') {
        golfFee = 0.0;
    } else {
        printf("Please enter a valid answer (Yes or No)");
    }

    printf("\n\tRacquetball?: ");
    scanf(" %c", &racOpt);

    if (racOpt == 'Y' || racOpt == 'y') {
        racFee = 15.99;
    } else
    if (racOpt == 'N' || racOpt == 'n'){
        racFee = 0.0;
    } else {
        printf("Please enter a valid answer (Yes or No)");
    }   
    float addFee = tennisFee + golfFee + racFee;
    return addFee;
}

float printFees(float monthlyFee, float addFee) {
    char discountOpt;
    float oneMonth = monthlyFee + addFee, sixMonths = oneMonth * 6,
          discount = sixMonths * .10, rdiscount,
          totalDue = sixMonths - rdiscount;
    printf("\nMonthly Fee: $%f", monthlyFee);
    printf("\nAdditional Fees: $%f", addFee);
    printf("\nTotal for one month: $%f", oneMonth);
    printf("\n\nReceive a 10%% discount - $%f by paying for six months advance", discount);
    printf("\nWithout discount, six months would cost $%f", sixMonths);
    printf("\nWould you like to receive a discount?");
    scanf(" %c", &discountOpt);

    if (discountOpt == 'Y' || discountOpt == 'y') {
        rdiscount = discount;
    } else
    if (discountOpt == 'N' || discountOpt == 'n') {
        rdiscount = 0.0;
    } else {
        printf("Please enter a valid answer (Yes or No)");
    }

    printf("Total due is $%f", totalDue);
    printf("\nThank you for joining our club!");

    return;
}

1 个答案:

答案 0 :(得分:1)

您需要对代码进行一些更改。赋值运算符是=,而您使用了==(应该用于比较)。接下来,您的函数声明表明它们将返回值,但您不会将这些返回值存储在main中(因此您将获得0.000000作为输出)。

#include <stdio.h>

void printHeader();
float basicFee();
float additionalCharges();
float printFees(float monthlyFee, float addFee);

int main(){
    float monthlyFee=0.0f;
    float addFee=0.0f;
    printHeader();
    monthlyFee=basicFee();
    addFee=additionalCharges();
    printFees(monthlyFee, addFee);
    return 0;
}

void printHeader()
{
    printf("Welcome to Tolba Health Care\nWe care about you!");
    printf("\nMonthly fees are:\n\t$59.99 for a single membership\n\t$69.99 for a couple membership\n\t$89.99 for a family membership");
    printf("\n\nWe also offer:\n\tTennis for $18.99 a month\n\tGlf for $21.99 a month\n\tRacquetball for $15.99 a month");
    printf("\n\nWe are running a special -- if you pay\nfor six months in advance, you recieve 10%% off!");
}

float basicFee(){
    char typeMemb;
    float monthlyFee;
    printf("\nWould you like a:\n(S) Single Membership\n(C) Couple Membership\n(F) Family Membership");
    scanf(" %c", &typeMemb);
    if (typeMemb=='S' || typeMemb=='s'){
        monthlyFee=59.99;
    }
    else if (typeMemb=='C' || typeMemb=='c'){
        monthlyFee=69.99;
    }
    else if (typeMemb=='F' || typeMemb=='f'){
        monthlyFee=89.99;
    }
    else{
        printf("\nPlease enter a valid form of membership.");
    }
    return monthlyFee;
}

float additionalCharges(){
    char tennisOpt, golfOpt, racOpt;
    float tennisFee, golfFee, racFee;
    printf("\nWould you like to add any of the following? (Y) for yes, (N) for no");
    printf("\n\tTennis?: ");
    scanf(" %c", &tennisOpt);
    if (tennisOpt=='Y' || tennisOpt=='y'){
        tennisFee=18.99;
    }
    else if (tennisOpt=='N' || tennisOpt=='n'){
        tennisFee=0.0;
    }
    else{
        printf("Please enter a valid answer (Yes or No)");
    }

    printf("\n\tGolf?: ");
    scanf(" %c", &golfOpt);

    if (golfOpt=='Y' || golfOpt=='y'){
        golfFee=21.99;
    }
    else if (golfOpt=='N' || golfOpt=='n'){
        golfFee=0.0;
    }
    else{
        printf("Please enter a valid answer (Yes or No)");
    }

    printf("\n\tRacquetball?: ");
    scanf(" %c", &racOpt);

    if (racOpt=='Y' || racOpt=='y'){
        racFee=15.99;
    }
    else if (racOpt=='N' || racOpt=='n'){
        racFee=0.0;
    }
    else{
        printf("Please enter a valid answer (Yes or No)");
    }   
    float addFee = tennisFee + golfFee + racFee;
    return addFee;
}

float printFees(float monthlyFee, float addFee){
    char discountOpt;
    float oneMonth = monthlyFee + addFee, sixMonths = oneMonth * 6, discount = sixMonths * .10, rdiscount, totalDue = sixMonths - rdiscount;
    printf("\nMonthly Fee: $%f", monthlyFee);
    printf("\nAdditional Fees: $%f", addFee);
    printf("\nTotal for one month: $%f", oneMonth);
    printf("\n\nReceive a 10%% discount - $%f by paying for six months advance", discount);
    printf("\nWithout discount, six months would cost $%f", sixMonths);
    printf("\nWould you like to receive a discount?");
    scanf(" %c", &discountOpt);

    if (discountOpt=='Y' || discountOpt=='y'){
        rdiscount=discount;
    }
    else if (discountOpt=='N' || discountOpt=='n'){
        rdiscount=0.0;
    }
    else{
        printf("Please enter a valid answer (Yes or No)");
    }

    printf("Total due is $%f", totalDue);
    printf("\nThank you for joining our club!");
    return 0;
}