Discussion:
Seriously stuck...
(too old to reply)
v***@gmail.com
2017-08-12 23:02:38 UTC
Permalink
So here's the thing. I'm not good at coding. Never really will be. Programming in C is not for me. However, for some reason, I'm in a C class and it's mandatory. Don't know why.

This is my assignment.

Loan Calculator Program

Write a program that lets the user enter the principal, annual interest rate, and number of years duration of a loan. The user can then choose to find out what the monthly payment will be and/or print out a loan repayment table. The user should be able to change the values for the principal, annual interest rate, and number of years duration without re-running the program.

A basic menu for this program would look like this:

Big Bank Loan Calculator

1. Enter principal
2. Enter annual interest rate
3. Enter duration of loan
4. Calculate loan payment
5. Display loan repayment table

0. Quit

Sample: If the user entered $10000 for the principal, 9% for the annual interest rate, and 5 years for the duration of the loan, the monthly payment should be $207.58, and the beginning of the repayment table should look like this:

Month Old Balance Payment Interest Principal New Balance
1 10000.00 207.58 75.00 132.58 9867.42
2 9867.42 207.58 74.01 133.58 9733.84
...
12 8485.64 207.58 63.64 143.94 8341.70

Math information you will need:

Here is the formula for computing the loan payment:
payment = principal x rate ________
1 - ( (1+rate) to the power (-m) ) )
Notes:
* rate is the monthly interest rate. For example, a 9% annual rate is 0.75% monthly, which is expressed as 0.0075 when converted to a decimal representation
* m is the number of months; -m is the negative of that number
* there is a pow() function in the math.h library that you can use for computing exponents
* for a $10000 loan over 5 years at 9% (the example above), you'd end up computing: 10000 * 0.0075 / ( 1 - pow(1.0075, -60))

WARNING: You will get a division by zero error if the user sets the interest rate to 0%. Make sure you restrict the annual interest rate to values > 0.

interest paid in a given month = old balance * monthly interest rate
principal paid = payment - interest paid
new balance = old balance - principal paid

NOTE from real life: The formulas given above are for LOANS. In Canada, mortgage payments are calculated slightly differently (interest is calculated "semi-annually, not in advance" -- ask your banker what this means), so don't be alarmed if the calculation given by this program doesn't exactly match your bank's quote on a mortgage payment. (It should match the bank's quote on a regular loan.)

Marks:
The project will be marked out of 25. 17 marks will be for the items listed below. The remaining 8 marks are specific to the project topic and described with each topic.

1. (5 marks) Your code should be clear and easy-to-read. This includes proper indentation, meaningful variable names, ample comments, appropriate use of functions, and good use of constants (#define's). You should have only one statement per line, and there should be blank lines between different sections of your code.

2. (5 marks) Your program should be user friendly. The output should be neatly displayed with accurate spelling. The text should not scroll off the screen before the user has a chance to read it. There should be a clear easy-to-use menu. Input from the user should be checked. The program should work properly when the user gives valid input, and provide useful error messages when the data is invalid.

3. (7 marks) You should make good use of the C programming language. You should have an appropriate variety of data types. Somewhere in your program you should demonstrate that you can use pointers. You should put your function prototypes into a .h file. You must not use global (extern) variables. To communicate variable values between functions, pass them as arguments or return values.

Menu:
2 marks

- Your menu should look very much like the one shown above. If you wish to use letters instead of numbers to label the choices or to centre rather than left-justify the menu, that's fine.

- Your menu should be in a separate function, probably called menu(). menu() should display the menu, get the response from the user, make sure the response is valid, and return the response to the calling function. You may write your menu() function so that it is called like this: choice = menu(); or write it so that it is called like this: menu(&choice); The latter format gives you a chance to demonstrate your ability to user pointers!

Display:
3 marks

- The loan repayment table above was significantly shortened. Yours should include ALL months (right to the end of the repayment period) and should pause after every 12 months so that the user has a chance to read the information before it scrolls off the screen.

Tough Part:
3 marks

- There are a number of ways this program could be improved. Please choose ONE of the following

- Make the math smarter: The calculations of the payment and monthly interest will end of with fractions of pennies. Although it's easy enough to get printf() to DISPLAY only two decimal places, it's a little trickier to get the actual STORED value to have only two decimal places. You can see the round off problem in the second line of the sample repayment table above: The interest and the principal SHOULD add up to the payment, but they're actually over by one cent. The way to solve this is to round to the nearest penny when you compute the monthly payment, and again each time you compute the monthly interest. Here's how to round a value to the nearest penny: payment = (long) (payment*100 + 0.5)/100.0; Be very careful about the placement of the brackets or it won't work! Once you have all these things correct to the penny, you'll notice that the last month won't usually leave a balance of exactly 0.00. Add to or subtract from the last payment amount so that the balance comes to exactly 0.00. (This is what the bank does in real life!)

- Allow the user to print the loan repayment table: When the user asks to display the repayment table, ask whether the information should be displayed on the screen or prepared for printing. Printing directly to the printer works slightly differently on different computers, so rather than actually print to printer, ask the user for a file name to use, and write to that file. The user can later load the file into an editor (e.g., Notepad) and print it from there. Note that you should NOT pause after every 12 entries when you are writing to a file. At the beginning or end of the file, write out what the principal, interest rate, and repayment period were so that if the user has several printouts s/he can tell what each one means.

- Make the menu smarter: Revise the menu so that it looks like this:
Big Bank Loan Calculator

1. Update principal (currently $10000.00)
2. Update annual interest rate (currently 9.00%)
3. Update duration of loan (currently not set)
4. Calculate loan payment
5. Display loan repayment table

0. Quit

To get the menu to display the various values, you will have to pass them to the menu() function. You could use a value of -1 to represent a value that hasn't been set yet since -1 is not a valid choice for any of the values. Your call to menu() will now look something like this: choice = menu(principal, rate, years);


and this is the code I've come up with (keep in mind I DO NOT KNOW what I'm doing really, I understand to a certain point and that's it)

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>

// list of functions

char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
void count_b(void);
void count_c(void);
void count_d(void);
void count_e(void);
int main(void)
{
int choice;
void count(void);


while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : count();
break;
case 'b' : count_b();
break;
case 'c' : count_c();
break;
case 'd' : count_d();
break;
case 'e' : count_e();
break;
default : printf("Program error!\n");
break;
}
fflush(stdin);
}
printf("Bye.\n");

