Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Forcing positive integer

Hi.
I'm having abit of difficulty moving from programming in pascal to c++.

there was a built in function in pascal that would be say a=MOD(a) and that would automatically square and then square root the number to give me the positive back, whether or not the initial value of a was positive or negative.

there must be a function in c++ that does this =S just can't find it.

I'm using the standard stdio.h and math.h headers.

thanks

edit: i'm also having trouble with my while condition =/

printf("Please enter the annual interest rate for the mortgage\n");
scanf("%f", &i);
while (i>=100); {
printf("Invalid amount of interest. Interest can only be between 0-100%.\n");
printf("Please re-enter amount for interest\n");
scanf("%f", &i);}

no matter what value of i for interest i enter into my console program, it enters the while statement =(
[962 byte] By [daveip] at [2007-11-11 10:20:14]
# 1 Re: Forcing positive integer
I think the function for a=MOD(a) is abs and is defined in math.h:
a = abs(a);
For float numbers use fabs.
[...]
i'm also having trouble with my while condition[...]

while (i>=100); {
printf("Invalid amount of interest. Interest can only be between 0-100%.\n");
printf("Please re-enter amount for interest\n");
scanf("%f", &i);}

no matter what value of i for interest i enter into my console program, it enters the while statement =(
You have to remove ; from while line:
while(i >= 100)
{
. . .
}
I hope this helps.
Viorel at 2007-11-11 20:59:00 >
# 2 Re: Forcing positive integer
thanks for your reply!!! works a treat thanks =)
daveip at 2007-11-11 21:00:05 >