线程1:断点3.1?

时间:2015-04-13 04:53:46

标签: function debugging pass-by-reference breakpoints

为什么我的程序在那时停止了?我想创建一个在复利之后打印平衡的功能,但由于某种原因,它不打印......这就是我所拥有的:

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

1 个答案:

答案 0 :(得分:0)

请考虑以下事项。

/* 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;
    }
}