How to break out of a for loop ?

Guys,is there a better way to get out of the function procedure when months==0, without using the break. I try using a bool value on the for loop but it does not work for me. The reason I wanted on the for loop is because if I break when month==0 it still prints the remaining numbers of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
int prodedure(int number_days_of_months[], int days_month, int days[], int size_days, int months[], int months_size);
void display(int months[], int months_size, int days[],int days_size);

int main()
{
    
    int months[10]; //stores the month enter by the user
    int days[10];   //stores the days enter by the user
    int number_days_of_months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //representing each of the 12 months of the year
    
    
    prodedure(number_days_of_months, 12, days, 10, months, 10);
    display(months, 10, days, 10 );
    
    
    
    
    
    
    return 0;
}



int prodedure(int number_days_of_months[], int days_month, int days[], int size_days, int months[], int months_size)
{
    //bool isGameRunning = true;
    
    for (int i = 0; i < 10; i++)
    {
        
        
        cout << "Enter dates in mm/dd format Example 5 31 (end by entering 0 for month):" << endl;
        cin >> months[i] >> days[i];
        
        while (months[i] > 12)
        {
            cout << "enter a valid month" << endl;
            cin >> months[i];
        }
        if (months[i]==0)
        {
            
            cout << "month is 0 it should had exit";
           
            break;
        }
        
        
        
        
        
    }
    
    
    return 0;
    
}
Last edited on
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

// return integer entered by the user
// retry on failed input
int get_int( const char* prompt = "" )
{
    int value = 0 ;
    std::cout << prompt ;
    if( std::cin >> value ) return value ;

    else
    {
        // input failed: user did not enter a number
        std::cout << "please enter an integer\n" ;
        std::cin.clear() ; // clear the error state
        std::cin.ignore( 1000, '\n' ) ; // discard the characters remaining in the input buffer
    }

    return get_int(prompt) ; // invalid input; try again

}

// return true for input of valid month (1-12)
// return false for end of input (month==0)
// retry on invalid input
bool get_month( int& month )
{
    month = get_int( "month (end by entering zero)? " ) ;

    if( month == 0 ) return false ; // end of input; return false

    if( month > 0 && month < 13 ) return true ; // valid month; return true

    std::cout << "invalid month. try again\n" ; // invalid month
    return get_month(month) ; // try again

}

int main()
{
    const int N = 10 ;
    int months[N] {} ;
    int days[N] {};
    int cnt = 0 ; // count of months input by the user

    // accept month, day up to a maximun of N entries or till zero is entered for the month
    for( ; cnt < N && get_month( months[cnt] ) ; ++cnt ) days[cnt] = get_int( "days? " ) ;

    // print out the months and days that were entered
    for( int i = 0 ; i < cnt ; ++i ) std::cout << months[i] << " : " << days[i] << '\n' ;
}
c++ has an excellent goto statement and breaking out of nested loops used to be the only acceptable use of it. The above is cleaner with this code, but there are places where the goto makes sense.

for( 1...)
for(2...)
for(3...)
{
if(exitcond)
goto done;
}


done:

Topic archived. No new replies allowed.