I need help to limit the loop into the next 4 interations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HazenZero
    New Member
    • Apr 2014
    • 1

    I need help to limit the loop into the next 4 interations

    I have a user input, and I need the loop to continue for the next 4 iterations of the user input. I have no idea how because everytime I try to do it, it becomes either and infinite loop or just crashes.. Please help
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	double H, t;
    	double a, b;
    
    	a = 2.13;
    	b = 0.05;
    	
    	printf("Please enter input fot t:");
    	scanf("%lf", &t);
    	
    	H = ((a * t)*(a * t))-((b * t)*(b * t)*(b * t));
    	
    	if (H > 100)
    	{
    		printf("\nHigh\n");
    	}
    	else if (H > 50)
    	{
    		printf("Average\n");
    	}
    	else 
    	{
    		printf("Low\n");
    	}
    	
    	printf("\nt\t\t   H\n");
    	printf("----------------------\n");
    	
    	for (t=t; t < (t + 5); t++)
    	{
    		H = ((a * t)*(a * t))-((b * t)*(b * t)*(b * t));
    		printf("%.0f\t\t%.2f\n", i, H);
    		
    	}
    
    	return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    t < (t + 5) will always be true by definition.

    Think of any number, add 5 to it. Is the result greater than the original number?

    You need to use 2 variables, t, which you increment and another variable that you set up before the loop starts to contain the end point for the loop.

    Code:
    max = t + 5;
    for( ; t < max; t++)
    {
      ...
    }

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Is your intent to loop through your program a fixed number of times? If so, then I wouldn't use user input t as a loop variable. Better to add a separate variable whose only purpose is to control the loop.

      Code:
      int i;
      for (i=0; i<5; i++)
      {
         ...
      }
      By the way, it is unusual to use a floating point variable (t) to control a loop.

      Comment

      Working...