This question must have a simple answer.
Author |
Message |
Zach Stechl #1 / 9
|
 This question must have a simple answer.
But I just can't figure it out now. Ok, here it goes. I was making a C program that changed fractions to decimals. But, everytime I try to do It, I come up with 0. If I change it from / to +, it adds fine. Well, here is a simplied copy of the code. PLEASE HELP! (I most likely just need to change the data type..........) #include <stdio.h> long input; long answer; long inp; main() { printf("Enter a number: "); scanf("%d", &input); printf("Enter another number: "); scanf("%d", &inp); answer = input / inp; //Right here. If I change it to add or subtract, it will work fine. printf("The answer is %d", answer); return 0; }
|
Sun, 26 Nov 2000 03:00:00 GMT |
|
 |
Ben Pfaf #2 / 9
|
 This question must have a simple answer.
But I just can't figure it out now. Ok, here it goes. I was making a C program that changed fractions to decimals. But, everytime I try to do It, I come up with 0. If I change it from / to +, it adds fine. Well, here is a simplied copy of the code. PLEASE HELP! (I most likely just need to change the data type..........) Yes. Change your data type to `double' for double-precision floating point arithmetic, and your scanf()'s to %f. BTW, scanf() isn't usually a good function to use.
|
Sun, 26 Nov 2000 03:00:00 GMT |
|
 |
Todd Rowa #3 / 9
|
 This question must have a simple answer.
Change answer, or all of them if you wish, to type float. Quote:
>But I just can't figure it out now. Ok, here it goes. I was making a C >program that changed fractions to decimals. But, everytime I try to do >It, I come up with 0. If I change it from / to +, it adds fine. Well, >here is a simplied copy of the code. PLEASE HELP! (I most likely just >need to change the data type..........)
|
Sun, 26 Nov 2000 03:00:00 GMT |
|
 |
Dann Corbi #4 / 9
|
 This question must have a simple answer.
You inadvertently left the 'l' out of your format specifier. A new person might get confused. For the new person, this is from the C-FAQ: 12.13: Why doesn't this code: double d; scanf("%f", &d); work? A: Unlike printf(), scanf() uses %lf for values of type double, and %f for float. See also question 12.9. -- Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9 Try "C Programming: A Modern Approach" ISBN 0-393-96945-2 Want Software? Algorithms? Pubs? http://www.infoseek.com Quote:
> But I just can't figure it out now. Ok, here it goes. I was making a C > program that changed fractions to decimals. But, everytime I try to do > It, I come up with 0. If I change it from / to +, it adds fine. Well, > here is a simplied copy of the code. PLEASE HELP! (I most likely just > need to change the data type..........) >Yes. Change your data type to `double' for double-precision floating >point arithmetic, and your scanf()'s to %f. BTW, scanf() isn't >usually a good function to use.
|
Sun, 26 Nov 2000 03:00:00 GMT |
|
 |
G. Herrmannsfel #5 / 9
|
 This question must have a simple answer.
Quote:
>But I just can't figure it out now. Ok, here it goes. I was making a C >program that changed fractions to decimals. But, everytime I try to do >It, I come up with 0. If I change it from / to +, it adds fine. Well, >here is a simplied copy of the code. PLEASE HELP! (I most likely just >need to change the data type..........) >#include <stdio.h> >long input; >long answer; >long inp; >main() > { > printf("Enter a number: "); > scanf("%d", &input); > printf("Enter another number: "); > scanf("%d", &inp); > answer = input / inp; //Right here. If I change it to add or >subtract, it will work fine. > printf("The answer is %d", answer); > return 0; > }
Try it with the first number larger than the second number. Maybe then it won't be zero. -- glen
|
Sun, 26 Nov 2000 03:00:00 GMT |
|
 |
Martin Ambuh #6 / 9
|
 This question must have a simple answer.