getchar();
return 0;
}

void count(void)
{
float principal; // principal is the loan amount

while(true)
{
printf( "Please enter the loan principal: " );
scanf("%f", &principal );
if ( principal == -1 )
while (getchar() != '\n')
continue;
{
return 0;
}

}
}

void count_b(void)
{
float rate; // rate is the interest amount

while(true)
{
printf("Please enter interest rate: ");
scanf("%f", &rate );
while (getchar() != '\n')
continue;
{
return 0;
}

}

}

void count_c(void)
{
int days; // amount of days int the loan

while(true)
{
printf( "Please enter term of the loan in days: " );

scanf("%d", &days );
while (getchar() != '\n')
continue;
{
return 0;
}

}

}

void count_d(void)
{
// loan payment


}

void count_e(void)
{
int columns;
int rows;

for(rows= 1; rows <=6; row++)
{
for(columns= 1; columns <=4; column++)
{
printf("Month Old Balance Payment Interest Principal New Balance", columns);
}
printf("\n");
}
return 0;
}



char get_choice(void)
{
int ch;

printf("Enter the letter of your choice: \n");
printf("a. Enter principal b. Enter annual interest rate\n");
printf("c. Enter duration of loan d. Calculate loan payment\n");
printf("e. Display loan repayment table q. Quit\n");
ch = get_first();
while ( ( ch < 'a' || ch > 'e') && ch != 'q')
{
printf("Please enter a selection or q (to quit): ");
ch = get_first();
}
return ch;
}

char get_first(void)
{
int ch;

ch = getchar(); // read next character
while (getchar() != '\n')
continue; // skip rest of line
return ch;
}




