473,408 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

C Programming using vi editor :(

22
I am currently working on a lab for my computer science class, and we are to write program (using a function) to calculate the monthly payment. We also are reading the principal loan, interest rate, and number of payments from a file, and then passing this values to the function which returns the "monthly payment". Then we write the monthly payment to another file. After this we end the program.

Here is what I have so far. My problem is when I open the file to see if my program wrote the correct monthly payment. When I read the file I see "The monthly payment is $NaN". I know this is telling me that it is not receiving a correct number and that is what the "NaN" is representing, but my question is why. Someone please look at this code and tell me what I might have done wrong. Thank you!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> /* preprocessor directive for printf, scanf */
  2. #include <math.h>  /* preprocessor directive for math operators */
  3.  
  4. double calculatePayment ( );
  5.  
  6. main ()
  7.  
  8. {
  9.      double Princ, interest, n, monthlyPayment, mI; /* variables for main */
  10.      FILE* in; /* assigns in as a file pointer */
  11.      FILE* out; /* assings out as a file pointer */
  12.  
  13.     /* open files */
  14.     in = fopen( "lab2.in", "r" );
  15.  
  16.     /* read in principal, annual interest rate, and total number of payments */
  17.     fscanf ( in, "%f%f%f", &Princ, &interest, &n);
  18.     fclose(in);
  19.  
  20.     mI = interest / 12;
  21.  
  22.     /* call calculatePayment to receive payment ammount */
  23.     monthlyPayment = calculatePayment( Princ, mI, n);
  24.  
  25.     /* write monthlyPayment to lab2.out file */
  26.     out = fopen ( "lab2.out", "w" );
  27.     fprintf( out, "The monthly payment is $%.2lf", monthlyPayment );
  28.  
  29.     /* close files, end program */
  30.     fclose(out);
  31.     return(0);
  32. }
  33.     /*
  34.          Function name:  calculatePayment
  35.          Parameters:  Principal loan ammount, interest rate, number of payments
  36.          Returns:  Monthly payment for loan
  37.     */
  38.  
  39.     /* Function heading */
  40.     double calculatePayment ( double Principal, double monthlyInterest, double numberPayments)
  41. {
  42.  
  43.     double payment;
  44.  
  45.     payment = (monthlyInterest*Principal)/(pow(1-(1+monthlyInterest), numberPayments))*-1;
  46.  
  47.     return (payment);
  48. }
  49.  
Feb 20 '07 #1
26 5191
sicarie
4,677 Expert Mod 4TB
I am currently working on a lab for my computer science class, and we are to write program (using a function) to calculate the monthly payment. We also are reading the principal loan, interest rate, and number of payments from a file, and then passing this values to the function which returns the "monthly payment". Then we write the monthly payment to another file. After this we end the program.

Here is what I have so far. My problem is when I open the file to see if my program wrote the correct monthly payment. When I read the file I see "The monthly payment is $NaN". I know this is telling me that it is not receiving a correct number and that is what the "NaN" is representing, but my question is why. Someone please look at this code and tell me what I might have done wrong. Thank you!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> /* preprocessor directive for printf, scanf */
  2. #include <math.h>  /* preprocessor directive for math operators */
  3.  
  4. double calculatePayment ( );
  5.  
  6. main ()
  7.  
  8. {
  9.      double Princ, interest, n, monthlyPayment, mI; /* variables for main */
  10.      FILE* in; /* assigns in as a file pointer */
  11.      FILE* out; /* assings out as a file pointer */
  12.  
  13.     /* open files */
  14.     in = fopen( "lab2.in", "r" );
  15.  
  16.     /* read in principal, annual interest rate, and total number of payments */
  17.     fscanf ( in, "%f%f%f", &Princ, &interest, &n);
  18.     fclose(in);
  19.  
  20.     mI = interest / 12;
  21.  
  22.     /* call calculatePayment to receive payment ammount */
  23.     monthlyPayment = calculatePayment( Princ, mI, n);
  24.  
  25.     /* write monthlyPayment to lab2.out file */
  26.     out = fopen ( "lab2.out", "w" );
  27.     fprintf( out, "The monthly payment is $%.2lf", monthlyPayment );
  28.  
  29.     /* close files, end program */
  30.     fclose(out);
  31.     return(0);
  32. }
  33.     /*
  34.          Function name:  calculatePayment
  35.          Parameters:  Principal loan ammount, interest rate, number of payments
  36.          Returns:  Monthly payment for loan
  37.     */
  38.  
  39.     /* Function heading */
  40.     double calculatePayment ( double Principal, double monthlyInterest, double numberPayments)
  41. {
  42.  
  43.     double payment;
  44.  
  45.     payment = (monthlyInterest*Principal)/(pow(1-(1+monthlyInterest), numberPayments))*-1;
  46.  
  47.     return (payment);
  48. }
  49.  
One of the first things I see (before I compile), is that you return a 0 to main. If you do this, you need to use 'int main() ' at the intro to main.

(Also, if you could use the code tags - they are [ code] and [ /code] (without the spaces), it makes your program much easier to read. Thanks!)
Feb 20 '07 #2
sicarie
4,677 Expert Mod 4TB
One of the first things I see (before I compile), is that you return a 0 to main. If you do this, you need to use 'int main() ' at the intro to main.

(Also, if you could use the code tags - they are [ code] and [ /code] (without the spaces), it makes your program much easier to read. Thanks!)
And from there, the error is in your "pow" statement. (hint: count the parentheses)
Feb 20 '07 #3
canteyn
22
And from there, the error is in your "pow" statement. (hint: count the parentheses)
when I counted it seemed to be fine, but I did recognize that I did not have one to close the "x value" in my pow statement ( pow(x, y) ). I corrected that, but now it gives me an error stating that I have to few arguments.
Feb 20 '07 #4
sicarie
4,677 Expert Mod 4TB
when I counted it seemed to be fine, but I did recognize that I did not have one to close the "x value" in my pow statement ( pow(x, y) ). I corrected that, but now it gives me an error stating that I have to few arguments.
Apologies - I should have said 'by the pow statement' I didn't know where you wanted to end it - can you post the new pow statement (so we're both working with the same code)?
Feb 20 '07 #5
RedSon
5,000 Expert 4TB
Please post your build output so we can see the error.
Feb 20 '07 #6
canteyn
22
when I counted it seemed to be fine, but I did recognize that I did not have one to close the "x value" in my pow statement ( pow(x, y) ). I corrected that, but now it gives me an error stating that I have to few arguments.
ok I am an idiot, I just fixed the argument issue, but I am still receiving NaN in my output file.
Feb 20 '07 #7
RedSon
5,000 Expert 4TB
When you debug this what is your monthlyPayment after the call to calculatePayment? If it is a valid number then your problem is in fprintf( out, "The monthly payment is $%.2lf", monthlyPayment );
Feb 20 '07 #8
canteyn
22
Expand|Select|Wrap|Line Numbers
  1.     /*
  2.          Function name:  calculatePayment
  3.          Parameters:  Principal loan ammount, interest rate, number of payments
  4.          Returns:  Monthly payment for loan
  5.     */
  6.  
  7.     /* Function heading */
  8.     double calculatePayment ( double Principal, double monthlyInterest, double numberPayments)
  9. {
  10.  
  11.     double payment;
  12.  
  13.     payment = (monthlyInterest*Principal)/pow((1-(1+monthlyInterest)), numberPayments)*-1;
  14.  
  15.     return (payment);
  16. }
  17.  
