Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-07-2009
victory1
Quote:

Hi I really need help I'm having a hard time trying to convert celsius to fahrenheit. As it stands right now I'm in an endless loop. I have to use the FOR statement in this program but its not working.

/* This program converts Fahrenheit to Celsius in increment of 5 */
int main()

Code:

```{ int main() /* This program converts Fahrenheit to Celsius in increment of 5 */ { int Celsius,degreesF; float Fahrenheit; printf("\nThis program will convert Fahrenheit to Celsius\n\n"); printf("Enter the degrees in Fahrenheit: "); scanf("%d", &degreesF);             Celsius =5; printf("Fahrenheit  Celsius \n"); printf("----------  ------- \n"); for ( Celsius=0; Celsius= (degreesF*5); Celsius = degreesF+ 5) {                      Celsius = (5.0 /9.0)*(Fahrenheit - 32); printf("%5d %10d\n", degreesF ); }             Celsius = Celsius + 5; getchar(); getchar(); getchar(); getchar(); return 0; }```
• 10-07-2009
quzah
You are assigning a value in the part of your loop where you should be controlling how it stops. (Hint: The middle portion. = is assignment == is comparison.)

Quzah.
• 10-07-2009
dwks
Also, instead of just putting some large number of getchar()s at the end of your program, you could write something like this:
Code:

`while(getchar() != '\n') {}`
• 10-07-2009
victory1
need explanation
Quote:

Originally Posted by quzah
You are assigning a value in the part of your loop where you should be controlling how it stops. (Hint: The middle portion. = is assignment == is comparison.)

Quzah.

What do you mean by assignment? I'm trying to understand this.
• 10-07-2009
quzah
I was pretty clear there. Here, maybe this will help:
Code:

`for ( Celsius=0; Celsius= (degreesF*5); Celsius = degreesF+ 5)`

Quzah.
• 10-07-2009
victory1
Quote:

Originally Posted by quzah
I was pretty clear there. Here, maybe this will help:
Code:

`for ( Celsius=0; Celsius= (degreesF*5); Celsius = degreesF+ 5)`

Quzah.

Hi again
Ok I got that part which I do appericate your help but for some reason my program is still in an endless loop. If you see something wrong with it could you please explain.

Thank you
• 10-07-2009
A for loop has three basic parts:

1) The initial assignment:

someVariable = 0; //for example

2) The test to see if the loop should end:

someVariable < number;
someVariable == number;

are common tests in a for loop.

3) The increment (or decrement):

someVariable++;
someVariable--;

are common

In your program's case, the end test condition is:

1) Wrong, since it needs a comparison operator ==, not an assignment operator =.
2) The range is far too high. If the user enters 70 degrees Fahrenheit, your loop will continue until the temperature reaches 350 degrees Fahrenheit (5 * 70).

That's way past T shirt weather! :p :p

Figure out what your assignment requires for a reasonable range, and then set it up with a test comparison ==, not an assignment, OK? :)
• 10-07-2009
victory1
Quote:

A for loop has three basic parts:

1) The initial assignment:

someVariable = 0; //for example

2) The test to see if the loop should end:

someVariable < number;
someVariable == number;

are common tests in a for loop.

3) The increment (or decrement):

someVariable++;
someVariable--;

are common

In your program's case, the end test condition is:

1) Wrong, since it needs a comparison operator ==, not an assignment operator =.
2) The range is far too high. If the user enters 70 degrees Fahrenheit, your loop will continue until the temperature reaches 350 degrees Fahrenheit (5 * 70).

That's way past T shirt weather! :p :p

Figure out what your assignment requires for a reasonable range, and then set it up with a test comparison ==, not an assignment, OK? :)

The assisgement I was given just ask the end user to enter in ANY temp. to convert F to C. My teacher told me to go fish.
• 10-07-2009
quzah
Code:

`printf("%5d %10d\n", degreesF );`
You should compile with warnings on.

Quzah.
• 10-07-2009
You will have to figure out what you want for your top temperature. I'm sure your assignment doesn't want the range to go up to 5 times the temperature entered by the user.

• 10-07-2009
victory1
Quote:

You will have to figure out what you want for your top temperature. I'm sure your assignment doesn't want the range to go up to 5 times the temperature entered by the user.

The assignment reads convert Fahrenheit to Celsius temperature in increments of 5 degrees. The initial value fo the Fahrenheit temperature and the total conversions to be made are to be requested as user nput during the program execution.
• 10-07-2009
quzah
Compute the stop value first.
Code:

```stopvalue = computeFtoCforinputvalue(); for( x = 0; x < stopvalue; x += 5 )     ...output...```

Quzah.
• 10-07-2009
RockyMarrone
and enjoy C/C++ coding.......

Code:

```#include <stdio.h> // Function for converting fahrenheit to celsuis float fahrenheit_to_celsius(const float temprature) {   return (float)(temprature - 32) * (5.0 / 9.0); } // Function for converting celsuis to fahrenheit float celsuis_to_fahrenheit(const float temprature) {   return (float)((temprature * (9 / 5)) + 32); } int main() {   int choice            = 0;   float start_temprature = 0;   float end_temprature  = 0;   printf("1... Fahrenheit to Celsuis\n");   printf("2... Celsuis to Fahrenheit\n");   printf("Please enter your choice\n");   scanf("%d", &choice);   printf("Please enter Start Temprature\n");   scanf("%f", &start_temprature);   printf("Please enter End Temprature\n");   scanf("%f", &end_temprature);   while (start_temprature <= end_temprature) {     switch (choice) {       case 1:       printf("fahrenheit == %f || celsuis == %f\n",               start_temprature,               fahrenheit_to_celsius(start_temprature));       break;       case 2:         printf("clesius  == %f || fahrenheit == %f\n",               start_temprature,               celsuis_to_fahrenheit(start_temprature));       break;       default:         printf("[warning] Choice is not valid\n");       break;     }     start_temprature += 5;   }   return 0; }```
• 10-08-2009
victory1
Quote:

Originally Posted by RockyMarrone
and enjoy C/C++ coding.......

Code:

```#include <stdio.h> // Function for converting fahrenheit to celsuis float fahrenheit_to_celsius(const float temprature) {   return (float)(temprature - 32) * (5.0 / 9.0); } // Function for converting celsuis to fahrenheit float celsuis_to_fahrenheit(const float temprature) {   return (float)((temprature * (9 / 5)) + 32); } int main() {   int choice            = 0;   float start_temprature = 0;   float end_temprature  = 0;   printf("1... Fahrenheit to Celsuis\n");   printf("2... Celsuis to Fahrenheit\n");   printf("Please enter your choice\n");   scanf("%d", &choice);   printf("Please enter Start Temprature\n");   scanf("%f", &start_temprature);   printf("Please enter End Temprature\n");   scanf("%f", &end_temprature);   while (start_temprature <= end_temprature) {     switch (choice) {       case 1:       printf("fahrenheit == %f || celsuis == %f\n",               start_temprature,               fahrenheit_to_celsius(start_temprature));       break;       case 2:         printf("clesius  == %f || fahrenheit == %f\n",               start_temprature,               celsuis_to_fahrenheit(start_temprature));       break;       default:         printf("[warning] Choice is not valid\n");       break;     }     start_temprature += 5;   }   return 0; }```

Thank you for all your help but I have to you a FOR statement in this program.
• 10-08-2009
RockyMarrone
hey victory1
Why u stuck with for plz can u tell me that First :)

fOR for loop() here in the code just replace

while (start_temprature <= end_temprature) {
with

for (float temprature = start_temprature; temprature <= end_temprature; temprature+=5) {
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last