I honestly feel like I'm failing big time here...
bartc
2017-08-12 23:14:08 UTC
Permalink
Post by v***@gmail.com
and this is the code I've come up with (keep in mind I DO NOT KNOW what I'm doing really, I understand to a certain point and that's it)
void count(void);
void count_b(void);
void count_c(void);
void count_d(void);
void count_e(void);
Try changing these to float count(void) etc. Because presumably they
return numbers.
Post by v***@gmail.com
void count(void)
Change all these too.
Post by v***@gmail.com
void count_b(void)
....
Post by v***@gmail.com
for(rows= 1; rows <=6; row++)
You need rows++ here.
Post by v***@gmail.com
{
for(columns= 1; columns <=4; column++)
And columns++ here.

And this point, the program should compile and run. Whether correctly,
is the next thing for you to figure out.
Post by v***@gmail.com
I honestly feel like I'm failing big time here...
C was an odd choice of language for a task like this as it's more for
technical stuff.
--
bartc
v***@gmail.com
2017-08-12 23:24:47 UTC
Permalink
Bart
7:14 PM (9 minutes ago)
Post by v***@gmail.com
and this is the code I've come up with (keep in mind I DO NOT KNOW what I'm doing really, I understand to a certain point and that's it)
void count(void);
void count_b(void);
void count_c(void);
void count_d(void);
void count_e(void);
Try changing these to float count(void) etc. Because presumably they
return numbers.
Post by v***@gmail.com
void count(void)
Change all these too.
Post by v***@gmail.com
void count_b(void)
....
Post by v***@gmail.com
for(rows= 1; rows <=6; row++)
You need rows++ here.
Post by v***@gmail.com
{
for(columns= 1; columns <=4; column++)
And columns++ here.

And this point, the program should compile and run. Whether correctly,
is the next thing for you to figure out.
Post by v***@gmail.com
I honestly feel like I'm failing big time here...
C was an odd choice of language for a task like this as it's more for
technical stuff.
--
bartc

Wow holy cow that helped. I'm getting somewhere now.


#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>

// list of functions

char get_choice(void);
char get_first(void);
int get_int(void);
float count(void);
float count_b(void);
float count_c(void);
float count_d(void);
float count_e(void);
int main(void)
{
int choice;
float count(void);


while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : count();
break;
case 'b' : count_b();
break;
case 'c' : count_c();
break;
case 'd' : count_d();
break;
case 'e' : count_e();
break;
default : printf("Program error!\n");
break;
}
fflush(stdin);
}
printf("Bye.\n");

getchar();
return 0;
}

float count(void)
{
float principal; // principal is the loan amount

while(true)
{
printf( "Please enter the loan principal: " );
scanf("%f", &principal );
if ( principal == -1 )
while (getchar() != '\n')
continue;
{
return 0;
}

}
}

float count_b(void)
{
float rate; // rate is the interest amount

while(true)
{
printf("Please enter interest rate: ");
scanf("%f", &rate );
while (getchar() != '\n')
continue;
{
return 0;
}

}

}

float count_c(void)
{
int days; // amount of days int the loan

while(true)
{
printf( "Please enter term of the loan in days: " );

scanf("%d", &days );
while (getchar() != '\n')
continue;
{
return 0;
}

}

}

float count_d(void)
{
// loan payment


}

float count_e(void)
{
int columns;
int rows;

for(rows= 1; rows <=6; rows++)
{
for(columns= 1; columns <=4; columns++)
{
printf("Month Old Balance Payment Interest Principal New Balance", columns);
}
printf("\n");
}
return 0;
}



char get_choice(void)
{
int ch;

printf("Enter the letter of your choice: \n");
printf("a. Enter principal b. Enter annual interest rate\n");
printf("c. Enter duration of loan d. Calculate loan payment\n");
printf("e. Display loan repayment table q. Quit\n");
ch = get_first();
while ( ( ch < 'a' || ch > 'e') && ch != 'q')
{
printf("Please enter a selection or q (to quit): ");
ch = get_first();
}
return ch;
}

char get_first(void)
{
int ch;

ch = getchar(); // read next character
while (getchar() != '\n')
continue; // skip rest of line
return ch;
}

