How to call function from within switch statement

Feb 12, 2021 at 5:34am
Im composing a book based experience game. Im a gigantic noob to C++, did a Udemy course yet didnt see its greater part, so am doing this game and whenever I am done will get back to Udemy.

Here is the scrap of code and what Im attempting to do
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
30
31
  void Scene1()
{
       // REMOVED A BUNCH OF IRRELEVANT TEXT
	std::cout << "SCENE 1 TEXT" std::endl;

        // THIS IS THE CODE I AM TRYING TO CALL
	std::string Scene1Numerical();
	{
		std::cout << "What would you like to do?" << std::endl;
		std::cout << "1. Keep laying in bed." << std::endl;
		std::cout << "2. Get up and examine the room further." << std::endl;
		std::cout << "3. Listen carefully for sounds from the environment." << std::endl;
		std::cout << "4. Get up and examine the bed." << std::endl;
		std::cout << "5. Get up and examine the desk." << std::endl;
		std::cout << "6. Get up and examine the door. \n" << std::endl;
		std::cout << "Choose a number and hit enter to continue: ";

		std::cin >> Choice1;
		std::cout << std::endl;
	}

	switch (Choice1)
	{
	case 1:
		std::cout << "You keep laying in bed. Hours pass as you drift in and out of sleep." << std::endl;
		std::cout << "Waking up in the early morning, you are once again greeting by a haunting light." << std::endl;
		std::cout << "You are hungrier and weaker than before, cant keep doing this forever!" << std::endl;
		std::cout << "Please choose again... \n";
               // THIS IS WHERE I WANT TO CALL IT FROM

		break;


I fundamentally need to have the option to return and republish the choices. Im not extraordinary with circles yet, I naturally feel it very well may be finished with a circle however don't know how. Im utilizing more switch instances obviously, and settled switch explanations for much further choices. Everything turns out great, yet Ive been stuck on this part for a couple of hours. Im attempting to pick up refactoring, so I put the above content into the capacity std::string Scene1Numerical(); yet don't know how to call it down beneath!


https://www.facetimeapp.net/facetime-for-pc/
Last edited on Feb 13, 2021 at 3:58am
Feb 12, 2021 at 7:52am
A few problems with your function:
1) Line 7: Remove the ;
2) In C++, you can't nest a function within another function.
3) You're declaring that the function returns a string, but never do so.

Line 18: Where is Choice1 declared? This should be a different variable than the Choice1 used at line 22.
After line 19: Assuming Choice1 is a string, you need:
return Choice1;

 
  Choice1 = Scene1Numerical();  //  Call like any other function that returns something. 


Last edited on Feb 12, 2021 at 7:53am
Feb 12, 2021 at 7:53am
1
2
3
4
5
6
void Scene1()
{
    std::string Scene1Numerical() ;
    {
    }
}

First, the code above has three errors.
1. Line 3 declares a function. Lines 4-5 have nothing to do with it.
Without the semicolon the
1
2
3
    std::string Scene1Numerical()
    {
    }

would implement the function too.
2. However, implementation of one function cannot be in the body of another function. (Exception: lambda closures.)

Define Scene1Numerical() outside of Scene1():
1
2
3
4
5
6
7
std::string Scene1Numerical()
{
}

void Scene1()
{
}

3. The Scene1Numerical() promises to return a value. A std::string. Where is the return statement?

(4. There is probably one more error.)


How do you call a function in general?
Feb 12, 2021 at 3:28pm
Feb 12, 2021 at 5:09pm
Well you can do something like this if you want recursive functionality:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

void Scene1(int choice);

void Scene1Numerical()
{
	cout << "What would you like to do?" << endl;
	cout << "1. Keep laying in bed." << endl;
	cout << "2. Get up and examine the room further." << endl;
	cout << "3. Listen carefully for sounds from the environment." << endl;
	cout << "4. Get up and examine the bed." << endl;
	cout << "5. Get up and examine the desk." << endl;
	cout << "6. Get up and examine the door. \n" << endl;
	cout << "Choose a number and hit enter to continue: ";

	cout << "Your choice: ";

        int choice{ 0 };
	cin >> choice;

	Scene1(choice);
}


void Scene1(int choice)
{
	// REMOVED A BUNCH OF IRRELEVANT TEXT
	cout << "SCENE 1 TEXT" << endl;

	switch (choice)
	{
	case 1:
		cout << "You keep laying in bed. Hours pass as you drift in and out of sleep." << endl;
		cout << "Waking up in the early morning, you are once again greeting by a haunting light." << endl;
		cout << "You are hungrier and weaker than before, cant keep doing this forever!" << endl;
		cout << "Please choose again... \n";
		Scene1Numerical();
		break;
	}
}

int main()
{
	Scene1Numerical();
}


Or in your main you could just loop the program:

NOTE: endProgram does not have any terminating condition so it will loop forever.

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
void Scene1(int choice)
{
	// REMOVED A BUNCH OF IRRELEVANT TEXT
	cout << "SCENE 1 TEXT" << endl;

	switch (choice)
	{
	case 1:
		cout << "You keep laying in bed. Hours pass as you drift in and out of sleep." << endl;
		cout << "Waking up in the early morning, you are once again greeting by a haunting light." << endl;
		cout << "You are hungrier and weaker than before, cant keep doing this forever!" << endl;
		cout << "Please choose again... \n";
		break;
	}
}

int main()
{
	bool endProgram{ false };

	while (!endProgram)
	{
		Scene1Numerical();
	}
}
Last edited on Feb 12, 2021 at 5:13pm
Feb 12, 2021 at 5:18pm
Looping would be preferred in that situation because the recursive solution could eventually eat up the entire stack.
Feb 12, 2021 at 5:21pm
@Ganado, agreed, I would also choose that approach. @OP, the example I posted above is indirect recursion, you should use recursion when you cant use a loop, or when working with data structures like linked lists and binary trees, for something like this, a simple while loop will suffice and work perfectly.
Last edited on Feb 12, 2021 at 5:53pm
Topic archived. No new replies allowed.