15,922,533 members
2.00/5 (1 vote)
See more:
```#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
int v, t, wc;

printf("Input temperature (<= 40° F) :...\n");
scanf_s("%d", &t);

printf("Input wind speed (>= 6 mph) :...\n");
scanf_s("%d", &v);

while (1) /*Calculating the wind chill*/
{
if (t <= 40 && v >= 6)
{
wc = (int)(0.0817*(3.71*sqrt(v) + 5.81 - 0.25*v)*(t - 91.4) + 91.4);
printf("The wind chill is %d degrees F.\n", wc);
}
else
{
printf("Number is out of range. Try again");
}
}
return 0;
}```

What I have tried:

Hello, I am making a C program to calculate the wind chill using while loop.

However, when I run the code I wrote, it appears"The wind chill is %d degrees F.\n", wc" infinity times, whereas I just need 1 to display.

Here is my question:

1. Where should I move my "printf("The wind chill is %d degrees F.\n", wc);" in order to make it print only one time?
2. How do I round the number to the units?
(When I input t=22, v=15, the result will be -1 but i want it to be -2 because it is -1.7507872.)

Thanks!
Posted
Updated 14-May-16 19:15pm
v6
PIEBALDconsult 14-May-16 22:55pm
You have a while(1) with no break, what do you expect to happen?
Member 12523929 14-May-16 23:11pm
I just want this formula limited to temperatures of 40 F and below
and wind speeds of 6 mph or greater.

Because I put if (t <= 40 && v >= 6) already, I thought putting 1 in the while loop is ok. Isnt it?
Sergey Alexandrovich Kryukov 14-May-16 23:37pm
Can you read the comments? Nobody said anything about (pretty silly) while(1). It works as it works: the look condition remains the same for any iteration. Pay attention for the basic thing: there is nothing which breaks the loop at all. OK or not OK is absurd question.
—SA
[no name] 14-May-16 23:14pm
The easiest way to solve your problem is to copy the if statement's condition to while loop while (t <= 40 && v >= 6) and adding break at the end of else statement

## Solution 1

Your while loop statement is misplaced. It should be placed before the first printf().

As PIEBALDConsult pointed out, you need to have a break condition, because otherwise your loop will run forever.

C++
```int main(void)
{
int v, t, wc;

while (1) /*Calculating the wind chill*/
{
// Not a very good example, but you get the idea
printf("Enter temp = 100 to exit.\n\n");

printf("Input temperature (<= 40° F) :...\n");
scanf_s("%d", &t);
if (t == 100)
break;

printf("Input wind speed (>= 6 mph) :...\n");
scanf_s("%d", &v);

if (t <= 40 && v >= 6)
{
wc = (int)(0.0817*(3.71*sqrt(v) + 5.81 - 0.25*v)*(t - 91.4) + 91.4);
printf("The wind chill is %d degrees F.\n", wc);
}
else
{
printf("Number is out of range. Try again");
}
}
return 0;
}```

As for rounding up, use the function `ceil()` in `math.h`
For rounding down use `floor()`.
Only casting to an int will truncate the value, hence 1.1 becomes 1.

For proper rounding you need to check if the value is smaller or larger than .5 and chose between ceil() and floor().

v3
Sergey Alexandrovich Kryukov 14-May-16 23:42pm
5ed, but probably this inquirer needs more basic explanation (I'm not saying it would actually be useless; I can also see some kind of missing of the meaning of comments.) Total lack of break or break condition is more fundamental mistake than the scope of the loop and location of particular I/O statements, especially when the inquirer cannot realize that.

My other note is "Infinity result". It's hard to understand what the inquirer really meant by that, but it's apparent that nothing is a "result", and nothing is floating-point +inf or −inf. :-)

—SA
George Jonsson 14-May-16 23:58pm
I think the OP meant that the result string is printed continuously on the screen without stopping.
And with the while loop placed just around the calculation statements, it does of course.
Well, let's hope the OP got a nudge in the right direction.
Sergey Alexandrovich Kryukov 15-May-16 0:11am
Probably... Let's hope so...
—SA
George Jonsson 15-May-16 0:29am
Thanks for the upvote.

## Solution 2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

A simple run of your program on debugger would have show you that the repeating was because of the `while` loop.