Help needed for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cool17
    New Member
    • Oct 2006
    • 24

    Help needed for loop

    Hi guys can help me to correct my mistake? For my program, i asked the user for the number of terms they want to enter, it will then loop to the required times

    Code:
    for(j=0;j<10;j++)
    {
     printf("\nEnter the number of terms:\n");
     scanf("%i",&j);
     printf("\nPlease enter expression info as follow:\n");
       {	
    	double value;
    	double power;
    	double times;
    	int pos;
    	int isX;
    	int KnowX;		
    	int i;
    	
    			
    	printf("-------- expression info:--------\n");
    	printf("What is the single value? =");
    	scanf("%f",&value);
    	printf("How many numbers of xtimes? =");
    	scanf("%f",&power);
                    printf("What is the power value? =");
    	scanf("%f",&times);
    
    	printf("What is the sign ? =");
    	scanf("%i\n",&pos);
                }
    	return 0;
    }
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by cool17
    Hi guys can help me to correct my mistake? For my program, i asked the user for the number of terms they want to enter, it will then loop to the required times

    Code:
    for(j=0;j<10;j++)
    {
     printf("\nEnter the number of terms:\n");
     scanf("%i",&j);
     printf("\nPlease enter expression info as follow: ");
       {	
    	double value;
    	double power;
    	double times;
    	int pos;
    	int isX;
    	int KnowX;		
    	int i;
    	
    			
    	printf("-------- expression info:--------\n");
    	printf("What is the single value? =");
    	scanf("%f",&value);
    	printf("How many numbers of xtimes? =");
    	scanf("%f",&power);
                    printf("What is the power value? =");
    	scanf("%f",&times);
    
    	printf("What is the sign ? =");
    	scanf("%i\n",&pos);
                }
    	return 0;
    }

    You change the loop variable 'j' inside the loop, which you should generally avoid with for loops as it may lead to confusion ... :-)

    I propose to ask the user for the number of terms, then use the just read in value in the loop condition, like so

    Code:
    int num = 0; 
    printf("\nEnter the number of terms:\n");
    scanf("%i",&num);
    for(j=0;j<num;j++)
    {
       ....
    }
    Is that approximately what you need?

    Comment

    • cool17
      New Member
      • Oct 2006
      • 24

      #3
      Yes yes that is what i need. thank you very much.

      Originally posted by arne
      You change the loop variable 'j' inside the loop, which you should generally avoid with for loops as it may lead to confusion ... :-)

      I propose to ask the user for the number of terms, then use the just read in value in the loop condition, like so

      Code:
      int num = 0; 
      printf("\nEnter the number of terms:\n");
      scanf("%i",&num);
      for(j=0;j<num;j++)
      {
         ....
      }
      Is that approximately what you need?

      Comment

      Working...