Need to call function from within switch statement

Im writing a text based adventure game. Im a huge noob to C++, did a Udemy course but didnt understand most of it, so am doing this game and once I am done will return to Udemy.

Here is the snippet of code and what Im trying 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 basically want to be able to go back and reprint the options. Im not great with loops yet, I intuitively feel it could be done with a loop but am not sure how. Im using more switch cases of course, and nested switch statements for even further options. It all works fine, but Ive been stuck on this part for a few hours. Im trying to learn refactoring, so I put the above text into the function std::string Scene1Numerical(); but am not sure how to call it down below!
Last edited on
Try this

// REMOVED A BUNCH OF IRRELEVANT TEXT
std::cout << "SCENE 1 TEXT" << std::endl;
while (true)
{
	// THIS IS THE CODE I AM TRYING TO CALL
	char choice = {};

	std::cout << "\nWhat would you like to do?" << std::endl;
	std::cout << "Choose a number and hit enter to continues" << std::endl << 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 << "q. To quit the program" << std::endl;

	std::cout << "\nYour choice : ";
	std::cin >> choice;
	std::cout << std::endl;

	// if choice is q or Q, quit the while loop
	if (choice == 'q' || choice == 'Q'){
		std::cout << "Bye.." << std::endl;
		break; // this breaks the while loop
	}


	switch (choice)
	{
	case '1':
		std::cout << "CASE 1 STUFF" << std::endl;
		break; // breaks case 1, 
	case '2':
		std::cout << "CASE 2 STUFF " << std::endl;
		break;
	case '3':
		std::cout << "CASE 3 STUFF" << std::endl;
		break;
	}// END SWITCH
}// END WHILE


Last edited on
Topic archived. No new replies allowed.