cin without pausing the rest of the code

Is there a way to continue the code while it waits for an input so if they type while the code is executing it will still work
If you look here
1
2
3
4
5
6
7
8
9
cout << "I am waiting on you";
cout << "I am waiting on you";
cout << "I am waiting on you";
cout << "I am waiting on you";
cin >> a;
cout << "I am waiting on you";
cout << "I am waiting on you";
cout << "I am waiting on you";
cout << "I am waiting on you";

for it to continue the "I am waiting on you" you have to enter a value, is there a way where that doesn't have to happen like it will just continue and if you type while it is outputting it will still recieve it?

Side Question: Where do you guys reccomend to start with making actual applications, i can make a console application and i've got some stuff down but when i open the Win32 Application template in VS it is all greek to me on where i put what, where would i find good tutorials on starting?
In other words, it will continue to print out "I am waiting on you" until a key is pressed?
Yes, of a sort
Last edited on
Try this mini-program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
	string message;
	while(!kbhit()) cout << "I am waiting on you " << endl;
	cin >> message;

	return 0;
}
Last edited on
I've tried getline(cin, variable); in the past and it gives me this error (and still does)
Error C3861 'getline': identifier not found ConsoleApplication3

nvm i just tried that and that did the trick! Yet it spammed the text... a lot... very fast lol, could i put a sleep or something? make it change, cause i don't mean for it to be spamming something, i want it used within a line of sentences eg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
	cout << "Hello, My name is Kilroy\n";
	cout << "I am a fake thing, all just written in cpp\n";
	cout << "If at any time you want me to stop just type STOPKILROY\n";
	//Now have the cin >> without stopping his dialog though!
	cout << "I was a young snippet of cout and cin when i was created";
	//more fake dialog here maybe?



	if (makehimstop == "STOPKILROY")
	cout << "Exsanity is a crazy peron!";
	system("TASKKILL /F /T cmd.exe");
	else

}

Something like that, yes i know there is a lot more errors i made this just real quick as an example but i do have the problem with it not detecting full words on IF statements it just goes straight to the else even if i enter the word i'm suppose to help with that as well would be great
Last edited on
Could it be you are using an old C++ compiler? My solution updated. See above.
I figured out the getline thing it was just something with my code i guess, but the kbhit outputs an error saying it is the wrong version or something and i just have to do _kbhit instead
I do still need help with this any help is appreciated
This was from a simple dungeon game i created.
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <iostream>
#include <conio.h> //used for input
#include <cstdlib> //for clear screen
#include <ctime>
// #include <utility> //idk why this is here
#include <set>
#include <vector>

using namespace std;
 
 #include "Game.h"
 #include "Player.h"
 
//Functions
void input (Player &,bool&); // player x & y, play game boolean, player lastX & lastY
void draw  (Player &,vector<int> ,vector<int> ); //player x & y, enemies x & y
void logic (Player& ,bool& ,vector<int> & ,vector<int> & );
void move_baddies(Player &,vector<int>&,vector<int>&);//Enemy x and y than player x and y
void make_baddies(Player &,vector<int>&,vector<int>&);//Enemy x and y than player x and y
void gameOver();

// void make_enemies(set < pair <int,int> > &);

//global constants
const int MAXX=10;
const int MAXY=10;

int main() {
	srand((unsigned)time(0)); 
	Game games;
	Player p;
	bool play = true;
	char pause = ' ';
	// int x = 1;
	// int y = 1;
	// int lastX = 0;
	// int lastY = 0;
	// int health = 100;
	bool eMove = true;
		
	cout << endl;
	cout << "Press . to end the game!\n";
	cout << "Use [W A S D] to move around the map!\n\n";
	cout << "Press any key to continue...";
	pause = _getch();
	
	if(pause == '.') play = false;
	cout << endl << endl;
	
	if(play)
	{
		// make_enemies(games.coords);
		make_baddies(p,games.Ex,games.Ey);
		draw(p,games.Ex,games.Ey);
	}
	
	while(play)
	{
		input(p,play);
		if(!play) break;
		if(eMove) move_baddies(p,games.Ex,games.Ey);  
		logic(p,play,games.Ex,games.Ey);
		if(eMove) eMove = false;	//Used to skip 1 turn for the move enemies
		else eMove = true;			
		draw(p,games.Ex,games.Ey);
		cout << "location: " << p.get_X() << " " << p.get_Y() << endl;
		cout << "Press . to end the game!\n";
		cout << "Health: " << p.get_health() << endl;
	}
	
	// cout << "press enter to exit\n";
	// cin.get();

	gameOver();
	return 0;
}

void move_baddies(Player &p,vector<int>& Ex,vector<int>& Ey) {
	int which = 0;
	int dir=0;
	int last = 0;
	
	for(int i = 0; i < Ex.size();i++) 
	{
		which = (rand()%2)+1;
		dir = (rand()%2)+1;
		
		if(which == 1)
		{	
			if(dir == 1)
			{
				// cout << Ex[i] << endl;
				Ex[i]++;
				// cout << Ex[i] << endl;
				for(int j=0;j<Ex.size();j++) 
				{
					if(i != j && Ex[i] == Ex[j] && Ey[i] == Ey[j]) 
					{
						Ex[i]--;
						break;
					}
				}
				if(Ex[i] > MAXX) Ex[i]--;
			}
			else if(dir == 2) 
			{
				Ex[i]--;
				for(int j=0;j<Ex.size();j++) 
				{
					if(i != j && Ex[i] == Ex[j] && Ey[i] == Ey[j]) 
					{
						Ex[i]++;
						break;
					}
				}
				if(Ex[i] < 1) Ex[i]++;
			} 
		}
		else if (which == 2)
		{
			if(dir == 1)
			{
				Ey[i]++;
				for(int j=0;j<Ey.size();j++) 
				{
					if(i != j && Ey[i] == Ey[j] && Ex[i] == Ex[j]) 
					{
						Ey[i]--;
						break;
					}
				}
				if(Ey[i] > MAXY) Ey[i]--;
			}
			else if(dir == 2) 
			{
				Ey[i]--;
				for(int j=0;j<Ey.size();j++) 
				{
					if(i != j && Ey[i] == Ey[j] && Ex[i] == Ex[j]) 
					{
						Ey[i]++;
						break;
					}
				}
				if(Ey[i] < 1) Ey[i]++;
			} 
		}
	}
}

