LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 08-19-2017, 07:16 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
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.

Last edited by BW-userx; 08-19-2017 at 07:21 PM.
 
Old 08-19-2017, 08:53 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Original Poster
Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
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.

Last edited by BW-userx; 08-19-2017 at 08:54 PM.
 
Old 08-20-2017, 11:47 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,420
Blog Entries: 11

Rep: Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369Reputation: 3369
Quote:
Originally Posted by BW-userx View Post
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.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch Function with multiple NICs Guestx Linux - Networking 2 03-05-2016 05:23 AM
how to call a function inside switch case sarathius Programming 4 01-28-2008 05:36 AM
pc modify to switch with router function? hocheetiong Linux - Networking 1 12-23-2007 03:51 AM
How to switch VI's backup function off ?? linzz Fedora 3 03-25-2006 07:00 PM
switch statements calling a function tekmorph Programming 2 10-19-2004 05:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration