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
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!
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.
#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();
}
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();
}
}
@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.