Hello. Recently I have started to learn C++, however I ran into some problems. First of all, take a look at this part of code:

while (Uncomplete) {
if(counter > 8) draw = true;

if(draw == true) {
	PlaceCursor(0, 10);
	cout << "Its a draw!\n" << "continue?";
	getch();
	Uncomplete = false; }

counter++;
}

Obviously its just part of my code. I'm not sure why, but "if(counter > 8) draw = true;" activates every loop, even if counter is equal to 0. I have tried inserting cout << "counter"; to my code, and it shows Counter = 1, but the "If" still actives... What could be the problem?

Secondly... how do you return several values with a function?
return val1, val2;
Doesn't seem to work...

Also, what is a good way to initialize a variable in a function, just once? For example my function starts multiple times:

Func() {
bool var1 = true;
if(Globalvar1 == 1) var1 = false;
}

So, if I start this function, and Globalvar1 isnt equal to 1, then var1 turns to true even though I want it to stay false. I thought about using static variables, but that didn't work.

And the last question, how do you force an exit out of several loops?
For example:

bool Once = true;
for( ; ; ) {
	for( ; ;) {
		if(SomeVar1 == true) {
			cout << "cakes!"
				break;
		}
	}
}

This would break only the if statement, but not the two For's.

Thanks in advance!

>Secondly... how do you return several values with a function?
return val1, val2;

This isn't possible, but you can use a structure (struct) to do something equal:

struct multiple_vars {
    int a;
    int b;
    int c;
};

multiple_vars add(int n1, int n2)
{
    multiple_vars result;
    result.a = n1;
    result.b = n2;
    result.c = n1+n2;
    return result;
}

> Obviously its just part of my code. I'm not sure why, but "if(counter > 8) draw = true;" activates every loop
Is this your actual code, or part of it?
Or just how you "remember" it.

Some points.
1. draw is true on entry, and since there is no draw=false assignment, you're stuck with it being true.

2. A common mis-placed semicolon bug perhaps if(counter > 8)[B];[/B] draw = true; > Secondly... how do you return several values with a function?
> return val1, val2;
> Doesn't seem to work...
No it doesn't.
Pass pointers (or references) to additional locations where you can store additional results.

> And the last question, how do you force an exit out of several loops?
Quickly, use goto

Or use a state variable

bool Once = true;
for( ; cond && !Once ; ) {
	for( ; cond && !Once ;) {
		if(SomeVar1 == true) {
			Once = true;
			cout << "cakes!"
				break;
		}
	}
}
Comments
I didn't think of pointers and references, but it's actually a very good, simple and effectieve solution :)

> Secondly... how do you return several values with a function?

Just two examples of Salem's explanation:

Using goto :

int a = 0;
while(condition)
{
    while(condition)
    {
         while(condition)
         {
              if(a==5) goto stop;
              a++;
         }
    }
}
stop:
/* Code which has to be executed after the while loop(s) */

Using a state variable (also called a flag):

int a = 0;
bool flag = true;
while(condition && flag)
{
    while(condition && flag)
    {
         while(condition && flag)
         {
              if(a==5) flag=false;
              a++;
         }
    }
}

Edit:: Sorry Salem, I looked over your example (of a state variable) :(

Thanks for all the information guys! I'm amazed how fast I got a reply on these forums.
I found the problem with "draw", I accidently typed if(draw = true) instead of if(draw == true), my mistake :(

Now I'm just interested about the third question...

Comments
I'm glad you could solve this on your own :)

>And the last question, how do you force an exit out of several loops?
Ideally you would refactor the "several loops" into a function. Then to force an exit you simply return.

>what is a good way to initialize a variable in a function, just once?

... f(...)
{
    static bool var = true;
    ...
}

As usually, it's a symptom of a bad design ;)...

This article has been dead for over six months. Start a new discussion instead.