void gameOver() {
	cout << "GAME OVER!!!" << endl;
}

void logic(Player& p,bool& play,vector<int> & Ex,vector<int> & Ey){
	for(int i=0;i<Ex.size();i++) 
	{
		// if(p.get_X() == Ex[i] && p.get_Y() == Ey[i]) //resets it back to the previous location
		// {
			// p.set_X(p.get_LastX());
			// p.set_Y(p.get_LastY());
			// p.set_health(p.get_health()-25);
		// }
		if(p.get_X() == Ex[i] && p.get_Y() == Ey[i]) //Resets back to original spawn location
		{
			p.set_X(1);
			p.set_Y(1);
			p.set_health(p.get_health()-25);
		}
	}
	if(p.get_health()<=0) 
	{
		play = false;
	}
}

void make_baddies(Player& p,vector<int>& Ex,vector<int>& Ey) {
	int a=0;
	int b=0;
	int num = 5;
	bool make = true;
	
	for(int i=0; i<num ;i++)
	{
		make = true;
		a = (rand()%10)+1;
		b = (rand()%10)+1;
		if(a == p.get_X() && b == p.get_Y()) //make sure that they do not spawn on the player
		{
			make = false;
			num++;
		}
		for(int j = 0; j < Ex.size(); j++ )//prevent spawning on each other
		{
			if(a == Ex[j] && b == Ey[j])
			{
				make = false;
				num++;
			}
		}
		if(make == true)
		{
			Ex.push_back(a);
			Ey.push_back(b);
		}
	}
}

//Draw the board
void draw (Player &p,vector<int> Ex,vector<int> Ey) {
	system("cls"); ///////////////CLEAR SCREEN
	bool print = true;
	//Top of the board
	for(int i=1;i<=MAXX+2;i++)
	{
		cout << "#";
	}
	cout << endl;
	
	// int count = 0;
	//Middle of the board (hard part)
	for(int i=1;i<=MAXX;i++)
	{
		// count = 0;
		cout << "#";
		
		
		for(int j = 1; j <= MAXY; j++)
		{
			if(i == p.get_X() && j == p.get_Y())
			{
				cout << "P";
				// count++;
			}
			else 
			{
				print = true;
				for(int k = 0; k < Ex.size(); k++)
				{
					if(i == Ex[k] && j == Ey[k])
					{
						print = false;
						cout << "E";
						// count++;
					}
				}
				if(print)
				{
					cout << " ";
					// count++;
				}
			}
		}
		cout << "#";
		// cout << "count = " << count;
		cout << endl;
	}
	//Bottom of the board
	for(int i=1;i<=MAXX+2;i++)
	{
		cout << "#";
	}
	cout << endl;
}

//Get computer input (WASD)
void input (Player& p,bool& play) {
	bool again = false;
	char ch=' ';
	
	while (again == false) // used to not take an input other than [WASD or .]
	{
		ch = _getch();
		if(ch == '.'){ play = false; break;}
		p.set_LastX(p.get_X());
		p.set_LastY(p.get_Y());
		// lastX = x;
		// lastY = y;
		switch(ch){
			case 'w':{
				// cout << "UP" << endl;
				if(p.get_X() <= 1) break; //used to keep the player in bounds
				p.set_X(p.get_X()-1);
				// x--;
				again = true;
				break;
			}
			case 's':{
				// cout << "DOWN" << endl;
				if(p.get_X() >= MAXX) break;
				p.set_X(p.get_X()+1);
				// x++;
				again = true;
				break;
			}
			case 'a':{
				// cout << "LEFT" << endl;
				if(p.get_Y() <= 1) break;
				p.set_Y(p.get_Y()-1);
				// y--;
				again = true;
				break;
			}
			case 'd':{
				// cout << "RIGHT" << endl;
				if(p.get_Y() >= MAXY) break;
				p.set_Y(p.get_Y()+1);
				// y++;
				again = true;
				break;
			}
			default:{
				ch = ' ';
			}
		}
	}
	// cout << "location: " << x << " " << y << endl;
}

Player.h
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
#ifndef PLAYER_H
#define PLAYER_H


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;


class Player
{
	public:
		Player();
		~Player();
		//Set
		void set_Y(int);
		void set_X(int);
		void set_Coord(int,int);
		void set_LastX(int);
		void set_LastY(int);
		void set_Last_Coords(int,int);
		void set_health(int);
		//Get
		int get_Y();
		int get_X();
		int get_LastX();
		int get_LastY();
		int get_health();
		
	private:
		int x;
		int y;
		int lastX;
		int lastY;
		int health;
};

#endif[code]
Game.h
[code]
struct Game
{
	// set < pair <int,int> > coords;
	vector < int > Ex;
	vector < int > Ey;
};








Last edited on
So what from that would i take to get what i want to do? i still haven't found a solution and now someone has pointed me to creating a whole new thread
Topic archived. No new replies allowed.