Help please. Keystroke Function?

I'm trying to make a function where you have to type press Enter in order for the next display to appear. It looks like this so far.

1
2
3
4
5
6
7
8
void keystroke()
{
    int i;
    while((GetAsyncKeyState(i!=13) == -32767))
        {
            cout<< "Please press enter";

        }


But this isn't working? When you press enter, it's meant to go on. This is like a pause thing. Does anyone know how to fix this, or does anyone have any ideas that I can do that would pause the program one step at a time?

If anyone want me to post the whole code up then just tell me. It's a little long though.

Thank you.
Last edited on
1
2
3
4
5
6
7
8
9
void keystroke()
{
    int i;
    while((GetAsyncKeyState(i!=13) == -32767))
        {
            cout<< "Please press enter";
            cin.get();

        }


That should do it. You need to have a cin statement in order for the user to ever be asked for input.
Thank you, Frieddy! I haven't tried it yet but I bet it works. Thanks bro!
Actually, Freddy. It didn't work. Here's my whole code.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* In this game, there will be a ninja.
He will kill the dark evil Ninja. He will go to
New York, L.A, or San Francisco to search for the evil Ninja.
When he goes to the correct one, he will battle the evil Ninja.
*/

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <Winuser.h>


using namespace std;

void name(); //function for the name of the user
char getName(); // gets the name of the person.
void dialogue(string name, string text); // use for talking
void keystroke(); // any key to continue
int main()
{
    string sPlayAgain;
    string *pPlayAgain;
    pPlayAgain = &sPlayAgain;
    string yes;

        Sleep(1000);
        cout <<"\nWelcome to Ninja!\n" << endl;
        Sleep(2000);






    do{

        do{

        name(); //remember to leave out "void".
        ifstream nameFile("name.txt"); //reads file
        string name;
        nameFile >> name; //using pointer from L23
        cout << "\nYour name is.. " << name << endl;
        Sleep(1500);
        cout << "\n Is that correct..?";
        cout << "\n Type Yes or No\n\n";
        cin >> yes;
        }while(yes != "Yes");

/* ************ Start with the game! ************/
string beginName;
    ifstream getTheName("name.txt");
    getTheName >> beginName;

    Sleep(1500);
    dialogue("Narrator","The city is burning. There are dead corpse everywhere...");

    dialogue(beginName,"Oh my god. I just left to go buy some groceries\n and this is what happened?");

    dialogue("Unknown","...");
    dialogue(beginName, "Who's there?!");
    dialogue("Pirate", beginName + "..cough..cough..");

    dialogue(beginName, "Oh my.. who did this to you? Was it, the dark evil ninja?");


    dialogue("Pirate", "yes..");
    dialogue(beginName, "please.. don't speak.");
    dialogue("Pirate", "I'm sorry...");
    dialogue(beginName, "Why are you sorry for?");
    dialogue("Pirate", "I couldn't stop him..");
    dialogue(beginName, "Don't worry... I'll.. i'll..!!");
    dialogue("Pirate", "*Head drops down*");
    keystroke();
    dialogue(beginName, "Nooooooooooo! PIRATE!");







/* ***********BELOW END GAME SEQUENCE************** */
    cout << "\nWould you like to play again? Please type Yes if you do. \n";
    cin >> sPlayAgain;
    if (sPlayAgain == "No")
    {
        cout <<"\n THANK YOU FOR PLAYING! \n";
    }
    }while(*pPlayAgain == "Yes");
    return 0;
}
 void name()
 {
        Sleep(1000);
        ofstream nameFile("name.txt");
        if(nameFile.is_open())
        {
            cout <<"\nWhat is your name? Press Ctrl+Z to quit\n" << endl;
            string name;
            cin >> name;
            nameFile << name;
            nameFile.close();

        }

}
void dialogue(string name, string text)
{
    string Name;
    Name = name;
    cout << endl << Name << ":\n " << text << endl;
    Sleep(3000);
}

void keystroke()
{
    int i;
    while((GetAsyncKeyState(i!=13) == -32767))
        {
            cout<< "Please press enter";
            cin.get();

        }
}



I tried to use it but it still doesn't work :(. Do you think there's another way?
@hellohellomoon
Here's a routine I use in a lot of my programs. Just call Pause().

1
2
3
4
5
6
7
8
void Pause()
/* Wait for a return key to continue */
{
char YN;
cout << "\n\n\t\t<---  Please press 'Enter' to continue... --->";
while ( (YN = getchar() ) != '\n') ; /* Just wait for Enter key  */
return;
}  /* End of Pause() */

There is no need for dummy variables.
you could also use cin.ignore( numeric_limits<streamsize>::max(), '\n');

GetAsyncKeyState(i!=13) ¿? You are passing a boolean.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx
GetAsyncKeyState(VK_RETURN)

¿What is this for?
1
2
3
    string sPlayAgain;
    string *pPlayAgain;
    pPlayAgain = &sPlayAgain;
What I wrote won't work if you did a simple cin >> statement before it. Just put cin.ignore() before cin.get() and it should be fine. The problem with just having cin.get() is that there may be a '\n' character left in the input stream if you did a cin >> statement, so that '\n' character will be pulled out by the cin.get() function and it won't ask the user for input. At least, that's my understanding of it. Using either cin.ignore() or what ne555 posted before the cin.get() should work.
Thank you whitenite1, ne555, and freddy92. I will try each of your solutions, and if it works, I'll let you guys know. Thanks again :)

Oh, and the code

1
2
3
string sPlayAgain;
STring*pPlayAgain;
pPlayAgain = &sPlayAgain;


really isn't needed. I was just trying to practice pointers and get used to them.

Oh, by the way, what are "dummy variables"? I'm new and I'm just curious :)

Thanks.
@hellohellomoon
Dummy variables are variables that are used pretty much as a place holder. Unlike a variable that is used in a for loop, or as a CONST, etc. Like the char 'YN' variable I used, it doesn't really serve a purpose except to wait for the enter key. For me, it may be just a dummy variable, but it does what I want it to do, so, to me.. it's no dummy... ;)
Oh I see. Thanks whitenite :)
Topic archived. No new replies allowed.