i was wondering how i can make it so i do something like:

cout << "Press any key to continue." << endl;

what kind of code do i need to put it so that the program dosnt do anything until someone presses any key on the keyboard?

A common one is cin.get() . It will work as long as you have a "clean" input stream.

well i just put cin.get(); in as a line of code and it just skipped on to the next thing...

Probably a dangling newline in the input stream (i.e. the stream is not "clean"). Add a cin.ignore() directly in front of it.

U can use fflush(stdin) also ..

U can use fflush(stdin) also ..

That is neither a standard use nor, because it's not standard, a reliable solution.

...
In some implementations this causes the input buffer to be cleared, but this is not standard behavior.
...

Flushing is an output operation that causes the current contents in the output stream's buffer to be output to their final destination. It is NOT an input operation.

how flushing stdin causes contents of output stream to be flushed????
It worked fine many times for me ..

>>how flushing stdin causes contents of output stream to be flushed????
The function is specified/intended/designed to be used with an output stream, not an input stream. Read my second quote again. It is non-standard behavior that only works "In some implementations...".

This means that it's compiler-specific behavior. When you're dealing with non-standard behavior, "It works for me" arguments are of no value. Just because your complier does that, doesn't mean another compiler will, or even should.

oh ok .. Thanks

There is another way to check if a key was pressed that no one mentioned. It's the kbhit() from conio.h library. As far as I kow it returns the int value of the charachter you typed in and 0 if you didn't.
Here is a basic example of using this function:

#include <conio.h>
#include <iostream>

using namespace std;

int main(void){
cout << "Please press any key..." << endl;//printing out the message
while(!kbhit());//waiting for a key to be pressed
return 0;
}

Basically what this code does it's making an infinite loop that stops only when a key is pressed.

There is another way to check if a key was pressed that no one mentioned. It's the kbhit() from conio.h library. As far as I kow it returns the int value of the charachter you typed in and 0 if you didn't.
Here is a basic example of using this function:

#include <conio.h>
#include <iostream>

using namespace std;

int main(void){
cout << "Please press any key..." << endl;//printing out the message
while(!kbhit());//waiting for a key to be pressed
return 0;
}

Basically what this code does it's making an infinite loop that stops only when a key is pressed.

kbhit() is from the <conio.h> header which is also non-standard and therefore not portable.

So how would i use the kbhit() so that once the user pressed any key it would loop again.
for example...

the first comment is where the loop will go to.

the other comments is where the code requires a key to be pressed to go back to the first comment. what i need to know is how exactly to do this...

void fightg (){
      
             goblin = 250;

            // it will loop to here if any key is pressed
              system ("cls");
                  
                    if (health <= 0){
                    cout << "You have died." << endl; 
                    cout << "Returning to Main menu." << endl; 
                    mainmenu();
                    }
                    else {
                         do 
                         cout << "Your health is: " << health << endl << endl;
                         cout << "The Goblin's heath is: " << goblin << endl << endl << endl;
                         cout << "Do you want to: " << endl;
                         cout << "1. Fight." << endl;
                         cout << "2. Heal." << endl;
                         cout << "3. Go to Main menu." << endl;
                         cin >> choice;
                           if (choice == 1){
                              system ("cls");
                               cout <<  "You hit your enemy for: " <<  endl;
                               
                               cout <<  "Your enemy hit you for:" <<  endl;
                            
                               cout << "To continue fighing press any key." << endl;
                               while(!kbhit());
                               // loops back to first comment
                                 }
                           else if (choice == 2){
                                   system ("cls") ;
                                   cout << "You have chosen to heal." << endl;
                                 
                                   
                                    cout <<  "Your enemy hit you for:" <<  endl;
                                  
                                   cout << "Your health is now: " << health << endl;
                                   cout << endl << endl;
                                   cout << "To continue fighing press any key." << endl;
                                   while(!kbhit()); // loops back to first comment
                                  fighting menu.
                                }
                           else if (choice == 3) mainmenu ();
                           else {
                                cout << "That is not a choice. please try again." << endl;
                              while(!kbhit()); // loops back to the first comment
                                
                                }
                             
                                 }
               
               }

kbhit() is from the <conio.h> header which is also non-standard and therefore not portable.

