2016-03-14 70 views
-3

我是編程新手,最近決定學習C.每當我在Dev C++中編譯和運行我的程序時,輸出始終爲0.000000。我究竟做錯了什麼?我到處看看,但無法解決這個問題:輸出始終爲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; 
} 
+2

看看'main'。您有兩個變量。如果你打印一個你從未設置過的變量的值,你打算打印什麼? – immibis

+2

另外,其中一個賦值語句是:monthlyFee == 89.99 ;而應該是每月FEE = 89.99; –

+0

您通過使用具有自動存儲持續時間的未初始化變量的值調用*未定義行爲*,這是不確定的。 – MikeCAT

回答

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; 
} 
+0

這似乎解決了它,請問爲什麼浮點數需要初始化爲0.0f? –

相關問題