【问题标题】:Thread 1: Breakpoint 3.1?线程1:断点3.1?
【发布时间】:2015-06-18 08:03:15
【问题描述】:

为什么我的程序会在此时停止?我想创建一个在复利后打印余额的函数,但由于某种原因,它没有打印......这就是我所拥有的:

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

int printBalance(double initial, double interest, double years);
int main() {
    double initial, interest, i, years;
    printf("Enter initial deposit: ");
    scanf("%lf", &initial);
    printf("Enter percent interest rate: ");
    scanf("%lf", &interest);
    printf("Enter the number of years: ");
    scanf("%lf", &years);
    for (i = 0; i < years; i++) {
        printBalance(initial, interest, i);
    }
}

int printBalance(double initial, double interest, double years) {
    double  balance;
    balance = initial * (pow((1 + interest/100), years));
    printf("%lf", balance);
    return 0;
}

【问题讨论】:

    标签: function debugging pass-by-reference breakpoints


    【解决方案1】:

    考虑以下内容。

    /* This program calculates the balance after compound interest. The user is
     * prompted to enter an initial amount, percent interest, and a number of
     * years. This program outputs the year number, principal amount at the
     * start of that year, the amount compounded, and the balance after the
     * compounding. */
    
    #include <stdio.h>                      //includes input/output library
    #include <stdlib.h>                     //includes general purpose functions
    #include <math.h>                       //includes math functions
    #include <string.h>                     //includes string operations
    
    void printBalance();                    //enables printBalance function
    int main() {                            //main function
        while (1) {                         //infinite loop--never leave main
            char x[1];                      //initializes 1-character string x
            do {                            //do-while loop: do 'this' while
                                            //'these conditions apply;
                printBalance();             //invokes printBalance function--
                                              call by reference
                printf("\nTry again? Y or N? ");
                scanf("%s", x);             //prompt user to input character
                                              into x
            }
            while (strcmp(x, "Y") == 0);    //if the difference of the ASCII
                                              values of x and "Y" is 0,
                                              reiterate the loop
            return 0;
        }
    }
    
    void printBalance() {                   //create printBalance function
        int i;                              //initialize integer i and the 
                                              following doubles
        double initial, interest, years, temp, ci, cii, balance;
        printf("Enter initial deposit: ");
        scanf("%lf", &initial);             //prompt user to input initial
                                              deposit
        printf("Enter percent interest rate: ");
        scanf("%lf", &interest);            //prompt user to input percent
                                              interest
        printf("Enter the number of years: ");
        scanf("%lf", &years);               //prompt user to imput number of
                                              years
        printf("\nYear\t\tInitial\t\tCompound Interest\t\tBalance\n");
        printf("----\t\t-------\t\t-----------------\t\t-------\n");
        temp = initial;                     //create temporary variable as a
                                              mask
        for (i = 1; i <= years + 1; i++) {  //print compounded value for each 
                                              year iterated
            ci = initial * (pow((1 + interest/100), years)-1);
            balance = initial * pow((1 + interest/100), i);
            cii = balance - temp;           //another mask variable
            printf("%d\t\t\t%.2lf\t\t%.2lf\t\t\t\t\t%.2lf\n", i, temp, cii,
                     balance);
            temp = balance;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2012-05-06
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多