Alternatives of goto in my C++ console application -
i've got point had deal logic in c++ project, , unfortunately not able right using goto
.
what problem ?
i have 1 function, named processrunning(const char *name)
verifies if process exists (it returns true
) or not (it returns false
).
i have 4 functions, int fun1(const char name[])
,int fun2(const char name[])
,int fun3(const char name[])
, int fun4(const char name[])
. - each of these functions looks this:
int fun1(const char name[]){ //open new .bat file //start of write in .bat //taskkill process.exe //start again process.exe //delete created file //end of write in file return system(name); }
and same other 3 functions.
now, in main()
program trying following:
int main(){ //... start_again: if(processrunning("process1.exe")){ fun1("fun1.bat"); goto start_again; } else if(processrunning("process2.exe")){ fun1("fun2.bat"); goto start_again; } else if(processrunning("process3.exe")){ fun1("fun3.bat"); goto start_again; } else if(processrunning("process4.exe")){ fun1("fun4.bat"); goto start_again; } else if(!processrunning("process1.exe") || !processrunning("process2.exe") || !processrunning("process3.exe") || !processrunning("process4.exe") || ){ goto start_again; } goto start_again; return 0; }
now whole program do:
- the program must run;
- it checks if 1 of 4 processes running;
- if there's no such process, go , check again (so there's permanent check if of conditions aren't satisfied)
- if finds
process1
(the first if), creates .bat file , executes (kill first process, start again, delete.bat
file). process running, program should still go through otherif
s , check if there'sprocess2
,process3
orprocess4
running , same thing them.
every process assigned application, let's say:
app1 -> goes process1
app2 -> goes process2
..
app4 -> goes process4
cases:
- process1 running
- process1 not running
.. - and on
sake of example i'll takeprocess1
should apply of them.
because app running, checks if there's process1. if there isn't, checks process2 , if none found, start on again , again. otherwise, if process1 found, what's in if
condition still after other processes.
now, problem have can't find better solution keep console program running time , make these checks. more, actual implementation i've found far doesn't treat situations.
any advices on this, please (improvements of logic, critiques, alternatives)? (sorry long post , possible grammar mistakes).
if want check existance of each process every loop , want run forever can use while loop.
int main(){ //... while(true) { if(processrunning("process1.exe")){ fun1("fun1.bat"); } if(processrunning("process2.exe")){ fun1("fun2.bat"); } if(processrunning("process3.exe")){ fun1("fun3.bat"); } if(processrunning("process4.exe")){ fun1("fun4.bat"); } } return 0; }
we don't want use else if
here never check process if 1 process found. using if
check each process each iteration.
edit:
from comments appears want run function once. can add bool each process , check in if condition. if true not run function again. can change code to:
int main(){ //... bool proc1 = false, proc2 = false, proc3 = false, proc4 = false, while(true) { if(!proc1 && processrunning("process1.exe")){ fun1("fun1.bat"); proc1 = true; } if(!proc2 && processrunning("process2.exe")){ fun1("fun2.bat"); proc2 = true; } if(!proc3 && processrunning("process3.exe")){ fun1("fun3.bat"); proc3 = true; } if(!proc4 && processrunning("process4.exe")){ fun1("fun4.bat"); proc4 = true; } } return 0; }
now if procx
false check if process running , if run fun1()
set procx
true. otherwise if procx
true rest of if statement skipped.
Comments
Post a Comment