#include <iostream>
#include <string>
#include <cmath>
#include <time.h>
usingnamespace std;
void first();
void first()
{
int x;
cout << "You awake in a strange forest, you are dizzy and disoriented, and you see a large" << " \n" << "castle far in the distance. You feel a sharp pain in your back. You black out. \n" << endl;
cout << "1) Wake-up" << endl;
cin >> x;
if (x==1)
cout << "You awake in a prison cell, in what looks to be the large castle you observed" << " \n" << "earlier. \n";
else
first();
}
int main()
{
first();
system("pause");
return 0;
}
When I run this, everything works fine, up until I enter something other than 1 for the variable X. It just makes the loop go on forever. What do I do to get this to work?
Firstly, This has neither a do-while or a while loop.
Secondly, You're using recursion here. If you recurse too much you can smash your stack.
Thirdly, Your problem is that you're using cin >> x; without using a cin.ignore() to ignore any newline characters stored in the buffer.
that just makes a new line I can't do anything on. Like last time. But that isn't the problem. The problem I need fixed is exactly what I stated before. "I wanted to know how to just go back to the variable 'first' without any input after you enter something other than 1."