Problems with a switch-statement in a for-statement

Hello dear users!

I want to execute following code, but it doesn't really apply what's within the for-statement - meaning: the switch-statement is totally being left out. Does anybody have an idea what I'm doing wrong? Thank you.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream> 
#include <cmath>

using namespace std;

    int months;

int monthdays()
{
    
      int j;
      int months3(7);
      int month(3);
      int a(0);
     
 for (int i=month; months3 == i; i++)
 {

  switch(i)
  {
  
  case 1:
       
    j += 31;
    
  break;
  
  case 2:
       
  if (a == 0)
  {
        
    j += 28;

  }
  else
  {

    j += 29;
      
  }
   
  break;
  
  case 3:
       
    j += 31;
    
  break;
  
  case 4:
       
    j += 30;
    
  break;
  
  case 5:
       
    j += 31;
    
  break;
  
  case 6:
       
    j += 30;
    
  break;
  
  case 7:
       
    j += 31;
    
  break;
  
  case 8:
       
    j += 31;
    
  break;
  
  case 9:
       
    j += 30;
    
  break;
  
  case 10:
       
    j += 31;
    
  break;
  
  case 11:
       
    j += 30;
    
  break;
  
  case 12:
       
    j += 31;
    
  break;
  
  default:
       
    abort();
    
  break;

  }
  
 }
    
return j=months;
     
}

int main()
{
    
    monthdays();

    cout << "The amount of \"Months\" are:" << endl;
    cout << months << endl;
 
 system("pause");
    
}
Look at the values you are starting your for loop with:

1
2
3
4
int months3(7);
int month(3);

for (int i=month; months3 == i; i++)


How is the condition ever going to be met? months3 == 7 and i ==3 so months3 is NOT equal to i and so the loop will never execute.
I see. Well, maybe I'm misunderstanding the for-statement, then?

1
2
3
4
5
for(
int i = month;   // Here's the initialization of the counter variable i. i equals month, = 3.
i == months3;  // Here's the condition part.. I basically tell the for-statement to loop until i equals months3, meaning the loop will stop when i(3) equals months3(7).
i++;                 // Here's what happens to the counter variable. It's being incremented for each loop that takes place. So i(3) will become months3(7) in 4 loops ...
)


I'm not sure if the problem lies there? I believe I've met the conditions correctly, how else would I tell the loop to loop until i(3) equals months3(7)?

I fear it's the switch-statement that doesn't go off, since it doesn't actually take i and do something with it.
Currently the loop will never execute. You need to change

i == months3;

to

i <= months3;

Now the loop will execute 5 times, i will equal 3, 4, 5, 6 and 7.
This part tells the for loop to execute until this condition is false.
Topic archived. No new replies allowed.