Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I have a large program which is encompassed by a do... while loop. It is dependent on a variable going under a certain number.
 
Here's the catch: The variable is found inside a for loop inside a for loop inside the do...while loop.
 
yeah, maybe I should change my layout (not entirely sure how).
 
To save me, does anybody know of a way I could access this variable or somehow escape the entire sequence? eventually this program will be just the start of another... so I can't say "stop everything".
 
Many thanks if there is a way, else sorry for the hastle! Have a nice day Smile | :)
Posted 3-Nov-12 7:05am
Comments
GeekBond at 3-Nov-12 13:18pm
   
Could you share some of the code, we can't see your screen. :) And have you thought of 'break;'?
CPallini at 3-Nov-12 17:26pm
   
Cannot you just set the variable value inside another thread?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

easy, use another variable which can store the value of your desired variable.
use label on your specific point and use goto statemnt to get there
 
int x;
for (;;)
for(;;)
for(;;)
{
 int y=123;
 if(y==123){ x=y; goto mylabel;}
}
 
mylabel:
cout << x;
 
goto is not a good approach but has its advantages, when you dont want to use return statement and do want make a jump, just that goto can jump anywhere in a function which makes it dangerous....
 
ps: if you dont want to leave your sequence but just want to see your variable than remove goto statement from if clause. if it is not just a value you want, you can also use a pointer type to access your variable.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Your programming style is wrong. Study best programming practices. You need to adopt a good programming style. If your program is hard to maintain as it becomes overgrown, this means your program is not scallable. Need to refactor. You have to move some piece of code in separate functions, and you instantly will escape many of your problems. Use debugger to see what your program is doing, and what your variables are. Even moving and separating functionalties is not enough. You will need OOP in this case.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Your problem can be solved in several ways. The first you should notice that your coding style isn't alright, you are probably writing Spaghetti code[^]. That's not a problem especially if you are a beginner because I'm pretty sure everyone starts with spaghetti. The problem is if you keep that style because some people tend to do so either becuase of stupidity or lazyness. Spaghetti is usually a result of bad code design that is the result of bad or no Separation of concerns[^]. You are trying to implement of lot of things (or everything) inside one function instead of breaking that stuff into several functions or classes. In general, if your function spans vertically more than what you can read on your monitor on "one page" then your code is spaghetti and needs splitting into several functions or classes. The solution to the problem of breaking out more than one loop is missing from C/C++, in some other languages its present, for example in java. If you take a look at the following page and search for the "search:" label on it then you will see a java snippet that breaks out from 2 loops: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html[^]. However in C/C++ you can not do that but you can achieve similar behaviour. One of them is using an exception what I don't recommend in C++ and is not available in C. The other solution is putting the whole spaghetti code with all loops into a separated function and then instead of breaking out from the loop you return from the function call where you want to break from several loops. This way your program doesn't exit, just returns from a function. Putting things to a separate function call is a good solution to a lot of problems in C.
 
Example:
// an ugly spaghetti solution of breaking out from 2 loops but sometimes it does the job
bool stop = false;
for (int i=0; i<5; ++i)
{
    for (int j=0; j<5; ++j)
    {
        printf("%d, %d\n", i, j);
        if (i==3 && j==3)
        {
            stop = true;
            break;
        }
        if (stop)
            break;
    }
}
 
// the function + return solution
void perform_loops()
{
    for (int i=0; i<5; ++i)
    {
        for (int j=0; j<5; ++j)
        {
            printf("%d, %d\n", i, j);
            if (i==3 && j==3)
                return;
        }
    }
}
 
// calling the function
perform_loops();
 
If you have a big ugly function with a lot of local variables with long code then its the best to do the following as a first step of refactorization:
1. Create an empty class and put the local variables of the function into the empty class as member variables.
2. Put the whole original function code into the class so that it uses the member variables of the class instead of local variables.
3. Split the method in the class into several methods, its quite easy since every method of the class can access every member variable of the class.
4. Now you have several methods in the class that depend on each other via member variables (variable initialization order, call order) so your next task is to make that dependency cleaner - this is much easier to solve than the original problem you started from (spaghetty/monster func). During this you might find out that some methods use only some of the member variables of the class so you can split those member variables into another class with all the methods that use those member variables and then you can use the second class from the first one.
 
Now that you have your new class (or classes) your original ugly function looks like the following:
int previously_ugly_func(params)
{
    MyNewPrettyClass c;
    return c.DoWork(params);
}
Instead of calling the original ugly func you can directly use the new class that solves the same problem you original ugly func solved.
  Permalink  
v3
Comments
nv3 at 5-Nov-12 14:14pm
   
Very good answer for a very vague question! Thumbs up. This is really good advice.
pasztorpisti at 5-Nov-12 14:45pm
   
Thank you! :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you can control your heap allocations - inside the large function - by the smart pointers
then you could try the "Exception-Trick"[^] as well Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100