now I just need to figure out how to get the amounts to calculate properly and show up in the table. Fewwwwwf
Barry Schwarz
2017-08-13 01:39:27 UTC
Permalink
Post by v***@gmail.com
Bart
7:14 PM (9 minutes ago)
<snip>
Post by v***@gmail.com
--
bartc
Please don't quote signatures (the "-- " and everything that follows.)
It causes problems when other newsreaders try to generate responses.
Post by v***@gmail.com
Wow holy cow that helped. I'm getting somewhere now.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
// list of functions
char get_choice(void);
char get_first(void);
int get_int(void);
float count(void);
float count_b(void);
float count_c(void);
float count_d(void);
float count_e(void);
int main(void)
{
int choice;
float count(void);
while ( (choice = get_choice()) != 'q')
The requirement was to use '0' to quit. Why are you using 'q'?
Post by v***@gmail.com
{
switch (choice)
{
case 'a' : count();
break;
Please consider using a more readable indenting scheme.

count() returns a float. You discard this value. If you didn't want
it, why did you ask the user to enter it? Obviously you do want it.
Maybe it would be a good idea to save the value in a variable that you
could then perform some calculations on. It might also be a good idea
to check the value for validity (for example, 0 in many cases will
cause problems when used in calculations). Any invalid value should
prompt the user for a valid one.
Post by v***@gmail.com
case 'b' : count_b();
break;
case 'c' : count_c();
break;
case 'd' : count_d();
break;
case 'e' : count_e();
break;
default : printf("Program error!\n");
This is not a program error. It is erroneous input from the user.
Post by v***@gmail.com
break;
}
fflush(stdin);
fflush is not defined to work on input streams.
Post by v***@gmail.com
}
printf("Bye.\n");
getchar();
return 0;
}
float count(void)
A name like get_principal would be much more informative.
Post by v***@gmail.com
{
float principal; // principal is the loan amount
while(true)
{
printf( "Please enter the loan principal: " );
scanf("%f", &principal );
if ( principal == -1 )
while (getchar() != '\n')
continue;
{
This pair of braces is misplaced. Consequently, you will execute the
following return regardless of the value of principal.
Post by v***@gmail.com
return 0;
}
}
Once you fix the if statement, you will need to return the value in
principal to the calling function.
Post by v***@gmail.com
}
float count_b(void)
{
float rate; // rate is the interest amount
while(true)
{
printf("Please enter interest rate: ");
scanf("%f", &rate );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
float count_c(void)
{
int days; // amount of days int the loan
You do need to know the number of payments but it is unlikely that
payments will be made daily. The requirement was for yearly payments.
Do you really expect the user to multiply the number of years by 365?
Post by v***@gmail.com
while(true)
{
printf( "Please enter term of the loan in days: " );
scanf("%d", &days );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
float count_d(void)
{
// loan payment
}
float count_e(void)
{
int columns;
int rows;
for(rows= 1; rows <=6; rows++)
Why are you assuming there are only six payments.
Post by v***@gmail.com
{
for(columns= 1; columns <=4; columns++)
{
printf("Month Old Balance Payment Interest Principal New Balance", columns);
Did you really intend to print this line 24 times.
Post by v***@gmail.com
}
printf("\n");
}
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice: \n");
printf("a. Enter principal b. Enter annual interest rate\n");
printf("c. Enter duration of loan d. Calculate loan payment\n");
printf("e. Display loan repayment table q. Quit\n");
ch = get_first();
while ( ( ch < 'a' || ch > 'e') && ch != 'q')
{
printf("Please enter a selection or q (to quit): ");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar(); // read next character
while (getchar() != '\n')
continue; // skip rest of line
return ch;
}
now I just need to figure out how to get the amounts to calculate properly and show up in the table. Fewwwwwf
You have a lot more work to do than just that.
--
Remove del for email
Stefan Ram
2017-08-12 23:34:47 UTC
Permalink
Post by v***@gmail.com
Sample: If the user entered $10000 for the principal, 9% for
the annual interest rate, and 5 years for the duration of the
loan, the monthly payment should be $207.58, and the
Month Old Balance Payment Interest Principal New Balance
1 10000.00 207.58 75.00 132.58 9867.42
main.c

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

#define PRINT(x) printf( #x " = %g\n", x );

int main( void )
{
double const percent = 0.01;
double const principal_in_USD = 10000.0;
int const duration_in_years = 5;
double const annual_rate = 9.0 * percent;
double const monthly_rate = annual_rate / 12.0;
int const duration_in_months = duration_in_years * 12;
double const power = pow( 1.0 + monthly_rate, -duration_in_months );
double const monthly_principal_in_USD = -( principal_in_USD * monthly_rate )/( 1.0 - 1.0/power);
double const monthly_rate_in_USD = principal_in_USD * monthly_rate;
double const monthly_payment = monthly_rate_in_USD + monthly_principal_in_USD;
double const new_balance = principal_in_USD - monthly_principal_in_USD;
PRINT( monthly_rate );
PRINT( power );
PRINT( monthly_payment );
PRINT( monthly_rate_in_USD );
PRINT( monthly_principal_in_USD );
PRINT( new_balance ); }

transcript

monthly_rate = 0.0075
power = 0.6387
monthly_payment = 207.584
monthly_rate_in_USD = 75
monthly_principal_in_USD = 132.584
new_balance = 9867.42
Steve Carroll
2017-08-13 03:07:10 UTC
Permalink
Post by v***@gmail.com
So here's the thing. I'm not good at coding. Never really will be. Programming in C is not for me. However, for some reason, I'm in a C class and it's mandatory. Don't know why.
This is my assignment.
Loan Calculator Program
Write a program that lets the user enter the principal, annual interest rate, and number of years duration of a loan. The user can then choose to find out what the monthly payment will be and/or print out a loan repayment table. The user should be able to change the values for the principal, annual interest rate, and number of years duration without re-running the program.
Big Bank Loan Calculator
1. Enter principal
2. Enter annual interest rate
3. Enter duration of loan
4. Calculate loan payment
5. Display loan repayment table
0. Quit
Month Old Balance Payment Interest Principal New Balance
1 10000.00 207.58 75.00 132.58 9867.42
2 9867.42 207.58 74.01 133.58 9733.84
...
12 8485.64 207.58 63.64 143.94 8341.70
payment = principal x rate ________
1 - ( (1+rate) to the power (-m) ) )
* rate is the monthly interest rate. For example, a 9% annual rate is 0.75% monthly, which is expressed as 0.0075 when converted to a decimal representation
* m is the number of months; -m is the negative of that number
* there is a pow() function in the math.h library that you can use for computing exponents
* for a $10000 loan over 5 years at 9% (the example above), you'd end up computing: 10000 * 0.0075 / ( 1 - pow(1.0075, -60))
WARNING: You will get a division by zero error if the user sets the interest rate to 0%. Make sure you restrict the annual interest rate to values > 0.
interest paid in a given month = old balance * monthly interest rate
principal paid = payment - interest paid
new balance = old balance - principal paid
NOTE from real life: The formulas given above are for LOANS. In Canada, mortgage payments are calculated slightly differently (interest is calculated "semi-annually, not in advance" -- ask your banker what this means), so don't be alarmed if the calculation given by this program doesn't exactly match your bank's quote on a mortgage payment. (It should match the bank's quote on a regular loan.)
The project will be marked out of 25. 17 marks will be for the items listed below. The remaining 8 marks are specific to the project topic and described with each topic.
1. (5 marks) Your code should be clear and easy-to-read. This includes proper indentation, meaningful variable names, ample comments, appropriate use of functions, and good use of constants (#define's). You should have only one statement per line, and there should be blank lines between different sections of your code.
2. (5 marks) Your program should be user friendly. The output should be neatly displayed with accurate spelling. The text should not scroll off the screen before the user has a chance to read it. There should be a clear easy-to-use menu. Input from the user should be checked. The program should work properly when the user gives valid input, and provide useful error messages when the data is invalid.
3. (7 marks) You should make good use of the C programming language. You should have an appropriate variety of data types. Somewhere in your program you should demonstrate that you can use pointers. You should put your function prototypes into a .h file. You must not use global (extern) variables. To communicate variable values between functions, pass them as arguments or return values.
2 marks
- Your menu should look very much like the one shown above. If you wish to use letters instead of numbers to label the choices or to centre rather than left-justify the menu, that's fine.
- Your menu should be in a separate function, probably called menu(). menu() should display the menu, get the response from the user, make sure the response is valid, and return the response to the calling function. You may write your menu() function so that it is called like this: choice = menu(); or write it so that it is called like this: menu(&choice); The latter format gives you a chance to demonstrate your ability to user pointers!
3 marks
- The loan repayment table above was significantly shortened. Yours should include ALL months (right to the end of the repayment period) and should pause after every 12 months so that the user has a chance to read the information before it scrolls off the screen.
3 marks
- There are a number of ways this program could be improved. Please choose ONE of the following
- Make the math smarter: The calculations of the payment and monthly interest will end of with fractions of pennies. Although it's easy enough to get printf() to DISPLAY only two decimal places, it's a little trickier to get the actual STORED value to have only two decimal places. You can see the round off problem in the second line of the sample repayment table above: The interest and the principal SHOULD add up to the payment, but they're actually over by one cent. The way to solve this is to round to the nearest penny when you compute the monthly payment, and again each time you compute the monthly interest. Here's how to round a value to the nearest penny: payment = (long) (payment*100 + 0.5)/100.0; Be very careful about the placement of the brackets or it won't work! Once you have all these things correct to the penny, you'll notice that the last month won't usually leave a balance of exactly 0.00. Add to or subtract from the last payment amount so that the balance comes to exactly 0.00. (This is what the bank does in real life!)
- Allow the user to print the loan repayment table: When the user asks to display the repayment table, ask whether the information should be displayed on the screen or prepared for printing. Printing directly to the printer works slightly differently on different computers, so rather than actually print to printer, ask the user for a file name to use, and write to that file. The user can later load the file into an editor (e.g., Notepad) and print it from there. Note that you should NOT pause after every 12 entries when you are writing to a file. At the beginning or end of the file, write out what the principal, interest rate, and repayment period were so that if the user has several printouts s/he can tell what each one means.
Big Bank Loan Calculator
1. Update principal (currently $10000.00)
2. Update annual interest rate (currently 9.00%)
3. Update duration of loan (currently not set)
4. Calculate loan payment
5. Display loan repayment table
0. Quit
To get the menu to display the various values, you will have to pass them to the menu() function. You could use a value of -1 to represent a value that hasn't been set yet since -1 is not a valid choice for any of the values. Your call to menu() will now look something like this: choice = menu(principal, rate, years);
and this is the code I've come up with (keep in mind I DO NOT KNOW what I'm doing really, I understand to a certain point and that's it)
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
// list of functions
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
void count_b(void);
void count_c(void);
void count_d(void);
void count_e(void);
int main(void)
{
int choice;
void count(void);
while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : count();
break;
case 'b' : count_b();
break;
case 'c' : count_c();
break;
case 'd' : count_d();
break;
case 'e' : count_e();
break;
default : printf("Program error!\n");
break;
}
fflush(stdin);
}
printf("Bye.\n");
getchar();
return 0;
}
void count(void)
{
float principal; // principal is the loan amount
while(true)
{
printf( "Please enter the loan principal: " );
scanf("%f", &principal );
if ( principal == -1 )
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_b(void)
{
float rate; // rate is the interest amount
while(true)
{
printf("Please enter interest rate: ");
scanf("%f", &rate );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_c(void)
{
int days; // amount of days int the loan
while(true)
{
printf( "Please enter term of the loan in days: " );
scanf("%d", &days );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_d(void)
{
// loan payment
}
void count_e(void)
{
int columns;
int rows;
for(rows= 1; rows <=6; row++)
{
for(columns= 1; columns <=4; column++)
{
printf("Month Old Balance Payment Interest Principal New Balance", columns);
}
printf("\n");
}
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice: \n");
printf("a. Enter principal b. Enter annual interest rate\n");
printf("c. Enter duration of loan d. Calculate loan payment\n");
printf("e. Display loan repayment table q. Quit\n");
ch = get_first();
while ( ( ch < 'a' || ch > 'e') && ch != 'q')
{
printf("Please enter a selection or q (to quit): ");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar(); // read next character
while (getchar() != '\n')
continue; // skip rest of line
return ch;
}
I honestly feel like I'm failing big time here...
It was Deplorable Owl who was publicly asking how better to hide his nonsense. Only what Deplorable Owl wants matters to Deplorable Owl. There is nothing you or I can do to change that. It's a chance to cause harm and it is what it is. The Mack has better screencasting programs.

Oh goodness, that is just tons of kvetching.



--
Curious how these posts are made? Email: ***@sandman.net
Pascal J. Bourguignon
2017-08-13 03:34:37 UTC
Permalink
Post by v***@gmail.com
So here's the thing. I'm not good at coding. Never really will
be. Programming in C is not for me. However, for some reason, I'm in a
C class and it's mandatory. Don't know why.
This is my assignment.
The best solution is to fail, go home, and rethink your life.
--
__Pascal J. Bourguignon
http://www.informatimago.com
Keith Thompson
2017-08-13 19:59:05 UTC
Permalink
Post by Pascal J. Bourguignon
Post by v***@gmail.com
So here's the thing. I'm not good at coding. Never really will
be. Programming in C is not for me. However, for some reason, I'm in a
C class and it's mandatory. Don't know why.
This is my assignment.
The best solution is to fail, go home, and rethink your life.
Original poster: Please ignore Pascal's nonsense.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
v***@gmail.com
2017-08-13 20:15:50 UTC
Permalink
I'm ignoring anything that isn't helpful. I've clearly stated that I'm not a coder and this is why I was seeking some advice in the right direction. I've been working on the code more and really the first two posters where who helped. I'm still snagged, but it's all good. Soon I will be done this class...
Richard Heathfield
2017-08-16 13:48:39 UTC
Permalink
Post by Pascal J. Bourguignon
Post by v***@gmail.com
So here's the thing. I'm not good at coding. Never really will
be. Programming in C is not for me. However, for some reason, I'm in a
C class and it's mandatory. Don't know why.
This is my assignment.
The best solution is to fail, go home, and rethink your life.
On first reading, I thought vamporchid's article was going to be a "do
my homework" request. Had it been, your comment would have been a
reasonable one.

But he provided a full explanation of the problem he was having, and
(crucially) he showed what he'd managed to achieve so far. For me,
that's enough that I'm prepared to cut him a little slack.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Steve Carroll
2017-08-13 03:42:13 UTC
Permalink
Post by v***@gmail.com
So here's the thing. I'm not good at coding. Never really will be. Programming in C is not for me. However, for some reason, I'm in a C class and it's mandatory. Don't know why.
This is my assignment.
Loan Calculator Program
Write a program that lets the user enter the principal, annual interest rate, and number of years duration of a loan. The user can then choose to find out what the monthly payment will be and/or print out a loan repayment table. The user should be able to change the values for the principal, annual interest rate, and number of years duration without re-running the program.
Big Bank Loan Calculator
1. Enter principal
2. Enter annual interest rate
3. Enter duration of loan
4. Calculate loan payment
5. Display loan repayment table
0. Quit
Month Old Balance Payment Interest Principal New Balance
1 10000.00 207.58 75.00 132.58 9867.42
2 9867.42 207.58 74.01 133.58 9733.84
...
12 8485.64 207.58 63.64 143.94 8341.70
payment = principal x rate ________
1 - ( (1+rate) to the power (-m) ) )
* rate is the monthly interest rate. For example, a 9% annual rate is 0.75% monthly, which is expressed as 0.0075 when converted to a decimal representation
* m is the number of months; -m is the negative of that number
* there is a pow() function in the math.h library that you can use for computing exponents
* for a $10000 loan over 5 years at 9% (the example above), you'd end up computing: 10000 * 0.0075 / ( 1 - pow(1.0075, -60))
WARNING: You will get a division by zero error if the user sets the interest rate to 0%. Make sure you restrict the annual interest rate to values > 0.
interest paid in a given month = old balance * monthly interest rate
principal paid = payment - interest paid
new balance = old balance - principal paid
NOTE from real life: The formulas given above are for LOANS. In Canada, mortgage payments are calculated slightly differently (interest is calculated "semi-annually, not in advance" -- ask your banker what this means), so don't be alarmed if the calculation given by this program doesn't exactly match your bank's quote on a mortgage payment. (It should match the bank's quote on a regular loan.)
The project will be marked out of 25. 17 marks will be for the items listed below. The remaining 8 marks are specific to the project topic and described with each topic.
1. (5 marks) Your code should be clear and easy-to-read. This includes proper indentation, meaningful variable names, ample comments, appropriate use of functions, and good use of constants (#define's). You should have only one statement per line, and there should be blank lines between different sections of your code.
2. (5 marks) Your program should be user friendly. The output should be neatly displayed with accurate spelling. The text should not scroll off the screen before the user has a chance to read it. There should be a clear easy-to-use menu. Input from the user should be checked. The program should work properly when the user gives valid input, and provide useful error messages when the data is invalid.
3. (7 marks) You should make good use of the C programming language. You should have an appropriate variety of data types. Somewhere in your program you should demonstrate that you can use pointers. You should put your function prototypes into a .h file. You must not use global (extern) variables. To communicate variable values between functions, pass them as arguments or return values.
2 marks
- Your menu should look very much like the one shown above. If you wish to use letters instead of numbers to label the choices or to centre rather than left-justify the menu, that's fine.
- Your menu should be in a separate function, probably called menu(). menu() should display the menu, get the response from the user, make sure the response is valid, and return the response to the calling function. You may write your menu() function so that it is called like this: choice = menu(); or write it so that it is called like this: menu(&choice); The latter format gives you a chance to demonstrate your ability to user pointers!
3 marks
- The loan repayment table above was significantly shortened. Yours should include ALL months (right to the end of the repayment period) and should pause after every 12 months so that the user has a chance to read the information before it scrolls off the screen.
3 marks
- There are a number of ways this program could be improved. Please choose ONE of the following
- Make the math smarter: The calculations of the payment and monthly interest will end of with fractions of pennies. Although it's easy enough to get printf() to DISPLAY only two decimal places, it's a little trickier to get the actual STORED value to have only two decimal places. You can see the round off problem in the second line of the sample repayment table above: The interest and the principal SHOULD add up to the payment, but they're actually over by one cent. The way to solve this is to round to the nearest penny when you compute the monthly payment, and again each time you compute the monthly interest. Here's how to round a value to the nearest penny: payment = (long) (payment*100 + 0.5)/100.0; Be very careful about the placement of the brackets or it won't work! Once you have all these things correct to the penny, you'll notice that the last month won't usually leave a balance of exactly 0.00. Add to or subtract from the last payment amount so that the balance comes to exactly 0.00. (This is what the bank does in real life!)
- Allow the user to print the loan repayment table: When the user asks to display the repayment table, ask whether the information should be displayed on the screen or prepared for printing. Printing directly to the printer works slightly differently on different computers, so rather than actually print to printer, ask the user for a file name to use, and write to that file. The user can later load the file into an editor (e.g., Notepad) and print it from there. Note that you should NOT pause after every 12 entries when you are writing to a file. At the beginning or end of the file, write out what the principal, interest rate, and repayment period were so that if the user has several printouts s/he can tell what each one means.
Big Bank Loan Calculator
1. Update principal (currently $10000.00)
2. Update annual interest rate (currently 9.00%)
3. Update duration of loan (currently not set)
4. Calculate loan payment
5. Display loan repayment table
0. Quit
To get the menu to display the various values, you will have to pass them to the menu() function. You could use a value of -1 to represent a value that hasn't been set yet since -1 is not a valid choice for any of the values. Your call to menu() will now look something like this: choice = menu(principal, rate, years);
and this is the code I've come up with (keep in mind I DO NOT KNOW what I'm doing really, I understand to a certain point and that's it)
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
// list of functions
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
void count_b(void);
void count_c(void);
void count_d(void);
void count_e(void);
int main(void)
{
int choice;
void count(void);
while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : count();
break;
case 'b' : count_b();
break;
case 'c' : count_c();
break;
case 'd' : count_d();
break;
case 'e' : count_e();
break;
default : printf("Program error!\n");
break;
}
fflush(stdin);
}
printf("Bye.\n");
getchar();
return 0;
}
void count(void)
{
float principal; // principal is the loan amount
while(true)
{
printf( "Please enter the loan principal: " );
scanf("%f", &principal );
if ( principal == -1 )
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_b(void)
{
float rate; // rate is the interest amount
while(true)
{
printf("Please enter interest rate: ");
scanf("%f", &rate );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_c(void)
{
int days; // amount of days int the loan
while(true)
{
printf( "Please enter term of the loan in days: " );
scanf("%d", &days );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_d(void)
{
// loan payment
}
void count_e(void)
{
int columns;
int rows;
for(rows= 1; rows <=6; row++)
{
for(columns= 1; columns <=4; column++)
{
printf("Month Old Balance Payment Interest Principal New Balance", columns);
}
printf("\n");
}
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice: \n");
printf("a. Enter principal b. Enter annual interest rate\n");
printf("c. Enter duration of loan d. Calculate loan payment\n");
printf("e. Display loan repayment table q. Quit\n");
ch = get_first();
while ( ( ch < 'a' || ch > 'e') && ch != 'q')
{
printf("Please enter a selection or q (to quit): ");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar(); // read next character
while (getchar() != '\n')
continue; // skip rest of line
return ch;
}
I honestly feel like I'm failing big time here...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Gee, no on saw more forgeries coming. LOL!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.28
Comment: GPGTools - https://gpgtools.org

iQIcBAEBCgAGBQJWJpfjAAoJEC03b6bOr/+drC0P/RZBxcLK8eFZ7QUKjlqJRqfj
66udasAYnovhi+C1bpBxXZIm8Xxg2+JDdVoMfw/d+5R8+KpEXpxK+4y9GB8NuBBz
98+LuNNIus0cXJ6goCyKZj5FUajHYIhgoEAb+O8F0pYMcqv08R39IddgVEQCasDZ
9QPXlQco0QOSD49BArlDa7qgwi17Rs2zkYnw27jEIVEuYlfhqj2T7Mcirwby4JD3
+1Geq5kCdeCtMG+z6gAdth0akrSMlYLAnixbYolEFm6wtg7DW2BrwVo7NTY8Zuy1
UjRrn8JAoHrCec82HulrBBEXC/dhti2SIszDnt1GnZSTB33p1plQgklUmmqN8Qvu
To/TAz07YWAeUHcYSyHzmJuVbL0r9emcQD/AfB8CxAhh+p5F5kc2hfwAOmJ4cGpV
+kIqUp7N1UXqSOXnFyxYz4HJv9/c0UPNXSY2QDPmaWd7AFrB80BcOw2yVS30jQwo
QhBtvoj2t/FJi/G7KnIpGYl3ZFy7QxtNL6PnPW5y46EjzDbNEnMbETTxAzI24QGw
x5xgyhdiyRnpCvyHFd21d09bYOrBHH6qe3NMVTRBPjh1MUaw1E8WhQo916Zvk4jz
pD23EIt9uk79a2aD+EbC/vZHLPGTqlbI1bmYV5KlzJusIO2i939Cs/AhYcdM8mEQ
WqdC0S1YIC7GfYk7riFe
=F3OP
-----END PGP SIGNATURE-----

--
One Smart Penny

Jonas Eklundh
Jorgen Grahn
2017-08-14 11:33:18 UTC
Permalink
Post by v***@gmail.com
This is my assignment.
Big Bank Loan Calculator
1. Enter principal
2. Enter annual interest rate
3. Enter duration of loan
4. Calculate loan payment
5. Display loan repayment table
0. Quit
I think I've mentioned it before, but teachers should really stop
handing out assignments which include implementing 1980s menu systems.
They are boring to write, tedious to test and use, and (like here)
usually have little to do with the core of the exercise.

A command-line parser or unit test shell could easily be provided by
the teacher.

/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Loading...