Click here to Skip to main content
14,244,009 members
Rate this:
Please Sign up or sign in to vote.
See more:
this is a testing project, i want to jump around functions

#include <iostream>

using namespace std;

int function1()
{
    cout<<"function1 (2 or 3)"<<endl;
    int x;
    cin>>x;
    return x;
}
int function2()
{
    cout<<"function2 (1 or 3)"<<endl;
    int x;
    cin>>x;
    return x;
}
int function3()
{
    cout<<"function3 (1 or 2)"<<endl;
    int x;
    cin>>x;
    return x;
}

int main()
{
    while(true)
    {
        switch(function1())
        {
        case 2:
            {
            switch(function2())
                {
                case 1: function1();
                case 3: function3();
                }
            }
        case 3:
            {
            switch(function3())
                {
                case 1: function1();
                case 2: function2();
                }
            }
        }
    }
}
Posted
Comments
   
I have no idea what it may mean, "jump around functions". What are you trying to achieve? Why writing so messy code? Yes, I can understand if you do it for study purposes...
—SA
Member 12246238 5-Jan-16 19:29pm
   
i want to jump around functions like in a simple menu but i dont know how make it work, im doing it for study purposes
Patrice T 5-Jan-16 20:16pm
   
Explain what is wrong in this code ? What it is not doing as you expect ?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

I think you are seeking a way to pass the return value of each function to the switch so that the desired function can be executed. Try this:

int main()
{
	int f = 1;
	while (true)
	{

		switch (f)
		{

			case 1:
				f = function1();
				break;

			case 2:
				f = function2();
				break;

			case 3:
				f = function3();
				break;

			default:
				break;
		}
	}
}
   
v2

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




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