Thread: Breaking out of the loop

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    Breaking out of the loop

    When the user enters a correct ISO setting , he is told that but the program then continues and states that an incorrect value was entered .
    Code:
    case '2':
    
    
    fprintf(output,"%s please enter the ISO\n\n" ,name );
    fgets(ISO,10,stdin); //Prompt the user for the ISO
    p = strchr(ISO, '\n'); // This serves to remove the enter character which fgets registers . It eliminates the effect of \n.
    		if ( p ) 
         			*p = 0;
    	
    				for(j=0;j<32;j++) // This loop will go through the 12 array elements.
    				{
     					
    					if(strcmp(ISO_VALID[j], ISO)==0)  
    					// The strcmp will do a comparison of the string entered with allowed string values stored in the array)
    					{
    
    						fprintf(output,"\n\n	This ISO is on the list of possible values !\n\n");
    						ISO_done=1;
    						ISONumber = atof(&ISO[0]);
    						getchar();
    						break;
    					}
    
    
    				}
    			
    		fprintf(output," \n\n  Incorrect ISO value entered . Please try again !");
    		fprintf(output, "The program will now return to main menu");
    		break;
    What should Iuse for it to break out of the loop back to the main menu .

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    I came up with this solution for the problem . It probably is't optimal but it works .

    Code:
    case '2':
    
    if (ISO_done==1)
    {
    fprintf(output,"You have already entered the ISO value");
    break;
    }
    {
    fprintf(output,"%s please enter the ISO\n\n" ,name );
    fgets(ISO,10,stdin); //Prompt the user for the ISO
    p = strchr(ISO, '\n'); // This serves to remove the enter character which fgets registers . It eliminates the effect of \n.
    		if ( p ) 
         			*p = 0;
    	
    				for(j=0;j<32;j++) // This loop will go through the 12 array elements.
    				{
     					
    					if(strcmp(ISO_VALID[j], ISO)==0)  
    					// The strcmp will do a comparison of the string entered with allowed string values stored in the array)
    					{
    
    						fprintf(output,"\n\n	This ISO is on the list of possible values !\n\n");
    						ISO_done=1;
    						ISONumber = atof(&ISO[0]);
    						ISO_completed=1;
    						getchar();
    						break;
    					}
    
    
    				}
    		if (ISO_completed==1)
    			{
    			break;
    			}
    			{
    			fprintf(output,"\n	Incorrect ISO entered ! Please try again\n"); // What happens if an incorrect aperture is entered.
       			
    			break;  
    			}
    		}
                    	break;

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    Arrow Didn't want to create another thread

    I have another problem , I'm using atof to convert 2 strings like so :

    Code:
    ApertureNumber= atof(&guess[0]);
    ISONumber = atof(&ISO[0]);
    Then the program processes these :
    Code:
    float exposure;
    exposure=(ApertureNumber*ApertureNumber)/(256*ISONumber);
    fprintf(output,"the exposure value is &#37;f",exposure);
    return 0;
    If I understant right atof returns a double , so the above should work , yet I keep getting 0.00000
    Last edited by ICool; 09-24-2007 at 02:31 AM.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by ICool View Post
    I have another problem , I'm using atof to convert 2 strings like so :

    Code:
    ApertureNumber= atof(&guess[0]);
    ISONumber = atof(&ISO[0]);
    Then the program processes these :
    Code:
    float exposure;
    exposure=(ApertureNumber*ApertureNumber)/(256*ISONumber);
    fprintf(output,"the exposure value is %f",exposure);
    return 0;
    If I understant right atof returns a double , so the above should work , yet I keep getting 0.00000
    1. Indenting is crap. Fix it.
    2. Variables are named in a strange way. Try to be consistent, and consider looking up a naming convention for variables.
    3. Why are you passing &guess[0] to it? Why not just guess? Same with ISO? Try changing it.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    if ( p ) 
       *p = 0;
    Is that the only thing you want to do if p != 0 or did you forget to use braces?

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by cpjust View Post
    Code:
    if ( p ) 
       *p = 0;
    Is that the only thing you want to do if p != 0 or did you forget to use braces?
    Just what I've been preaching for ages...
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets and breaking out of while loop
    By pollypocket4eva in forum C Programming
    Replies: 25
    Last Post: 01-05-2009, 06:50 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Breaking out of a loop
    By escarrina in forum C Programming
    Replies: 9
    Last Post: 04-05-2004, 12:05 PM