loop help.

How would I make a function with a endless loop inside of another function, and the loop will not effect the application from runing, it will not effect any aspect of the code, except for the function its in? I know what these are in LUA coroutines, does C++ have anything like this? Also how do I check if a int, string(etc) exists or not?
Not sure about your second question, but your first question could be solved by creating another thread. http://stackoverflow.com/questions/266168/simple-example-of-threading-in-c
Also how do I check if a int, string(etc) exists or not?

I'm not sure I understand this question. A variable exists if it's declared in the code. Otherwise, it doesn't.

Can you try and explain a bit more precisely what it is you're asking?
Last edited on
@Mikey, i think he's coming from a scripting background where thats not always the case.

@Johnny- variables always exist in compiled code, you only need to check their values.

coroutines are known as threads in c++ look up multithreading and in particular pthread

Last edited on
Well I wanted to do a loop, to check if there is a variable, and if there is not one, then it will make it. Also I will look that up, thank-you.
What do you mean by "if there is not one"? A variable is either declared in the code, or it isn't. It's not something that happens at compile time.
Like this


}else if(not x){

int x = 0;


Its pretty much just a if statement trying to find of if x is there and if not create it and set its vaue.
You don't seem to have understood how variables work in C++.

If you haven't already created (i.e. declared) x, then the first line of the code you posted will fail to compile. You need to declare a variable before you can use its name in code. There's no such thing in C++ as a variable that doesn't "exist" - because if your code tries to use the name of a variable that hasn't been declared, the compiler can't even create your program.

This should be covered by even the most rudimentary of textbooks or tutorials.

Edit: And please use code tags when posting code.
Last edited on
My head exploded reading that code.
maybe the preprocessors offer some similar functionality.
the include guards do what you want:
#ifndef someMacro
#define someMacro

but personally, i don't advice such approach, defining macros exhausts the global namespace, so define them only when needed.
also, macros have a constant value, as they are "pre-processed" into your code.

what you want can't be accomplished in C++, maybe you should try it in an interpreted language.
i don't know if you can create dlls in an interpreted language, if so, try to create this part of the program in a dll, and use this dll in the C++ part.
Johnny, read my post again, you cant do that, the variable is GUARANTEED to exist.

compiled code creates variables at compile time.

in c++ when you say "if (not x)" you are saying "if (x==false)"

if x didn't exist, your code "if(not x)" would not compile so would be impossible to run.

this is because c++ is converted to machine code before it is run so everything has to exist already so the conversion (compilation) works.

interpreted languages like php and lua don't work like that, they generate code as they need to, JIT compiled (just in time) so you will often need to check if a variable has been created yet.

this is one of the fundamental differences between compiled languages and interpreted languages.

if your example, the int x will create the variable at function scope, but in c++ it will only exist in the braces that it was declared in, scoping is much tighter in C++.

the following code creates 4 variables all called x, each exists only within the braces it was declared in except x=1 which is global.
int x=1;
void myFunc()
    int x=2;
    cout << x;  // outputs 2
        int x=3;
        cout << x;  // outputs 3
            int x = 4;
            cout << x;  // outputs 4
        cout << x;  // outputs 3
    cout << x;  // outputs 2

Oh, okay. I think I understand now. Thanks everyone.
Topic archived. No new replies allowed.