have a questions about my C proggie

Omegachi

Diamond Member
Mar 27, 2001
3,922
0
76
i am making a simple program that is suppose to round numbers off. How do you convert a float value to an integer value???


and then i got this error that saids:

cc: "round.c", line 52: error 1535: % operator takes integral operands.

on line 52 i try to modulate my float value by 1----- remin = value % 1;

what does that error mean??

here is my program if you wanna check it out:


*/

/* This Program Rounds A Float Value To the Nearest Integer */

int main()
{
/* Declare Variables */

int round_value;
float value;
int round_to_int(float value);

/* Prompt the user for a float value */

printf("Please Enter A Float Value (0 to quit): ");
scanf("%f", &value);

/* while loop when value not equal to 0 */

while (value != 0)
{
/* call function round_to_int */

round_value = round_to_int(value);


/* print the results */

printf("rounding %f yields %d", value, round_value);

}

}

/* function round_to_int */

int round_to_int(float value)
{
/* declare round_to_int function variables */

int round_value;
int remin;

/* determining the reminder */

remin = value % 1.0;

/* if statements */

if(remin >= 5)
round_value = value + 1;
else
round_value = round_value;

/* return round_value */

return round_value;
}

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Why not just read value as a string, then convert the string to an int with atoi? It might even do the rounding for you.

You could try casting the variable as an int with (int)value.
 

Omegachi

Diamond Member
Mar 27, 2001
3,922
0
76
what do you mean by converting it to int atoi? sorry, i am a beginner. :(

i'll take your advise and try that (int)value thing
 

Omegachi

Diamond Member
Mar 27, 2001
3,922
0
76
okay, it don't work, or maybe i am doing it wrong.....where should i put the (int)?
 

Omegachi

Diamond Member
Mar 27, 2001
3,922
0
76
i guess my main concern is that error.....why am i getting that % operator error?
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
cc: "round.c", line 52: error 1535: % operator takes integral operands.

what that tells you is that -- "% operator takes integral operands"
operands are the values that you use with operators (eg: a % b --> operands are a and b, while % is the operator)
modulus operator, %, takes only integer values. you cannot use floats with them

if you want to decide whether you want to round it up or down, there are workarounds without using the modulus operators (like substracting it with 0.5 and see if the integer part gets substracted or not)

also, correct me if i'm wrong, but doesn't scanf takes %lf instead of %f for float values?

-927-
 

CSoup

Senior member
Jan 9, 2002
565
0
0


<< cc: "round.c", line 52: error 1535: % operator takes integral operands.
also, correct me if i'm wrong, but doesn't scanf takes %lf instead of %f for float values?

-927-
>>



%f is for floats, %lf is for long floats (also called doubles).
 

Omegachi

Diamond Member
Mar 27, 2001
3,922
0
76
thanks stndn....i think i am going to use the subtract .5 idea instead of that damn modulus thingie
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
helo,
i am actually not so sure .. it's been a while since i code in c/c++, but try this ...
to convert from int to float, you can use typecast.. eg:
int a;
float b;
a = 1;
b = (float) a;

that should convert the value of a to a float ...

if you are converting from float to int, the numbers will be rounded.
whether it is rounded up or down depends on the compiler... most will just drop the decimal places, while others may test the value to see if it's greater than 0.5

if you are allowed to use math.h, there are functions called floor() to round down, and there's another one to round up (forgot what it's called, maybe ceil()).

otherwise, you can try to substract with 0.5 and then do whatever depending on the result
eg:
float a, b;
a = 1.5;
b = a - 0.5;
// round both a and b, and test to see if the result of typecast from float to int is the same number
// then either round the original value (a) up or down depending on the result
}

hope it helps :)

-930-
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Uhm ... howabout rint()?

%man rint
RINT(3) Linux Programmer's Manual RINT(3)


NAME
rint - round to closest integer

SYNOPSIS
#include <math.h>

double rint(double x);

DESCRIPTION
The rint() function rounds x to an integer value according
to the prevalent rounding mode. The default rounding mode
is to round to the nearest integer.

RETURN VALUE
The rint() function returns the integer value as a float­
ing-point number.

CONFORMING TO
BSD 4.3

SEE ALSO
abs(3), ceil(3), fabs(3), floor(3), labs(3)



Note that it returns a double, but you can take care of that with a cast.