here is the function including pow statement as it is currently.


When I compile I am typing

"gcc lab2.c -lm"

and then I am checking the "lab2.out" file for the result, which in the file states

"The monthly payment is $NaN"

Is that what you were asking for Redson?
Feb 20 '07 #9
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1.     /*
  2.          Function name:  calculatePayment
  3.          Parameters:  Principal loan ammount, interest rate, number of payments
  4.          Returns:  Monthly payment for loan
  5.     */
  6.  
  7.     /* Function heading */
  8.     double calculatePayment ( double Principal, double monthlyInterest, double numberPayments)
  9. {
  10.  
  11.     double payment;
  12.  
  13.     payment = (monthlyInterest*Principal)/pow((1-(1+monthlyInterest)), numberPayments)*-1;
  14.  
  15.     return (payment);
  16. }
  17.  
here is the function including pow statement as it is currently.


When I compile I am typing

"gcc lab2.c -lm"

and then I am checking the "lab2.out" file for the result, which in the file states

"The monthly payment is $NaN"

Is that what you were asking for Redson?
I'm guessing that you're overflowing. Can you calculate the parts separately, and output them to see if they're correct?
Feb 20 '07 #10
RedSon
5,000 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1.     /*
  2.          Function name:  calculatePayment
  3.          Parameters:  Principal loan ammount, interest rate, number of payments
  4.          Returns:  Monthly payment for loan
  5.     */
  6.  
  7.     /* Function heading */
  8.     double calculatePayment ( double Principal, double monthlyInterest, double numberPayments)
  9. {
  10.  
  11.     double payment;
  12.  
  13.     payment = (monthlyInterest*Principal)/pow((1-(1+monthlyInterest)), numberPayments)*-1;
  14.  
  15.     return (payment);
  16. }
  17.  
