Starting function inside while loop

Hi,
I would like to know if I start a function inside a while loop will this loop be ever finished/closed if that function actually never ends? I wanted to make my program anticrashing you know like if there are 3 options if you write someting bad it will repeat the question etc. and im using while loop for that which can be a big mistake. If I start a couple of loops like that will it consume more and more memory? Thanks in advance for constructive replies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	char ch;
	
	do
	{
         cin >> ch;
		if( ch == '1' )
		{
			Do something;
		}
		if (ch == '2')
		{
			Do something;
		}
		if (ch == '3')
		{
			Do something;
		}
		if (ch == '4')
		{			FunctionWhichContainsAnotherFunctionAndAnotherSoItPropoblyNeverEnds();
		}
		if ( ch != '1' && ch != '2' && ch != '3' && ch != '4' )
		{
			cout << "something";
		}
	}while( ch != '1' && ch != '2' && ch != '3' && ch != '4' );


2nd question. If I have two functions like these:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Function()
{
FunctionB();
}

void FunctionB()
{
FunctionA();
}

main()
{
FunctionA();
}

Will it open hundrets of functions and crash?
Last edited on
I don't think the code you provided does what you think it does.
The code inside the do{} block only gets executed once, then control switches to the while{} block where it will repeat until the conditional, in this case "ch!='1'&&ch!='2'etc", returns false.
You don't have any code inside the while loop, so nothing will get repeated.

1
2
3
4
5
6
do{
     //executes once then control moves to the while loop
}
while(conditional){
     //executes until conditional returns false
}
1
2
3
4
5
6
7


do
{
  //  whatever
} while ( !condition );

As far as I know there is no {} after while. It will simply repeat from "Do".
BTW. After some thinking I know the answer for my stupid question. It will open many functions/loops because it will never get to the moment it can finish. Sorry for useless spam.
Last edited on
Your first block of code, the one with the do while loop, is so nonsensical that I don't know how to respond to the "lets say I pressed 4" question regarding that code.

As for your 2nd question, I'm going to assume that you made a mistake and that you meant for "void Function(){ FunctionB(); }" to instead read "void FunctionA(){ FunctionB(); }

If that's the case, and assuming you forward declared FunctionB(), then yes that code will cause an infinite loop.
I said that the do{} block only gets executed once because you specified the case where you "write something bad" and the conditional doesn't get met? I don't know. Regardless of whether or not you use the do-while version or the while version, the results will be generally the same.

As for your initial statement where you said you wanted to repeat the prompt if the user enters something that doesn't make sense, then for purely educational purposes inside of a program that will not be put to practical use, I suggest something like

1
2
3
4
5
6
7
8
9
while(true){
     char input;
     cin>>input;
     if(IsNotAcceptable(input)) continue;
     else{
          //more conditionals
     }
}
          
Thanks for your advice. Actually I did something really similar to what you mentioned in previous post. Thanks ;)
Last edited on
Topic archived. No new replies allowed.