how to put printf in a inifinite loop

Oct 11, 2017 at 2:18am
ive been trying to put printf in an infinite loop but printf just wont show up.
Also I want to know how I could have an infinite loop and a certain loop serpaate.
here my code (I'm also new to this)

#include<stdio.h>
#include<stdlib.h>



int main(void)


{
char answer;
printf("Would you like to do any testing (enter y for yes and n for no):\n");
scanf("%c", &answer);

if (answer == 'y')
{
printf("How how would you like to do the testing?\n\n");
}
else if (answer == 'n')
{
return 0;
}
else if (answer >= 0)
{
return 0;
}
{
int number;
printf("Enter 1 for indefinitetestin or enter 2 for a set of number of test\n");
scanf("%d", &number);

if (number == 1)
{
printf("Ok, the program will run indefinitely. Enter -1111 to exit.\n");
}
else if (number == 2)
{
printf("Ok, the program will run for a set of number test.\n");
}

}




int windspeed;
for (int counter = 1; counter >0; counter++){
printf("Please Enter a value for windspeed:");
scanf("%d", &windspeed);
}


while (windspeed <= 118) {
printf("There will be no hurricane\n");
scanf("%d", &windspeed);

}

while (windspeed >= 119 && windspeed <= 153) {
printf("Category 1\n");
printf("Please enter another value\n");
scanf("%d", &windspeed);

}
while (windspeed >= 154 && windspeed <= 177) {
printf("Category 2\n");
printf("Please enter another value\n");
scanf("%d", &windspeed);
}
while (windspeed >= 178 && windspeed <= 208) {
printf("Category 3\n");
printf("Please enter another value\n");
scanf("%d", &windspeed);
}
while (windspeed >= 209 && windspeed <= 251) {
printf("Category 4\n");
printf("Please enter another value\n");
scanf("%d", &windspeed);
}
while (windspeed >= 252) {
printf("Category 5\n");
printf("Please enter another value\n");
scanf("%d", &windspeed);
}


printf("\n");
system("pause");
int counter;
}
Oct 11, 2017 at 10:24am
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

(Going back and editing your OP to use tags would help.)

2) Can you clarify what it is that isn't working? Which printf statement(s) aren't showing? Which are showing?

3) I can see that you don't use the value of number to determine anything other than what to print out. You certainly don't use it to control whether or not you do an "infinite" loop.

4)

1
2
3
4
5
int windspeed;
  for (int counter = 1; counter >0; counter++){
  printf("Please Enter a value for windspeed:");
  scanf("%d", &windspeed);
}


This loop isn't actually infinite. At some point, counter will reach the maximum positive number an int can hold, and then become negative.

5) You tell the user that entering -1111 will exit the "infinite" loop, but you never actually do that. Nowhere do you test for that value.

6)
Also I want to know how I could have an infinite loop and a certain loop serpaate.

I don't understand what you mean by that. Can you explain more clearly what it is you're trying to achieve?

Last edited on Oct 11, 2017 at 10:24am
Oct 11, 2017 at 3:52pm
1
2
3
4
5
while(true) 
{
  printf("derp ");
  fflush(stdout); 
}


Last edited on Oct 11, 2017 at 3:54pm
Topic archived. No new replies allowed.