cant divide with C
i seem to have gotten into trouble with the most rudimentary function around;
#include <stdio.h> main() { 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? |
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 !
|
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
Code:
printf(%f\n", 4.0/3); |
The second problem cause will probably be exhibited if -Wall gcc option is used too.
|
You could always recast the int into a float as well
try Code:
|
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): main() { 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)); } } |
-Wall is a compiler option like such
user@Hell$ gcc -Wall test.c |
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. |
-Wall
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) |
Hard to comment without your real source code posted nor the real gcc messages.
|
First of all u should convert 4/3 to float using (float)(4/3).
Then try to print it as float.......u r done......... |
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. |
Quote:
There's no point converting after the division between integers is done. |
\n
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? |
\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. |
are you saying thet the '\n' is from when i pressed enter after writing the actual name of the program (such as ./program) ??
|
I don't think so, I guess it's the one you entered to trigger the scanf.
|
No
ok this has to do with the keyboard being buffered. when getchar is called it waits for an enter to confirm the selection but that enter generates another character in the buffer "\n" ok now if you have more then one getchar or you are implementing this in a loop then that \n will be used as the next input method instead of the correct one. You have 2 solutions to this either create a input method that doesn't suffer from this deficiency or flush the buffers after every input. |
'\n'
okay, i get what you mean, but i dont see why getchar would do this. this getchar function gets a single character. this will naturally have to be followed by <enter> to confirm what was typed. surely getchar takes this into account?
secondly: i didnt use getchar in the program, i used scanf("%c"); any ideas? |
yes but it asks me to enter the second one before ive even entered the first. there can be no enter in the buffer other than that which i used to start the program ('./program'). if it was buffereing an '\n' than it would be the second, not the first getchar that would be skipped, would it not?
(the above is out of curiosity as, im not using getchar, im using scanf("%c"); but i guess this wouldnt change things. |
Post your full source please and then we can see whats wrong.
|
If you're using scanf("%c",&somechar);, it will read ONE character in from the buffer. Perhaps you entered a number to tell it the number of numbers, as your pseudocode suggested? For example (input in bold):
Code:
Number of numbers: 5 (NEWLINE) |
Quote:
Quote:
You can have yours reading characters without needing \n by turning the interface in "raw" mode, like "vi" do for example. Wrap your program with a shell script like this: Code:
#!/bin/sh |
code:
sorry for the long delay. as i havnt ever got feedback on code, as well as an answer as to what the fault is, i wouldnt mind your opinions on wether this code would be considered neat/clean/efficient etc.#include <stdio.h>
main() { int a, b, c; char *d; printf("enter amount of values: "); scanf("%d", &a); d = (char*) calloc(a, sizeof(char)); if (d == NULL) printf("cannot allocate memory.\n"); else { for (b=0;b<=a-1;b++) { printf("please enter char %d: ", b+1); scanf ("%c", d+b); } for (b=0;b<=a-1;b++) { if ( *(d+b) == ' ') *(d+b) = '_'; } printf( "you entered: "); for (c=0;c<=a-1;c++) printf("%c ", *(d+c)); printf("\n"); } } |
All times are GMT -5. The time now is 08:22 PM. |