Really stupid question about programming in C. Please HELP!

MWink

Diamond Member
Oct 9, 1999
3,642
1
76
I have a C program due today and I can't for the life of me get it working. I have tracked down what I think is the final bug but I have no idea HOW to fix it. I have a number that I need to convert into a percent (Ex. Go from 23 to .23) but when I divide by 100 it turns into 0 every time. I think what I need to do is make it "float". How do I go about doing that? Or am I on the wrong track? Someone please help me!

Here is what the program is supposed to do, if it helps.

I just can't get it to calculate the interest.
 

MWink

Diamond Member
Oct 9, 1999
3,642
1
76
How do I do that? Right now it is an "INT".


Edit: Ok, I think I changed it but it still does not work and when I compile it, I get:

--------------------Configuration: proj2 - Win32 Debug--------------------
Compiling...
proj2.c
F:\My Documents 2\School\CMSC104\Proj2\New Folder (2)\proj2.c(44) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data

proj2.obj - 0 error(s), 1 warning(s)
 

rippy

Senior member
Jun 12, 2001
511
0
0
instead of saying:

int theNumber;

do this:

double theNumber;

problem solved :)
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
The warning is OK - it is just letting you know that you started with an INT and are moving it into a float.
 

MWink

Diamond Member
Oct 9, 1999
3,642
1
76
Now it's even worse! :Q I HATE CS!

Anymore ideas? I have to go to class now. I'll check back later.
 

Fiz1

Junior Member
May 14, 2001
20
0
0


<< How do I do that? Right now it is an "INT".


Edit: Ok, I think I changed it but it still does not work and when I compile it, I get:

--------------------Configuration: proj2 - Win32 Debug--------------------
Compiling...
proj2.c
F:\My Documents 2\School\CMSC104\Proj2\New Folder (2)\proj2.c(44) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data

proj2.obj - 0 error(s), 1 warning(s)
>>



Note: A double is 8 bytes. It has the range of 2.2E-308 to 1.8E3082. Where a int (or known as a Short integer (short)) has only 2 bytes the values from -32768 to 32767.

Also note, ints can not contain remainds. So let's say int x = 1.24. The value x will contain 1. Not 1.24 If you
use double x = 1.24 it will work. To test it yourself, use the printf("%d", x) statement and see which values it prints out.

Also, that warning means, you are storing a double as an int. remember doubles can have remainders where ints can't. So if that number has a remainder, once it becomes an int. that remainder is gone.

Hope that helps (btw, I don't read this forum much so if you have any addition question. THere's a good chance I won't read it.)

Fiz1
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Please define worse!

example:

int variable = 23;
float floated_variable = (float)variable;
float percent = floated_variable / 100.0;
printf ("%f.2",percent);


Post your code for pointers on where your problem may be.
 

MWink

Diamond Member
Oct 9, 1999
3,642
1
76
Here is the code:

#include <stdio.h>
int main ( )
{
int bal, mpay, year, month, ipaid, intrst, tint;
float irate;
bal=mpay=year=month=ipaid=intrst=tint=0;
irate=0.0;
while (bal <= 0) {
printf ("Enter the balance: ") ;
scanf ("%d", &bal) ;
if (bal <= 0) {
printf ("Sorry, the balance must be greater than zero!\n") ;
}
}
while (irate <= 0 || irate >= 25) {
printf ("Enter the interest rate as a percentage: ") ;
scanf ("%d", &irate) ;
if (irate <=0 || irate >= 25) {
printf ("Sorry, the interest rate must be greater than zero and less than twenty five!\n") ;
}
}
while (mpay <= 0) {
printf ("Enter the monthly payment: ") ;
scanf ("%d", &mpay) ;
if (mpay <= 0) {
printf ("Sorry, the monthly payment must be greater than zero!\n") ;
}
}
irate=irate/100;
irate=irate/12;
while (bal > 0) {
intrst=bal*irate;
tint=tint+intrst;
bal=bal+intrst;
bal=bal-mpay;
month=month+1;
}
while (month >= 12) {
month=month-12;
year=year+1;
}
printf ("It will take %d years and %d months to pay off the balance.\n", year, month) ;
printf ("You will pay %d in interest.", tint) ;
}
 

Noirish

Diamond Member
May 2, 2000
3,959
0
0
i can't remember arithmetics in c anymore.

some language define / as getting quotient so 23/100 = 0 and 123/100 = 1.
you might want to do 23.0 / 100.0, this way, both are float.

if it still doesn't work, do 23 * .01
 

GundamW

Golden Member
Feb 3, 2000
1,440
0
0
Lets see.
scanf("%d", ....) is for integer input; there is one for float.
some of your variables should be float, like bal, mpay,...
When you read in irat as %d, I think you will get an integer.
And when you divide an integer with another integer, you will get an integer. (Correct?)

More hint?




 

GundamW

Golden Member
Feb 3, 2000
1,440
0
0
If %d is for decimal integer, %??? would be for float.
Now that is a tough one. I think I should go look at the reference.;)
 

MWink

Diamond Member
Oct 9, 1999
3,642
1
76
WOW! I got it on the first guess. It appears to be %f. I think it is sort of working now. Thanks. Now to track down all the little bugs.