user input and instantly provide a feedback

Beginner here..., i was tasked to make a adventure game and have trouble at this one part.. after reading past most of the forums here, it is said that system() is bad so i refused to use it :D. But i ran into a problem where whenever i wanted to move, by pressing the up down left right key it requires me to press enter also. Is there anyways to have it not require an enter and take my input straight and display it?

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
#include <iostream>
#include <windows.h>
#include "conio_yp.h"

using namespace std;

const int mapRow = 10;
const int mapCol = 10;
char map[mapRow][mapCol];
bool isExit = false;

enum  HERO_CLASS 		// ENUMERATION
{
 	  WARRIOR = 0,
 	  ARCHER,
 	  MAGE
 	  
};

struct Hero				// DECLARING A HERO AS A DATA TYPE
{
 	   HERO_CLASS pClass;
 	   char pChar;
 	   int pPosX, pPosY;
 	   
 	   void ini(HERO_CLASS heroClass)
 	   {
	   		pClass = heroClass;
			   
			if(pClass == WARRIOR)
			{
			 		  pChar = 6;
			 		 
	        }
			
			else if(pClass == ARCHER)
			{
			 	 pChar = 4;
		    }
			
			else if(pClass == MAGE)
			{
			 	 pChar = 3;
	        }   		
       }
};

Hero player;

void Inimap()						// INITIALIZE MAP
{
     char tempMap[mapRow][mapCol] = {
        {'1','1','1','1','1','1','1','1','1','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','0','0','0','0','0','0','0','0','1'},
        {'1','1','1','1','1','1','1','1','1','1'}
                                         };
     
     for(int i=0; i<mapRow; i++)
     {
        for(int j=0; j<mapCol; j++)
        {
            map[i][j] = tempMap[i][j];
        }
     }
}

void ShowMap()						// SHOW MAP
{
     
     for(int i=0; i<mapRow; i++)
     {
        for(int j=0; j<mapCol; j++)
        {	
			
			if(i == player.pPosY && j == player.pPosX)
			{ 
			  	textcolor(CYAN); 
			 	cout<<player.pChar;
				textcolor(WHITE);  
		    }
		    
		    else
		    {
             	cout<<map[i][j];
			}
        }
     cout<<endl;
     }
         
}



void Menu()					// SHOWING OF MENU
{
 	 int mAction = -1;
 	 
 	 cout<<"1. Open Inventory"<<endl;
 	 cout<<"2. Exit"<<endl;
 	 cout<<"Please select your action : ";
 	 
 	 cin>>mAction;
 	 
 	 if(mAction == 1)
 	 {
	  	
		  //NOT YET IMPLEMENT		   
	  	  	   
     }
     
     else if(mAction == 2)
     {
	  	 isExit = true; 
     }
	 cin.get();
}

void mMove()				// MOVEMENT
{
 	 if(GetAsyncKeyState(VK_LEFT))
 	 {
	  	player.pPosX--;						  
     }
     
 	 else if(GetAsyncKeyState(VK_RIGHT))
 	 {
	  	player.pPosX++;						  
     }
 	  	 
 	 else if(GetAsyncKeyState(VK_UP))
 	 {
	  	player.pPosY--;						  
     }
     
	 else if(GetAsyncKeyState(VK_DOWN))
 	 {
	  	player.pPosY++;						  
     }
     
}	  

void pSelectScreen()			//SELECTION SCREEN
{
 	 int mAction = -1;
 	 
 	 cout<<"1. Warrior"<<endl;
 	 cout<<"2. Archer"<<endl;
 	 cout<<"3. Mage"<<endl;
 	 cout<<"Please select your class : ";
 	 
 	 cin>>mAction;
 	 
 	 if(mAction == 1)
 	 {
	  	player.ini(WARRIOR);
  	 	player.pPosX = 1;
		player.pPosY = 1;
		  		   
     }
     
     else if(mAction == 2)
     {
	  	 player.ini(ARCHER);
	  	 player.pPosX = 9;
	  	 player.pPosY = 9;
	  	
     }
     
     else if(mAction == 3)
     {
	  	  player.ini(MAGE);
	  	  player.pPosX = 5;
		  player.pPosY = 5;
	 }
	 cin.get(); 	 
}

void bChecking()
{
     if(player.pPosX > 8 || player.pPosY > 8 || player.pPosX < 1 || player.pPosY < 1)
     {
                     if(player.pPosX > 8)
                     {
                          player.pPosX--;
                     }
                     
                     else if(player.pPosX < 1)
                     {
                          player.pPosX++;
                     }
                     
                     else if(player.pPosY > 8)
                     {
                          player.pPosY--;
                     }
                     
                     else if(player.pPosY < 1)
                     {
                          player.pPosY++;
                     }
                     
     }
    
}

int main()						//MAIN FUNCTION
{
    system("mode con: cols=80 lines=30");
    
    Inimap();
    pSelectScreen();
    		
    do
    {	
	  		 
	   		 cout<<endl;
	   		 mMove();
             bChecking();
	         ShowMap();
	         
			 cout<<endl; 
	  		 //Menu();
	  		 
			 //cout << string(50, '\n');
			 //system("PAUSE");	 
			 //system("CLS");
			 
			 cout << string(50, '\n');
	
	}while(!isExit);
	
    
    
    
    cout<<"Press ENTER to exit..";
    cin.get();
    return 0;
}

// WHAT TO PUT IN MAP
// STRUCT HERO - INCREASE IT WITH 3 - 5 VARIABLES 


SORRY FOR THE SUPER LONG CODE
Last edited on
160:1 C:\Users\User\Desktop\TextAdventureGame\curses.h [Warning] "MOUSE_MOVED" redefined

got this error after including curses.h in the code
I expect it's already defined in windows.h
sorry for noobness.. still dont get how to get it to work... is it because it is conflicting with conio.h?

mmm maybe there's another library for your game
well actually it works well when i have system("PAUSE") working in the do-while loop. Everytime i press up down left or right, it will re-run the loop again and print a new location for the player. Was wondering are there any alternatives that can bring the same effect without using system("PAUSE").
try this

cin.ignore(80, '\n');
If you are using conio.h (sorry,I don't recognise conio_yp.h) then you could use getch() or _getch(). (Check your compiler documentation).

Here on this forum suggestions usually focus on standard C++ or cross-platform solutions. Conio is not standard. Nevertheless, there's nothing to stop you using it.
sorry bout that cause my conio_yp.h was given by my lecturer.
I just wanna try and not use system("PAUSE") although its just a homework. (dont wanna make a bad habit out of it).

still have a question for the codes here though.. how do i read the user input and instantly provide a feedback and having a brief pause to let the player think which direction he wants to go?
bump.. really need help ><
std::cin is buffered. This means that you *must* always hit enter after pressing some keys before your program will actually receive them.

If you want to do unbuffered input then you will have to search out some other way of getting the input.

Something like the curses library might be what you want.
yea would love to learn how to use the curses library... but the problem is that by #include "curses.h".. i will get an error

160:1 C:\Users\User\Desktop\TextAdventureGame\curses.h [Warning] "MOUSE_MOVED" redefined
If you don't mind having to use windows, you can use GetKeyState inside a loop.
i somehow managed to get it to work using getch(). but i dont really understand why and how is it different from cin.get() or cin.ignore.
getch() is different in at least two ways
a) it is not a standard part of C++. (It may not be available if you use a different compiler, or different OS).
b) it does not require the enter key to be pressed.
Thanks for all the help !

- CASE CLOSED -

or

- PROBLEM SOLVED -
Topic archived. No new replies allowed.