-   Programming (
-   -   cant divide with C (

skoot 08-06-2005 05:36 PM

cant divide with C
i seem to have gotten into trouble with the most rudimentary function around;
#include <stdio.h>
{ printf("%f", 4/3); }

is producing a long 3-line number outpour. (dividing works for me if the answer is a whole number)

problem two:
after changing everything in an "int" dominated program to a "char" dominated one, the program starts intaking only half of the amount of numbers i specify i want to put in .eg:
please enter char 1: please enter char 2:
please enter char 3: please enter char 4:
please enter char 5:

with the above, i specify five and it only scanfs 2 chars(char 2 and 4), skipping 1, 3, and 5 completely. when i change everything back to integers the program functions properly.

???????whats happening?

jlliagre 08-06-2005 06:16 PM

Your compiler is certainly spitting an error message when the -Wall option (assuming you are using gcc) is used, fix first what it is complaining of before trying to run your program !

eddiebaby1023 08-07-2005 04:23 AM

Your "4/3" is producing an int result, and you're telling printf() to treat it as a float. If you tell printf() to do the wrong thing, you shouldn't complain when it does the wrong thing! Try

printf(%f\n", 4.0/3);
I don't understand your second question, can you post a minimal code version of the program that exhibits the problem, please?

jlliagre 08-07-2005 05:59 AM

The second problem cause will probably be exhibited if -Wall gcc option is used too.

exvor 08-07-2005 09:34 AM

You could always recast the int into a float as well



printf("%f",(float) 4 / 3);

skoot 08-07-2005 09:49 AM

dividing in c
what is the -Wall option?
already tried(float(4/3), shall try 4.0/3.
here is what the second problem relates to(real version in C of course;P):
int a;
char *b;
scanf("number of values you want to enter"); /* this value is assigned to a */
b = (char*) calloc etc. etc.;
for(a-1 times do the following)
scanf("%c", (b+a));
for(a-1 times do the following)
{ printf("char %d = %c \n" , a+1, *(b+a)); }

exvor 08-07-2005 09:57 AM

-Wall is a compiler option like such

user@Hell$ gcc -Wall test.c

jlliagre 08-07-2005 10:27 AM

Hmm, you tried "float(4/3)" when "(float)4/3" was suggested ...
Expect disappointments.

Please enclose your code sample between [ code ] tags, and post your real code, not pseudo code, where the possible mistakes are not visible.

Finally, compile your code with gcc -Wall option, and don't post again until no warning is fixed, or only if you do not manage to find how to fix one of these.

skoot 08-07-2005 03:23 PM

okay. dividing has worked. thanks:)

for the second problem -Wall stated the following "errors":
1) line with '{' (below main() line)
2) bottom line with the corresponding '}' (showing that 1 and 2 are not errors)
3)the line: charpointer = ((char*)calloc(a, sizeof(char))); /* where 'a' is amount of times i want scanf to scanf the charpointer value, this line looks fine to me(?) */

(sorry about the pseudo-c in last post, didnt think it would offend)

jlliagre 08-07-2005 03:32 PM

Hard to comment without your real source code posted nor the real gcc messages.

skie_knite007 08-09-2005 12:02 PM

First of all u should convert 4/3 to float using (float)(4/3).
Then try to print it as float.......u r done.........

exvor 08-09-2005 01:32 PM

Your second problem has something to do with the \n charecter. When your done imputing
your charecter it uses the newline generated by enter in the next statement. doesent work with integers because \n is not an integer but this is only true if you are parsing
the imput witch without code then would be hard to determine.

jlliagre 08-10-2005 03:20 PM


First of all u should convert 4/3 to float using (float)(4/3).
Then try to print it as float.......u r done.........
This is just plain wrong ...

There's no point converting after the division between integers is done.

skoot 08-12-2005 04:21 PM

regarding the '\n' suggested by exvor:
1) wouldnt this only happen if im using the %s and not %c.
surely '\n' is not mistaken for a character(?)
2)please enter char one:please enter char two:
this is what is printed- the program doesnt even wait for me to enter anything before asking for the second char, so i havnt had the chance to even press enter at this time, cancelling out the chance of the program receiving a '\n', or am i wrong?

exvor 08-13-2005 07:36 AM

\n is a charecter. getchar waits for you to hit enter to accept the charecter enterd this
enter press is put into the keyboard buffer and used next time getchar is called.

All times are GMT -5. The time now is 05:27 AM.