LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-27-2009, 10:39 PM   #1
guitarstorm
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Rep: Reputation: 0
Using a for loop to print an interest compounding table


Hey, i'm new here and looking for some help with a program. I have to use a for loop to print an interest compounding table. So for example, when i type in 0.05 for the rate and 3 for the number of years, i get huge numbers like this... Shouldn't i be getting small #s for the balance, like 1.1, 1.2, etc?

Years Balance
-------------
1 10284435
2 2147483647
3 2147483647

Here's my code...

#include <stdio.h>

int main (void)
{
int i, rate, years, balance;

printf("Type in integers rate and years, both <10.\n");
scanf("%f%d", &rate, &years);

printf("Years Balance\n");
printf("-------------\n");
balance=1;
for(i=1; i<=years; i++){
balance+=balance*(1+rate/100.0);
printf("%d %d\n", i, balance);
}
return 0;
}
 
Old 02-28-2009, 11:50 AM   #2
bgoodr
Member
 
Registered: Dec 2006
Location: Oregon
Distribution: RHEL[45] {x86,x86_64}, Debian "testing" {x86,x86_64}
Posts: 219

Rep: Reputation: 36
int's versus floats versus printf specs

Quote:
Originally Posted by guitarstorm View Post
Hey, i'm new here and looking for some help with a program. I have to use a for loop to print an interest compounding table. So for example, when i type in 0.05 for the rate and 3 for the number of years, i get huge numbers like this... Shouldn't i be getting small #s for the balance, like 1.1, 1.2, etc?

Years Balance
-------------
1 10284435
2 2147483647
3 2147483647

Here's my code...

#include <stdio.h>

int main (void)
{
int i, rate, years, balance;

printf("Type in integers rate and years, both <10.\n");
scanf("%f%d", &rate, &years);

printf("Years Balance\n");
printf("-------------\n");
balance=1;
for(i=1; i<=years; i++){
balance+=balance*(1+rate/100.0);
printf("%d %d\n", i, balance);
}
return 0;
}
scanf is anything but type-safe. Hence why using the Standard Template Library in C++ is the way to go. I can only guess that the above is some homework assignment in a C course (<insert rant about why C++ isn't being taught enough here>).

Take a look at scanf. Notice that the format string might not match the actual type you fed it. scanf has no way of knowing that the type you fed it doesn't actually match a float (probably not exactly true in some GLIBC's but I digress, but perhaps you don't have -Wall -pedantic turned on??? but you should).

Do a "man printf" on your Linux box to get an idea of the printf specs such as "%d" and try to understand how %d matches up with which numeric types, int or floats. <hint> <hint>

bgoodr
 
Old 02-28-2009, 01:33 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I think what bgoodr is trying to point out is that your use of scanf() is incorrect. You are telling scanf() to convert to a floating point format, but then store the converted value in an integer variable. You need to either change your scanf() format string to match the variable type (use "%d %d"), or probably better, use float or double type for your 'rate' and 'balance' variables.
--- rod..
 
Old 02-28-2009, 06:44 PM   #4
guitarstorm
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Yep it is for a programming assignment in C...

Anyway, when i change to %d for everything and type in the rate and years, i get no output... And when i change the scanf function to %f for rate and %f for the balance output, it comes out as 0.000 for the first year, and then NaN for the other two years.
 
Old 03-01-2009, 12:46 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Use floating point data types as appropriate for the values they represent. Pay attention to promotion and rounding rules for conversions between data types. Pay attention to format strings for both printing and scanning functions.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
howto print output of mount command as a table? ygloo Programming 8 10-18-2006 02:32 PM
can't print with lpr because of "neighbor table overflow" phillipedison1891 Linux - General 0 07-06-2005 02:38 PM
just installed CUPS printer is OK.. but can't print Error: loop in printcap on lpr soylentgreen Linux - Newbie 4 09-23-2004 11:41 AM
how to print the partition table Vlackmar Linux - General 3 11-06-2001 05:26 AM


All times are GMT -5. The time now is 04:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration