![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
Having trouble with an exercise in the C book.
Im having trouble with an exercise in the C book written by the inventors of C. Normally i don't like people who post code and ask what is wrong with it but i've been working on this since this morning with no success. I don't have much choice because the C book doesn't have the answers in the back of the book. Im not doign this for a class im interesting in learning C and that is why i want to know what im doing wrong. Hoping someone can look at it because the more i look at it the more it just blends together and i can't pick out what is wrong. In a previous exercise i wrote a program that converts fahrneheit to celsius and displays the conversions from 0 to 300 in steps of 20. Now it is asking for me to have it display the output going from 300 to 0. this is what i have:
main() { int fahr; printf("Fahr\t Celsius\n"); for (fahr = 300; fahr >= 0; fahr = fahr - 20); printf("%3.0f \t%6.1f\n", fahr, (5.0/9.0) * (fahr-32.0)); } here is the output i get: Fahr Celsius 0 0.0 i wouldn't have posted if this thing hasn't frustrated me so much because i know it is just something simple im overlooking. thanks for any replies in advance. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
main()
{
int fahr;
printf("Fahr\t Celsius\n");
for (fahr = 300; fahr >= 0; fahr = fahr - 20);
printf("%3.0f \t%6.1f\n", fahr, (5.0/9.0) * (fahr-32.0));
}For starters, get rid of semicolumn, since you want to have printf inside the loop. I would always put {} around loops and other control structures, although in this case it does not matter (since it's just one statement). Last edited by EverLearning; Jul 5th, 2005 at 5:11 PM. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
how about something like this:
#include <stdio.h>
int main(int argc, char* argv[])
{
int fahr;
printf("Fahr\t Celsius\n");
for (fahr = 300; fahr >= 0; fahr = fahr - 20)
printf("%i \t %6.1f\n", fahr, (5.0/9.0) * (fahr-32.0));
return 0;
}(double check what is typed in the book! I would not try to cast an int to FLOAT implicitly by passing it to the printf function... if necessary, do an explicit cast with (FLOAT) ) : printf("%3.0f \t %6.1f\n", (float)fahr, (5.0/9.0) * (fahr-32.0));
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Jul 5th, 2005 at 5:27 PM. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
being picky
it would be a float.EDIT: if in printf() you use %f that is. Oh, whatever.... |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
right... I beg your pardon - %lf must be double then... ( unless you are using a nonconforming M$
, which I have for waaay to long... (4 months, hehe ))
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Jul 5th, 2005 at 5:38 PM. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Actually I think you can do it with %f as well, my bet here.
This I found helpful before (...'coz I often forget this stuff!), took several minutes to find it on google: table So, the only difference between float and double is the size then. |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
thank you that was my problem that i needed to cast fahr. that initially made me feel like something was arwy with the int and the printf being %f. I've spent most of my time learning about C++ so i still need to get used to printf function where you have to specific everything. sorry for not posting sooner but the post weren't getting sent to me via email.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|