Thread: Stupid question, repeating a loop iteration?

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    6

    Stupid question, repeating a loop iteration?

    Hey all, so I am working on a very simple program and it's almost to where I want it but I cannot figure out for life of me how to get this iteration to repeat when the user does not enter the value required


    here is the portion of the code:

    Code:
             for (x = 1; x <= numb_of_grades; x = x + 1)          
              {   /*  begin 'for X' loop.  */
                  printf ("Enter grade #%i:", x);
                  scanf ("%i", &grade);
                  
                  
                  if ( grade < 0 || grade > 100)
                       printf ("*** Invalid entry. Grade must be 0 to 100. ***"); 
                    
                   
                  else if ( grade >= 0 || grade <=100 )             
                          gradeTotal = gradeTotal + grade;
                        while ( (c = getchar() != '\n') && c != EOF);
                  
               
               
                     
                  
              }  /*  end 'for X' loop.  */
    So all I want is the "invalid entry" to keep the user from going past the iteration he is on. So if he enters 4 grades I want it to do this:

    Grade #1: 100
    Grade #2: -30
    *** Invalid entry. Grade must be 0 to 100. ***\
    Grade #2: 30
    Grade #3: 100
    Grade #4: 100

    Right now it just continues on through the loop.

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You may want to create another loop there, which will execute as long as grade < 0 || grade > 100 condition is true, and in the body will ask for the grade again.

    Something akin to:
    Code:
    do {
        //ask the user politely for input
        //if (input is inadequate) - beat the living... I mean, inform the user politely that it's erroneous
    } while (input is inadequate);

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    6
    Code:
    int x, numb_of_grades, grade, gradeTotal = 0;	float finalavg = 0;
    	char c; 
        	
        
        /*  Display initial greeting */
    	/*  ------------------------ */
    	
    	printf ("This program calculates the average of as many grades you wish to enter.\n\n"); 
    
    
        
        numb_of_grades = 0;
        do{
        	
        		printf ("First, enter the number of grades to process:"); 
                scanf ("%i", &numb_of_grades);
                while ( (c = getchar() != '\n') && c != EOF); 
                
       	   	    if ( numb_of_grades < 2 );
        	        printf ("You must enter at least 2! \n\n");
        	 
        }while ( numb_of_grades < 2);
        
                  printf ("Enter grade #%i:", x);
                  scanf ("%i", &grade);
        
         return 0;
    How would you make so it doesnt say the input is inadequate if it isnt?

  4. #4
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Check line 19. That's why some choose to use curly brackets everywhere a block can be made, even if it's empty or just for one instruction. It helps alleviating such mistakes.

    Also, line 16 - %i and %d aren't the same. Check scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s - cppreference.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-19-2012, 02:58 PM
  2. Replies: 11
    Last Post: 10-08-2011, 10:24 AM
  3. Iteration of loop
    By funky in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 07:26 AM
  4. What the? Loop repeating to much?
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2006, 05:17 PM
  5. Loop iteration v's vectors
    By sononix in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 10:25 AM

Tags for this Thread