Find the answer to your Linux question:
Results 1 to 3 of 3
Here’s a program that asks the user for 20 responses, then adds them up and displays the stats. Its like a rating program with a range 1 - 10. The ...
  1. #1
    Linux User
    Join Date
    Jun 2007
    Posts
    458

    Usage of "break;"

    Here’s a program that asks the user for 20 responses, then adds them up and displays the stats. Its like a rating program with a range 1 - 10. The following goes perfect as exampled by the book:

    Code:
    #include <stdio.h>
    
    int main (void) {
    	int ratingCounters[11], i, response;
    
    	for ( i = 1; i <= 10; ++i )
    		ratingCounters[i] = 0;
    
    	printf("Enter your responces\n");
    
    	for ( i = 1; i <= 20; ++i ) {
    		scanf("%i", &responce);
    
    		if ( responce < 1 || responce > 10 )
    			printf("Bad responce: %i\n", response);
    		else
    			++ratingCounters[responce];
    	}
    
    	printf("\n\nRating      Number of rResponces\n");
    	printf("--------------       ---------------------------------\n");
    
    	for (i = 1; i <= 1-; ++1 )
    		printf("%4i%14i\n", i, ratingCounters[i]);
    
    	return 0;
    }
    However, in an exercise, I have to modify it so any number of responces can be entered. And the number 999 can be used to terminate the loop. I know I ave to make it an infinite loop, remove the init_expression. But how use 999 with break; to end the loop?

    Please explain the use of break statement.
    "When you have nothing to say, say nothing."

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Code:
    #include <stdio.h>
    
    int main (void) {
            int ratingCounters[11], i, response;
    
            for ( i = 1; i <= 10; ++i )
                    ratingCounters[i] = 0;
    
            printf("Enter your responces\n");
    
            for ( i = 1; i <= 20; ++i ) {
                    scanf("%i", &response);
    
                    if (response == 999)
                            break;
    
                    else if ( response < 1 || response > 10 )
                            printf("Bad responce: %i\n", response);
                    else
                            ++ratingCounters[response];
            }
    
            printf("\n\nRating      Number of rResponces\n");
            printf("--------------       ---------------------------------\n");
    
            for (i = 1; i <= 10; i++ )
                    printf("%4i%14i\n", i, ratingCounters[i]);
    
            return 0;
    }
    In the second for loop, when user enters 999 as the response, the loop will terminate and program continues.

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    458
    Thank you. In fact, I just wanted the break . All others were typos. This program is typed in notepad. Not compiled. But the previous one was. (without typos). That worked.
    "When you have nothing to say, say nothing."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...