here is the function including pow statement as it is currently.


When I compile I am typing

"gcc lab2.c -lm"

and then I am checking the "lab2.out" file for the result, which in the file states

"The monthly payment is $NaN"

Is that what you were asking for Redson?
No I want the output from gcc try using the verbose flag to see all the compiler junk. But don't worry about it now you fixed the complier problems so I don't need to see the compiler output.
Feb 20 '07 #11
sicarie
4,677 Expert Mod 4TB
I'm still getting 'undefined reference to pow()'.
Feb 20 '07 #12
canteyn
22
yea there is something wrong with my function, I am pretty sure of that. However, I have no idea where is the world it is messing up, because I am pretty sure that it is not returning the proper value back to my main code, I am at a complete loss.
Feb 20 '07 #13
sicarie
4,677 Expert Mod 4TB
yea there is something wrong with my function, I am pretty sure of that. However, I have no idea where is the world it is messing up, because I am pretty sure that it is not returning the proper value back to my main code, I am at a complete loss.
So what is your algorithm for calculating the payment? (maybe we can work through it and find the error or find a workaround)
Feb 20 '07 #14
RedSon
5,000 Expert 4TB
Have you stepped through you code using a debugger to see what is going on?
Feb 20 '07 #15
canteyn
22
I am unsure on how to debug at all I am sorry.

My algorithm is as follows

1. Open file lab2.in
2. Read in principal loan
3. Read in annual interest rate
4. Read in total number of payments
5. Close file lab2.in
6. monthlyInterest = annualrate / 12
7. Call “calculatePayment” function to calculate
8. Read in “payment” from “calculatePayment” function
9. Open file lab2.out
10. Write the results of “calculatePayment” function to file
11. Close file lab2.out
12. Close program

Lab 2 Function Algorithm (calculatePayment)

1. Parameters: P = principal, i = monthly interest rate, n = total number of payments (number of months in payment term)
2. Read in principal loan, annual interest rate, total number of payments
3. Calculate to find loan payment:

iP
Payment = ------------
(1-(1+i) ‾ ⁿ
4. Return “payment” to main


My data in the "lab2.in" file reads as follows:

18300 0.0575 36

My data in the "lab2.out" file SHOULD read as follows:

The monthly payment is $554.65

but is $NaN
Feb 20 '07 #16
RedSon
5,000 Expert 4TB
I'm not sure how to debug using gcc. You might try googling on that, being able to use a debugger is invaluable when trying to solve problems.
Feb 20 '07 #17
canteyn
22
I'm not sure how to debug using gcc. You might try googling on that, being able to use a debugger is invaluable when trying to solve problems.
I agree I hate gcc!
Feb 20 '07 #18
nmadct
83 Expert
I'm not sure how to debug using gcc. You might try googling on that, being able to use a debugger is invaluable when trying to solve problems.
To debug with gcc, the first step is to compile with the -g switch to turn on debugging.

The most basic way to debug is using gdb, the command-line debugger. It's powerful enough but has a steep learning curve.

The other way is using an IDE, you can try KDevelop on Linux or DevC++ on Windows. I believe these actually use gdb as the backend for their debuggers.
Feb 20 '07 #19
nmadct
83 Expert
Also, there's a graphical front-end to gcc, it's called ddd. Probably not as convenient as a full IDE but easier and faster than gdb.
Feb 20 '07 #20
RedSon
5,000 Expert 4TB
I agree I hate gcc!
LOL I didnt say I hated gcc!
Feb 20 '07 #21
canteyn
22
LOL I didnt say I hated gcc!
ok well I said I hate gcc :)

and I have to turn this in in 1 hour, lol, this is not looking so well for me.
Feb 20 '07 #22
sicarie
4,677 Expert Mod 4TB
ok well I said I hate gcc :)

and I have to turn this in in 1 hour, lol, this is not looking so well for me.
I think you're mixing up order of operations (I was wondering why you were adding 1 and then subtracting one right away).

Can you do the operations one step at a time?
Feb 20 '07 #23
sicarie
4,677 Expert Mod 4TB
(For example: )
Expand|Select|Wrap|Line Numbers
  1. payment = 1+monthlyInterest;
  2.  
  3. payment = pow(payment, n);
  4.  
  5. payment = 1 - payment;
  6.  
Feb 20 '07 #24
canteyn
22
(For example: )
Expand|Select|Wrap|Line Numbers
  1. payment = 1+monthlyInterest;
  2.  
  3. payment = pow(payment, n);
  4.  
  5. payment = 1 - payment;
  6.  
Ok well I tried that, and I am no longer getting $NaN, rather now I am getting $-0.00
Feb 20 '07 #25
sicarie
4,677 Expert Mod 4TB
Ok well I tried that, and I am no longer getting $NaN, rather now I am getting $-0.00
Did you do outputs after each one to see if they were computing the correct numbers (if they were even getting passed correctly)?
Feb 20 '07 #26
DeMan
1,806 1GB
my apologies if someone already suggested it but BEWARE the implicit cast......

If you are calculating with doubles use values like 1.0 rather than 1, otherwise funny things might happen when the compiler tries to "fix" what you're doing
Feb 20 '07 #27

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: John Walton | last post by:
Hello, everyone. I just began school, and they already assigned us science fair. Since I'm in 8th grade, I get to do demonstrations for our projects. I'm probably going to demonstrate Python's...
63
by: John Salerno | last post by:
I know there's a request for a good IDE at least once a week on the ng, but hopefully this question is a little different. I'm looking for suggestions for a good cross-platform text editor (which...
6
by: Bob Palank | last post by:
I'm considering using VJ# in a first programming course in addition to or in place of JBuilder and the J2SE. Given install problems other students have had, VJ# seems like a nice alternative. I...
46
by: Chris Stewart | last post by:
I've always had an interest in Python and would like to dabble in it further. I've worked on a few very small command line programs but nothing of any complexity. I'd like to build a really...
2
by: tvnaidu | last post by:
Is XML is closely work with C programming?. Earlier I think C and C++ are very close in language wise, recently downloaded a project, that contains C code only, but also some XML files, I am...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.