Good Afternoon,

I'm having problems with this assignment that deals with loops that implements the continue statement. Can someone check the loops that I have implemented on Part B and Part C that must have the continue statement. Here is the code

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>
#include <limits.h>              //changed by sirisha
using namespace std; 

int main( )
{
    //variable declarations
    int number;                 //var to store an int value
    int sum, min, max, avg;     //vars to store simple statistical info
    int counter;                //a counter for controling the loop
    int size;                   //another counter

    /*****************************************************************************
    Part A. continue in a while loop
    *****************************************************************************/
    cout<<"************** PLay Counter Controled do-while-Loop************/"<<endl; 

    //initialization
    sum=max=avg =0;
    min=INT_MAX;                //min has the maximum integer valued assigned//changed by sirisha
    cout<<" Enter How many integers do you want to play with? => "; 
    cin>>counter;               //get the counter
    size = counter;             //memory the counter

    //start the loop
    while (counter > 0)
    {       
        cout<<"Enter an interger ==> ";               
        cin>>number;            //get a number

        //here we use a continue to skip odd numbers
        if (number % 2 ==1)
            continue;           //to skip all statement from here to the end of the while loop, 
                                //and start the next iteration of the loop.
        sum += number;          //changed by sirisha            
        if (number > max)
            max = number; 
        if (number < min)
            min = number;

        counter--; 
    } 
    cout<<" You entered "<< size << " many integers. " <<endl
        <<" The sum is " << sum << endl 
        <<" The min is " << min << endl
        <<" The max is " << max << endl
        <<" The Avg is " << sum / size << endl; 



    /***************************************************************
    Part B. This part is for you to complete. 
    Re-do Part A using continue in an event controlled for-loop.
    ***************************************************************/
    cout<<" PLay event controled for-loop with -99 "<<endl; 
    counter=sum=max=avg =0;
    min=INT_MAX;
    cout<<"Enter an interger ==> ";               
    cin>>number;                //get a number

    //start the loop
    for (cout<<"Enter an integer ==> ",cin>>number;                       //initialization
         true;                      //event control
          cout<<"Enter an integer ==> ",cin>>number)                      //update
    {
        //add the loop body
        if (number == -99)
        {
            cout<<"Event -99 happened"<<endl;
            continue;
        }
        counter += 1;
        sum += number;
        if (number > max)
            max = number;
        if (number < min)
            min = number;
    } 
    cout<<" You entered "<< counter << " many integers. " <<endl
        <<" The sum is " << sum <<endl
        <<" The max is " << max << endl
        <<" The min is " << min <<endl
        <<" The Avg is " << sum/counter <<endl; 


    /**************************************************************
     Part C. This part is for you to complete. 
     Re-do Part A using continue in an event controlled do-while-loop
     **************************************************************/
    cout<<" PLay event controled do-while-Loop with event -99 "<<endl; 

    counter=sum=max=avg =0;     //initialization
    min=INT_MAX;                //changed by sirisha

    //start the loop
    do{
        //add the loop body
        cout<<"Enter an interger ==> ";               
        cin>>number;            

        if (number != -99)      
        {
            counter++;  
            continue;
            sum += number;                  
            if (number > max)
                max = number; 
            if (number < min)
                min = number;
        }
    } while (number != -99);  

    cout<<" You entered "<< counter << " many integers. " <<endl
        <<" The sum is " << sum <<endl 
        <<" The min is " << min <<endl
        <<" The max is " << max <<endl
        <<" The Avg is " << sum/counter <<endl; 




    //well done and exit
    return 0; 
}

Check lines 68-70 - the test condition is "true" (that is, it will always be "true") - do you really want that? Shouldn't you have a condition you can test to see if you want to keep looping?

Line 110 - what happens to the lines after the "continue" statement? Consider re-arranging the lines so you add to your counters, but perhaps "continue" when your exit condition is met.

This article has been dead for over six months. Start a new discussion instead.