Thread: for loop not working properly

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    116

    for loop not working properly

    Hi everyone. Am unable to figure out why this for loop doesnt work. The loop variable jumps straight to 10 without running through 1,2,3,4 etc, and the inner loop just automatically jumps to flag=1. Dispite this, both loops continue indefinitely (when they should stop at these values). Can anyone figure out why? The program runs so dont have any errors

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    	FILE *fp;	
    	int loop=1;
    	int signalOutput;
    	int flag=0;
    	int column1;
    	float column2;
    	float temperatureArray[10];
    	int quit;
    
    
    	srand(time(NULL));
    
    
    	
    
    
    	printf("\n\n The sensor readings and their corresponding temperatures:\n");
    	
    		fp = fopen("c:\\LOOKUP.txt", "r");
    		
    		for( ; loop=10; ++loop)
    		{
    			signalOutput = (int)(rand()%15);
    			
    			for(flag=0; flag=1; 0)
    			{
    				fscanf(fp, "%i %f",&column1, &column2);
    				
    				if ( signalOutput <= column1 ) 
    				{
    					temperatureArray[loop] = column2;
    					flag = 1;
    					printf("\n the %i reading was %i = %f", loop, signalOutput, temperatureArray[loop]);
    				}			
    			}
    		}
    			
    		
    	fclose(fp);
    
    
    	printf("Press enter to quit");
    	scanf("%i", &quit);
    
    
    	return 0;
    }
    the text file contains:

    0 10
    1 12
    2 14
    3 16
    4 18
    5 20
    6 21
    7 22
    8 23
    9 24
    10 24.5
    11 25
    12 25.25
    13 25.5
    14 25.75
    15 26


    hopefully we can figure out whats wrong with this

    thanks and all the best

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your loop condition is actually an assignement -> always true.
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    how do I rectify that ZuK?

    Thanks for the reply

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    For, While and Do While Loops in C - Cprogramming.com (loop<=10)
    Also you line 10 should be part of line 29 i.e. your loop initialization. Much easier to understand that way.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    mario how can I bring line 10 into line 29? Would this work?

    Code:
    for(; signalOutput==column1; )
    also, I have already read and learned some material on loops and unfortunately was not able to understand everything properly so I think being corrected by those who know better is the only way to improve my for loop skills. I know its a lot to ask though so those who feel they woudlnt have time to help to point out spefically what I need to do differently I totally understand that

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    mario how can I bring line 10 into line 29? Would this work?
    you initialized the variable loop in line 10, you should put the initialisation into the loop def
    Code:
            for( loop = 1; loop =10; ++loop)
    But temperatureArray has just 10 elements ( index 0 .. 9 ) the loop should be
    Code:
            for( loop = 0; loop < 10; ++loop)
    Kurt

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    thanks kurt. Would it need to be int loop though or is loop sufficient for it to be considered a declaration of a variable?

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    i copied n pasted that line in but placed int before the loop and i deleted the loop declaration from the local variable list and it doesnt work. I got these error messages. what do you think is causing that:

    1>------ Build started: Project: Assignment 3, Configuration: Debug Win32 ------
    1> testingarea2.c
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(16): warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(22): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(24): error C2143: syntax error : missing ';' before 'type'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(24): error C2143: syntax error : missing ';' before 'type'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(24): error C2143: syntax error : missing ')' before 'type'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(24): error C2143: syntax error : missing ';' before 'type'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(24): error C2065: 'loop' : undeclared identifier
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(24): warning C4552: '<' : operator has no effect; expected operator with side-effect
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(24): error C2059: syntax error : ')'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(25): error C2065: 'loop' : undeclared identifier
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(25): error C2143: syntax error : missing ';' before '{'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(34): error C2065: 'loop' : undeclared identifier
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(36): error C2065: 'loop' : undeclared identifier
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea2.c(36): error C2065: 'loop' : undeclared identifier
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    here is the complete code so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    	FILE *fp;	
    	int signalOutput;
    	int flag=0;
    	int column1;
    	float column2;
    	float temperatureArray[10];
    	int quit;
    
    
    	srand(time(NULL));
    
    
    	
    
    
    	printf("\n\n The sensor readings and their corresponding temperatures:\n");
    	
    		fp = fopen("c:\\LOOKUP.txt", "r");
    		
    		for(int loop=0; loop < 10; ++loop)
    		{
    			signalOutput = (int)(rand()%15);
    			
    			for(flag=0; flag=1; 0)
    			{
    				fscanf(fp, "%i %f",&column1, &column2);
    				
    				if ( signalOutput <= column1 ) 
    				{
    					temperatureArray[loop] = column2;
    					flag = 1;
    					printf("\n the %i reading was %i = %f", loop, signalOutput, temperatureArray[loop]);
    				}			
    			}
    		}
    			
    		
    	fclose(fp);
    
    
    	printf("Press enter to quit");
    	scanf("%i", &quit);
    
    
    	return 0;
    }
    I dont understand why I cant get this loop to work. Ive used loops loads of times before and never had problems. Its weird why it just jumps from 0 to 10 without going through any processes. I realy dont understand why thats happening

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You seem to use MS c compiler and that obviosly doesn't support C99.
    Therefore you have to declare any local variables at the top of the function.
    So leave line 10 as
    Code:
    int loop;
    and the loop as in my prev post.
    Kurt

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    ok thanks ill try that...

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    now loop is just staying at 0 and never increasing

    also flag is jumping straight to 1 still

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    1) The inner loop has the exact same problem as the outher loop had.
    2) check your logic. flag is set to 1 in the body of the inner loop.
    Kurt

  14. #14
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    I thought the flag=1 meant that when flag=1 end the loop? Wouldnt that make it correct?

    Also, in this case wouldnt the values be right as going from 0 to 1 is what has been specified as opposed to in the other loop where I specified 1-10 and 0-9 which dont correspond with each other?

    This is too confusing lol

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
                for(flag=0; flag=1; 0)
    sorry I don't understand this
    an assignment as condition doesn't make sense here.
    Also a literal 0 doesn't have any effect.
    if you want to loop from 0 to < 1 then this is only loopng once and you don't need a loop at all.

    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop not working properly
    By Shinzu911 in forum C Programming
    Replies: 3
    Last Post: 04-24-2012, 10:01 AM
  2. Not Working properly
    By Tejas Sanap in forum C Programming
    Replies: 7
    Last Post: 05-10-2011, 06:52 AM
  3. exit loop not working properly
    By melodia in forum C Programming
    Replies: 3
    Last Post: 11-21-2010, 06:04 PM
  4. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM
  5. why isn't my loop working properly?
    By orion- in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:23 PM