Hey guys, this should be an easy answer for all you experts out there.
Im making myself a text based RPG, so i can see how deep i understand the language. And i wanted some help creating my own personalized system("PAUSE").
Im having a tough time getting it to work. Now, i dont want a snippit of code to JUST to replace the system("PAUSE") commonly placed at the end of a main function, like most beginners are taught to do. I want a PAUSE that would would work anywhere in my program
I tried this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void SomeFunction()
{
while(){
std::string name, confirm;
std::cout << "enter your name: "
...
...
std::cout << "Are you sure this is your name?"
std::cin >> confirm;
if(confirm == "No")
{
"I see, Press ENTER to try again"
cin.clear();
cin.ignore(); //This ignore didnt work
continue;
}
}
}
|
When i put this in, it went straight through the ignore, to the continue
1 2 3 4 5 6 7 8 9
|
std::cin >> confirm;
if(confirm == "No")
{
"I see, Press ENTER to try again"
cin.clear();
cin.ignore(1); //This ignore STILL didnt work
continue;
}
|
I tried to adjust like this, thinking it would erase the 1 '\n' between cin >> confirm and cin.ignore(), but it didnt act like i wanted it to, and it still went straight through, past the ignore, to the continue
1 2 3 4 5 6 7 8 9
|
std::cin >> confirm;
if(confirm == "No")
{
"I see, Press ENTER to try again"
cin.clear();
cin.ignore(1, '\n'); //This ignore didnt work
continue;
}
|
Tried this, as you can tell, i dont know what the heck i am doing!
1 2 3 4 5 6 7 8 9
|
std::cin >> confirm;
if(confirm == "No")
{
"I see, Press ENTER to try again"
cin.clear();
cin.ignore(2); //Ignore finally works, but has a huge flaw T_T
continue;
}
|
Just playing trail and error, i got to this point, and the ignore finally had a notable effect! Except alas, i needed to hit enter TWICE before it moved to the continue;
So now im really confused. I really dont want to use system("PAUSE"), not because i was told it was bad, but because if i just use it... well then that means im not getting any better.
Im sure i will find the answer in the book im reading "C++ Primer 5th", but im only on chapter 3, and im sure it will be while until my problem is addressed
Sense i barely know what the heck i am doing with cin.ignore, im at a huge loss
i didnt read how to use it in an article or book, and i wasnt told what i am doing WHILE im using it
I was just bluntly told
"Use this instead of system("PAUSE")"
"system() is NOT good to use"
************
Conclusion
************
SO, im looking for this answer:
How can i make my own custom system("PAUSE"):
I want to be able to choose the button pressed to continue.
(EX: Press Esc to exit...) (Press Z to attack or Press X to flee)
If its also not too much trouble, im wondering how i can clear the screen without using system("CLS") aswell.
Something like this in human terms
1 - Pause Program
2 - If user hits Esc (exit)
* - If user hits Z (attack)
* - If user hits X (flee)
3 - Clear Screen
4 - Execute {exit()||attack()||flee()}
THE CLEAR SCREEN IS NOT AS IMPORTANT AS THE CUSTOM PAUSE.
Please focus on the pause in your posts, and let the clear screen come secondary
Thank you in advance for taking the time to read and/or answer my question =]
You rock if your reading this sentence right now!