Well in this case one could use getchar() function from the <iostream> header(again, as long it's a clean input). It works just the same as the cin.get() method.

PS: I was just saying that kbhit() is another way of solving this problem, there is no need of denying it. Maybe it will be a good solution for someone.

but how would i use getchar() to loop back up to previous code?
like in my above post i have the example of what im talking about but how would i make it loop?

Well if your goal was to make an RPG text game then, youre' doing it wrong. First of all I think you should split your code in a few different functions (divide et impera). Also I think you sould consider using switch instead of multiple if statements.
Something like this:

void  fightMenu(void) {
char choise;

 cout << "Your health is: " << health << endl << endl;
 cout << "The Goblin's heath is: " << goblin << endl << endl << endl;
 cout << "Do you want to: " << endl;
 cout << "1. Fight." << endl;
 cout << "2. Heal." << endl;
 cout << "3. Go to Main menu." << endl;
 cin >> choise;

switch(choise) {
case '1': fight();break;
case '2': heal();break;
case '3': mainMenu();break;
}
}

while (!kbhit()); will only pause the execution process and wait until you press some key, after that the prorgam will continue running, it won't loop you back anywhere. If you need to loop back to a previous line of code you should do a while loop like this:

char option = 'y';
do{
cout << "do something" << endl;//here the program does something
cout << endl << "Do you whish to continue?(y/n) ";
cin >> option;
}while (option != 'y');

this way the code between "do{" and "}while()" will be executed over and over again until you respond "y" to the question.

PS: If you want to do it smart you should do a player class which would have (for example) health and manna variables and methods to change those variables like injure(int) or something like this.

well i see what your saying so now i have this:

do {
                         cout << "Your health is: " << health << endl << endl;
                         cout << "The Goblin's heath is: " << goblin << endl << endl << endl;
                         cout << "Do you want to: " << endl;
                         cout << "1. Fight." << endl;
                         cout << "2. Heal." << endl;
                         cout << "3. Go to Main menu." << endl;
                         cin >> choice;
                           if (choice == 1){
                              
                              system ("cls");
                               cout <<  "You hit your enemy for: " << endl << endl << endl;
                             
                               cout <<  "Your enemy hit you for:" <<  endl << endl << endl;
                             
                             
                                do {
                                   cout << "To continue fighing type y and press enter." << endl;
                                   
                                   cin >> choice;
                                   }
                                   while (choice != 'y');
                                   // press any key code to go back to the main fighting menu.
                               
                           
                           
                               }
                               
                           else if (choice == 2){
                                   system ("cls") ;
                                   cout << "You have chosen to heal." << endl;
                                   
                                   
                                    cout <<  "Your enemy hit you for:" <<  endl;
                                   
                                   cout << "Your health is now: " << health << endl;
                                   cout << endl << endl;
                                   do {
                                   cout << "To continue fighing type y and press enter." << endl;
                                   }
                                   cin >> choice;
                                   while (choice != 'y');
                                   // press any key code to go back to the main fighting menu.
                                }
                           else if (choice == 3) mainmenu ();
                           else {
                                cout << "That is not a choice. please try again." << endl;
                               
                                }
                            } while (goblin > 0);
                                 }

but now my problem is when i run this with the rest of my code, get to this spot when i press enter all it does is repeat it instantly over and over without me being able to do anything.

OK, the first thing you're doing wrong is that you're using the same variable name "choise" as a char and as an int simultaneously, you should have different variables for the 'y'(yes answer) and the option of the fighting menu (1 2 3). That is why I told you to split the function in to other smaller functions, this way it will be easier for you to track down your errors.
Next thing, look at the lines 40 and 41 you're reading the answer outside the do...while loop statement. And you're still not using switch() statements.
Try fixing all that and see how it works.

It would be better if you would read some books first before making this kind of programs. I recommend "Teach yourself C++ in 21 days" by Jesse Liberty, It's a very good book for a quick start.

is there anything online that i can use? like online books or tutorials? anyway:


i managed to fix it so that it runs smoothly but how do i make it so that when someone types something in wrong, say they type.. yt because it was close to the Y how would i stop if from freaking out? because if i do that it just puts the same stuff on the screen over and over and over.

also is there a way that i can make it so they just have to press any button and it will continue? instead of press y, so that this would keep it from having a wrong letter typed in.
anyway here's the new code, i will use switch statements in a little bit once i can fix the code i want.

void fightg (){
      
             goblin = 250;

              // do loop here, until the monsters health is <= 0
              system ("cls");
                  
                    if (health <= 0){
                    cout << "You have died." << endl; 
                    cout << "Returning to Main menu." << endl; 
                    mainmenu();
                    }
                    else {
                         do {
                         system ("cls");
                         cout << "Your health is: " << health << endl << endl;
                         cout << "The Goblin's heath is: " << goblin << endl << endl << endl;
                         cout << "Do you want to: " << endl;
                         cout << "1. Fight." << endl;
                         cout << "2. Heal." << endl;
                         cout << "3. Go to Main menu." << endl;
                         cin >> choice;
                           if (choice == 1){
                              
                              system ("cls");
                               cout <<  "You hit your enemy for: " << endl << endl << endl;
                              
                               cout <<  "Your enemy hit you for:" <<  endl << endl << endl;
                              
                               do{
                               
                               cout << "To continue fighing press y." << endl;
                               cin >> bob;
                             } while (bob == "y");
                               // press any key code to go back to the main fighting menu.
                               }
                               
                           else if (choice == 2){
                                   system ("cls") ;
                                   cout << "You have chosen to heal." << endl << endl;
                                   
                                    cout <<  "Your enemy hit you for:" <<  endl << endl;
                                   
                                   cout << "Your health is now: " << health << endl << endl;
                                   cout << endl << endl;
                                   do{
                               
                               cout << "To continue fighing press y." << endl;
                               cin >> bob;
                             } while (bob == "y");
                                   
fighting menu.
                                }
                           else if (choice == 3) mainmenu ();
                           else {
                                cout << "That is not a choice. please try again." << endl;
                                // will go back to the start of the loop again.
                                }
                            } while (goblin > 0);
                                 }
               
               

}

There are tons of free to download e-books and video tutorials over the Internet, on resources like torrent trackers or warez file servers, unfortunately I can't mention any of them on this forum ... you know - "The Rules". I strongly recommend that you buy a real book, made out of paper, it's way easier to read than a e-book.

You can make a multiple condition inside the while statement to check the user's input.

opt = "";
do{
if ((opt == 'y')||(opt == 'Y')||(opt == "yes")||(opt == "Yes")){//if "Yes" it
cout << "do something" << endl;//does something
} else if ((opt == 'n')||(opt == 'N')||(opt == "no")||(opt == "No"){//if "No" it
break;//breaks out of the while loop
} else {//if none of the previous
cout << "you typed a wrong answer" << endl;
}
cout << "Do you wish to continue?(y/n) ";
cin >> opt;
}while(true);//infinite loop

If you would check if any key was pressed, than how would you know if the user wants to repeat the previous action or not?

you can use the function getch();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.