Hi could someone please help me with my code? I want to calculate an accumulate investment after a given amount of years, months, or days. I am reading in an annual interest from the user and would like to have this converted into monthly and daily interest based on the user's chosen type of compounding. I am using an if statement to do this. However, when I run the program, why do I received 3 sets of values for the 3 different types of compounding? I want to receive only 1 set, but I don't know what am I doing wrong?

``````/* File: account.c
*     * Date: February 2, 2010 */

/* This program calculates the accumulated value of a given initial investment and annual interest, given the options of either annual, monthly, or daily
* compounding */

#include <stdio.h>

/* Function declaration */
float calc_acc_amt(int x, float acc_amount, float annual_interest, int y);

main()
{
/* Variable declarations */
float initial; /* Input initial value of investment */
float interest; /* Input annual interest */
float input; /* User input of initial value and annual interest */
int type; /* Input type of compounding */
int start = 1; /* Used for start of loop in function */
float acc_amt; /* Accumulated amount of investment invoked by function */
float monthly_interest; /* Modified interest due to chosen input of monthly compounding */
float daily_interest; /* Modified interest due to chosen input of daily compounding */
int length; /* Length of compounding */

/* Program intro */
printf("This program will calculate the accumulated value of your investment at an annual, monthly, or daily compounding\n");

/* Obtain input from user */
printf("Please enter the following data, seperated by spaces.\n");
printf("Initial value, annual interest: ");
input = scanf("%f %f", &initial, &interest);

printf("Type of compounding: (1) annually, (2) monthly, (3) daily: ");
input = scanf("%d", &type);

printf("Length of compounding: ");
scanf("%d", &length);

while (input != EOF) /* Initialize loop */
{       if (type = 1)
{       /* Invoke function */
acc_amt = calc_acc_amt(start, initial, interest, length);

}
if (type = 2)
{
monthly_interest = (interest)/(12);
acc_amt = calc_acc_amt(start, initial, monthly_interest, length);
}
if (type = 3)
{
daily_interest = (interest)/(365);
acc_amt = calc_acc_amt(start, initial, daily_interest, length);
}
if (type != 1 || type != 2 || type != 3)
{
break;
}

/* Update loop */
printf("Initial value, annual interest: ");
input = scanf("%f %f", &initial, &interest);

printf("Type of compounding: (1) annually, (2) monthly, (3) daily: ");
input = scanf("%d", &type);
}

} /* End main */

/* Function */

float calc_acc_amt(int x, float acc_amount, float annual_interest, int y)

{float value;
while (x <= y)
{ printf("%d: ", x);
if (x != 1) /* when x is not equal to 1 */
{
value = value + (value * annual_interest);
printf("Total accumulated value is \$%f\n", value); }

else /* when x = 1 */
value = (acc_amount + (acc_amount * annual_interest));
{ printf("Total accumulated value is \$%f\n", value); }
x = x + 1;
}
}``````

First, you need a better approach to indentation.

``````/* File: account.c
*     * Date: February 2, 2010 */

/* This program calculates the accumulated value of a given initial investment and annual interest, given the options of either annual, monthly, or daily
* compounding */

#include <stdio.h>

/* Function declaration */
float calc_acc_amt(int x, float acc_amount, float annual_interest, int y);

main()
{
/* Variable declarations */
float initial;              /* Input initial value of investment */
float interest;             /* Input annual interest */
float input;                /* User input of initial value and annual interest */
int type;                   /* Input type of compounding */
int start = 1;              /* Used for start of loop in function */
float acc_amt;              /* Accumulated amount of investment invoked by function */
float monthly_interest;     /* Modified interest due to chosen input of monthly compounding */
float daily_interest;       /* Modified interest due to chosen input of daily compounding */
int length;                 /* Length of compounding */

/* Program intro */
printf
("This program will calculate the accumulated value of your investment at an annual, monthly, or daily compounding\n");

/* Obtain input from user */
printf("Please enter the following data, seperated by spaces.\n");
printf("Initial value, annual interest: ");
input = scanf("%f %f", &initial, &interest);

printf("Type of compounding: (1) annually, (2) monthly, (3) daily: ");
input = scanf("%d", &type);

printf("Length of compounding: ");
scanf("%d", &length);

while (input != EOF) {      /* Initialize loop */
if (type = 1) {         /* Invoke function */
acc_amt = calc_acc_amt(start, initial, interest, length);

}
if (type = 2) {
monthly_interest = (interest) / (12);
acc_amt =
calc_acc_amt(start, initial, monthly_interest, length);
}
if (type = 3) {
daily_interest = (interest) / (365);
acc_amt = calc_acc_amt(start, initial, daily_interest, length);
}
if (type != 1 || type != 2 || type != 3) {
break;
}

/* Update loop */
printf("Initial value, annual interest: ");
input = scanf("%f %f", &initial, &interest);

printf
("Type of compounding: (1) annually, (2) monthly, (3) daily: ");
input = scanf("%d", &type);
}

}                               /* End main */

/* Function */

float calc_acc_amt(int x, float acc_amount, float annual_interest, int y)
{
float value;
while (x <= y) {
printf("%d: ", x);
if (x != 1) {           /* when x is not equal to 1 */
value = value + (value * annual_interest);
printf("Total accumulated value is \$%f\n", value);
}

else                    /* when x = 1 */
value = (acc_amount + (acc_amount * annual_interest));
{
printf("Total accumulated value is \$%f\n", value);
}
x = x + 1;
}
}``````

What's happening at line 83 ? Is something meant to be in those braces?

The next step is use a compiler with a good range of diagnostics.

``````\$ gcc -W -Wall -O2 foo.c
foo.c:13: warning: return type defaults to ‘int’
foo.c: In function ‘main’:
foo.c:41: warning: suggest brackets around assignment used as truth value
foo.c:45: warning: suggest brackets around assignment used as truth value
foo.c:50: warning: suggest brackets around assignment used as truth value
foo.c:38: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
foo.c: In function ‘calc_acc_amt’:
foo.c:89: warning: control reaches end of non-void function
foo.c: In function ‘main’:
foo.c:68: warning: control reaches end of non-void function
foo.c: In function ‘calc_acc_amt’:
foo.c:74: warning: ‘value’ may be used uninitialised in this function``````

The first few all refer to those `if (type = 1)` where you should be using `if (type =[B]=[/B] 1)`

Your `if` statements

``````if (type = 1)
if (type = 2)
if (type = 3)``````

are setting `type` to the values 1, 2, 3 and each `if` is executed because of that. Look up the comparison operators again.