c++ - Switch statement only works for some cases -


for(int i=0; i<3; i++){  switch(i)     case 0:     layout[i].x=i;     layout[i].y=i;     case 1:     layout[i].x=funcx(i);     layout[i].y=funcy(i);     case 2:     layout[i].x=2*i;     layout[i].y=4*i;} 

this simplified code having problem with. want code is, when i=0, whats in case 0, when i=1, whats in case 1 , on.

but here problem.. example when i=1, calculates correct .x (case 1) value .y calculates different such 0 or 2. tried put {} around code inside each case, made no difference. tried 1 3 instead..

ofstream zone1h;         zone1h.open("test.txt"); for(int l=0; l<5; l++)                       zone1h<<layout[i].x<<"  "<<layout[i].y<<endl; 

could saving part issue? never had problem part though..

you're missing break @ end of each case. it's falling through cases , last 1 taking effect.

for(int i=0; i<3; i++){   switch(i){     case 0:       layout[i].x=i;       layout[i].y=i;       break; // <-- add     case 1:       layout[i].x=funcx(i);       layout[i].y=funcy(i);       break; // <-- add     case 2:       layout[i].x=2*i;       layout[i].y=4*i;       break; // <-- add   } } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -