LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using switch in a function to call another function with a switch but it is not going into 2 switch ... (https://www.linuxquestions.org/questions/programming-9/using-switch-in-a-function-to-call-another-function-with-a-switch-but-it-is-not-going-into-2-switch-4175612276/)

BW-userx 08-19-2017 07:16 PM

using switch in a function to call another function with a switch but it is not going into 2 switch ...
 
I got this set up to use a switch to set some vars then call a function it goes into that function that has been called but then it does not go into the switch within that called function.

this is all withing the same C file but some function calls are out side of it. but they are seen.




Code:


void set_switch_random_setting(void)
{

printf("in timer set_switch_random_setting\n");


int get_ran_setting;

get_ran_setting = (rand() % 12);

printf("get_ran_setting %d\n", get_ran_setting);

switch (get_ran_setting)
{
case 0:
printf("C 0 get_ran_setting %d\n", get_ran_setting);
opts.cycle_system_color = 2; // two same colors with random angle
opts.set_random_angle = 2;
reset_colors();
break;
case 1:
printf("C 1 get_ran_setting %d\n", get_ran_setting);
opts.set_random_color = 1;
set_random_colors();
break;

.........

default:
break;

} //end switch


}
// the function being called in question is (they all are really) but to make it short.


void set_random_colors(void)
{
printf("HELLO in set_random_colors : opts.set_random_color %d \n",opts.set_random_color); <-- that is as far as it gets;
printf("HEY THERE opts.set_random_color %d", opts.set_random_color );


it gets to that printf above waits until timer is done then goes back to whence it came from, never entering this switch. which has been set in the sending switch.

 int i;
switch (opts.set_random_color)
{printf("IN SWITCH in set_random_colors : opts.set_random_color %d \n",opts.set_random_color);
    case 1:
        set_randy(); <-- which just calls this function ;
        break;
    case 2 ... MAX_SHOW_COLORS:
      //  printf("switch set_rans\n");
        for (i = 0; i < opts.set_random_color; i++)
            set_rans();
        break;
    default:
        break;
}//end switch
get_random_angle();
set_random_fill_two_color_angle();
}

the number for that switch in the second switch is valid

it will not even print the HEY THERE printf just the one above then waits time up reset and does it goes repeats ( depending on the random get_ran_setting = (rand() % 3); returned.

BW-userx 08-19-2017 08:53 PM

NEVER MIND I just side tracked that whole thing and just wrote it in the first switch. even though I have a different case not shown that is sent into a different function then within that function it is called to yet another function before returning to the first switch and that one had no issues. two function calls outside of the switch, before returning.

maybe it is a switch thing - I do not know, but I am marking this solved on grounds that I had to write the same code twice to get it to work. :cry:

astrogeek 08-20-2017 11:47 PM

Quote:

Originally Posted by BW-userx (Post 5750057)
Code:

switch (opts.set_random_color)
{printf("IN SWITCH in set_random_colors : opts.set_random_color %d \n",opts.set_random_color);
    case 1: ...
        break;
    case 2 ... MAX_SHOW_COLORS:
        break;
    default:
        break;
}


All else aside, C switch does not work that way. There are at least two obvious errors here, please see the language reference for the use of C switch. Please see the C/C++ Tutorials sticky link at top of the Programming forum for C/C++ language resources.

Please end your pattern of posting before doing basic research, and please do not treat the LQ programming forum as your development scratchpad for transient coding ideas. Make your design choices, perform your basic troubleshooting and research before posting, then if you still need help post a minimal example of the specific problem you are having, without all the irrelevant application specific code.

These hasty and ill-considered threads will continue to show up in search results for years to come, and lead those lookiing for help to dead ends with no useful information. This is not healthy for LQ, not helpful to others and not beneficial to yourself.

Please take this as a warning that this behavior must stop, now. Review the LQ Rules, Site FAQ and other resources available from the sidebar if you have questions.

This thread is now closed.


All times are GMT -5. The time now is 08:49 PM.