LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cant divide with C (https://www.linuxquestions.org/questions/programming-9/cant-divide-with-c-350676/)

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>
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?

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
Code:

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


try

Code:



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):
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)); }
}

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

-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)

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

Quote:

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

\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?

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.

skoot 08-22-2005 03:38 PM

are you saying thet the '\n' is from when i pressed enter after writing the actual name of the program (such as ./program) ??

jlliagre 08-22-2005 04:02 PM

I don't think so, I guess it's the one you entered to trigger the scanf.

exvor 08-22-2005 04:09 PM

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.

skoot 08-22-2005 04:16 PM

'\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?

skoot 08-22-2005 04:38 PM

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.

exvor 08-22-2005 04:55 PM

Post your full source please and then we can see whats wrong.

Matir 08-22-2005 05:02 PM

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)
Reads 5 in, and leaves the newline in the buffer.

jlliagre 08-22-2005 09:47 PM

Quote:

if it was buffereing an '\n' than it would be the second, not the first getchar that would be skipped, would it not?
The first \n, the one following the command is gotten by the shell, not your program.
Quote:

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?
This is wrong, enter is not mandatory to confirm something was typed, at least from a program point of view.
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
stty raw
./program
stty -raw

This also can be done in plain C, but the former is simpler, at least to test the "uncooked" mode.

skoot 08-26-2005 06:50 PM

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.