Why does using a looping switch in the main function affect other functions?

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
26
27
28
29
while (userChoice != 3)
{

switch (userChoice)
{
case 1:
someFunction();
break;

case 2:
anotherFunction();
break;

default:
cout << "\n\n""ERROR: Wrong input""\n\n";
this_thread::sleep_for(chrono::milliseconds(2000));
break;

} 

}

//This was my previous method:

if (userChoice != 3)
{
main();
}
Last edited on
I was able to solve it, the problem was that the input for the userChoice variable was placed outside the loop in the main function so it entered the called function back and forth.
Hello jsmithplus,

The use of code tags is good, but watch your indenting. Have a look at https://en.wikipedia.org/wiki/Indentation_style Personally I prefer the "Allman" style.

Why does using a looping switch in the main function affect other functions?

Without seeing the whole program it is hard to say. One possibility is that you are calling "main" from within "main". This is not done. Only the operating system calls "main" when it starts the program.

By calling "main" in your program you are essentially starting the program over and that is not what you want. This is where a do/while or while loop is used.

Most times it is best to post the whole code as someone else might see another problem.

Hope that helps,

Andy
Topic archived. No new replies allowed.