:But I just can't figure it out now. Ok, here it goes. I was making a C :program that changed fractions to decimals. But, everytime I try to do :It, I come up with 0. If I change it from / to +, it adds fine. Well, :here is a simplied copy of the code. PLEASE HELP! (I most likely just :need to change the data type..........) ================ Since you haven't gotten the C-FAQ from ftp://rtfm.mit.edu/pub/usenet-by-group, you have left yourself open to flammage. The use of the evil scanf function, the (not-yet-allowed) '//' syntax error for comments, and the (soon-to-be-deprecated) implicit declaration of main mark you as a newbie, ripe for the crackle-crackle. But when you do get the FAQ, you will find this: 3.14: Why doesn't the code int a = 1000, b = 1000; long int c = a * b; work? A: Under C's integral promotion rules, the multiplication is carried out using int arithmetic, and the result may overflow or be truncated before being promoted and assigned to the long int left-hand side. Use an explicit cast to force long arithmetic: long int c = (long int)a * b; Note that (long int)(a * b) would *not* have the desired effect. A similar problem can arise when two integers are divided, with the result assigned to a floating-point variable. References: K&R1 Sec. 2.7 p. 41; K&R2 Sec. 2.7 p. 44; ANSI Sec. 3.2.1.5; ISO Sec. 6.2.1.5; H&S Sec. 6.3.4 p. 176; CT&P Sec. 3.9 pp. 49-50. ================ : : :#include <stdio.h> :long input; :long answer; :long inp; :main() : { : printf("Enter a number: "); : scanf("%d", &input); : printf("Enter another number: "); : scanf("%d", &inp); : answer = input / inp; //Right here. If I change it to add or :subtract, it will work fine. : printf("The answer is %d", answer); : return 0; : } :
|
Sun, 26 Nov 2000 03:00:00 GMT |
|
 |
John Kugelma #7 / 9
|
 This question must have a simple answer.
Quote:
> But I just can't figure it out now. Ok, here it goes. I was making a C > program that changed fractions to decimals. But, everytime I try to do > It, I come up with 0. If I change it from / to +, it adds fine. Well, > here is a simplied copy of the code. PLEASE HELP! (I most likely just > need to change the data type..........) > #include <stdio.h> > long input; > long answer; > long inp; > main() > { > printf("Enter a number: "); > scanf("%d", &input); > printf("Enter another number: "); > scanf("%d", &inp); > answer = input / inp; //Right here. If I change it to add or > subtract, it will work fine. > printf("The answer is %d", answer); > return 0; > }
It's a simple "quirk" of the division operator. When it takes two integral operands, it produces an integral result, not a floating point one. This may seem odd, but it's really quite useful for many things. Add a cast to (double) before input or inp and the division will instead be performed with floating point arithmetic. Be aware that answer is a long integer, though, so it will convert that result back into an integer, which is basically what happens with integer division. You should make it a float or double and printf it with the %f conversion specifier. --
I believe we can change anything. I believe in my dream. - Joe Satriani
|
Mon, 27 Nov 2000 03:00:00 GMT |
|
 |
Alexander Dymeret #8 / 9
|
 This question must have a simple answer.
Quote: > But I just can't figure it out now. Ok, here it goes. I was making a C > program that changed fractions to decimals. But, everytime I try to do > It, I come up with 0. If I change it from / to +, it adds fine. Well, > here is a simplied copy of the code. PLEASE HELP! (I most likely just > need to change the data type..........) > #include <stdio.h>
You'd better place these declarations inside main() Quote: > long input; > long answer; > long inp; > main() > { > printf("Enter a number: "); > scanf("%d", &input); > printf("Enter another number: "); > scanf("%d", &inp); > answer = input / inp; //Right here. If I change it to add or
Both input and inp are integer, so result will be integer too. If input<inp, result is 0. Try : double answer; ............... <skip> answer=(double)input/inp; printf("The answer is %lf", answer);
|
Tue, 28 Nov 2000 03:00:00 GMT |
|
 |
John Kugelma #9 / 9
|
 This question must have a simple answer.
Quote:
> > But I just can't figure it out now. Ok, here it goes. I was making a C > > program that changed fractions to decimals. But, everytime I try to do > > It, I come up with 0. > > #include <stdio.h> > > long input; > > long answer; > > long inp; > You'd better place these declarations inside main()
He'd "better"? Are you threatening him? :-) Quote: > > main()
Make that "int main (void)" Quote: > > { > > printf("Enter a number: ");
Add fflush (stdout); Quote: > > scanf("%d", &input);
scanf() is Bad. Quote: > > printf("Enter another number: ");
Similarly. Quote: > > scanf("%d", &inp); > > answer = input / inp; //Right here. If I change it to add or > Both input and inp are integer, so result will be integer too. If > input<inp, result is 0. > Try : > double answer; > ............... <skip> > answer=(double)input/inp; > printf("The answer is %lf", answer);
The conversion specifier for a double is %f, not %lf. --
I believe we can change anything. I believe in my dream. - Joe Satriani
|
Wed, 29 Nov 2000 03:00:00 GMT |
|
|
|