How do I get some input without the user having to press enter?
(I read the FAQ on that, but I didn't quite understand it).
Printable View
How do I get some input without the user having to press enter?
(I read the FAQ on that, but I didn't quite understand it).
what are the inputs?? int? char? or string?
What don't you understand?Quote:
Originally posted by Ranedhel
How do I get some input without the user having to press enter?
(I read the FAQ on that, but I didn't quite understand it).
The input is a 'int'.
assUming you don't have a problem using <conio.h> this answer should suffice. I don't know what you didn't "quite understand" on the faq page, but perhaps you can tell us that and we can explain it to you.Code:http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385
A compiler specific implementation using getch()
#include <stdio.h> /* for printf() */
#include <conio.h> /* for getch() */
int main(void)
{
printf("Press a key to continue...");
getch();
return 0;
}
If you don't mind using <windows.h> you can use oen of these functions (from a Console class I wrote once upon a time). The first one just returns true if a key has been pressed. The second one waits for a keypress and sets 'ch' to its value. The third also waits for a key to be pressed but returns the value (requiring no arguments to the function).Code:bool Console::keyHit() {
HANDLE stdIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD saveMode;
GetConsoleMode(stdIn, &saveMode);
SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
bool ret = false;
if (WaitForSingleObject(stdIn, 1) == WAIT_OBJECT_0)
ret = true;
SetConsoleMode(stdIn, saveMode);
return ret;
}
bool Console::getChar(TCHAR &ch) {
bool ret = false;
if (!isValid())
return ret;
HANDLE stdIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD saveMode;
GetConsoleMode(stdIn, &saveMode);
SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
if (WaitForSingleObject(stdIn, INFINITE) == WAIT_OBJECT_0) {
DWORD num;
ReadConsole(stdIn, &ch, 1, &num, NULL);
if (num == 1)
ret = true;
}
SetConsoleMode(stdIn, saveMode);
return ret;
}
TCHAR getChar() { TCHAR ch = 0; getChar(ch); return ch; }
Hey admins
Perhaps these solutions could reside as alternate solutions on that faq page... Just a thought.
I sorta like to understand every bit of what I put into my game, so I'd have to learn about windows programming before I can use your code(but still, thanks!).
Here is what I don't understand about the example in the FAQ:
while ((ch = getch()) != EOF && ch != '0')
How does this work, and more importantly; can I make more than one possible input(besides 0).
Nevermind, I understand how it works(duh!).
Here is my code, could you show me how to edit it?
(I know there are alot of unnecessary lines, but I haven't got around to refining it yet.)
Code:#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int quit;
int input;
int x = 1;
int y = 1;
int recentx;
int recenty;
int main()
{
while (quit == 0)
{
char map[5][5];
if (x < 1)
{
x = x + 1;
}
if (x > 5)
{
x = x - 1;
}
if (y < 1)
{
y = y + 1;
}
if (y > 5)
{
y = y - 1;
}
if (map[x][y] == (char)35)
{
x = recentx;
y = recenty;
}
map[1][1] = (char)46;
map[1][2] = (char)46;
map[1][3] = (char)46;
map[1][4] = (char)46;
map[1][5] = (char)46;
map[1][6] = (char)46;
map[1][7] = (char)46;
map[1][8] = (char)46;
map[1][9] = (char)46;
map[1][10] = (char)46;
map[2][1] = (char)46;
map[2][2] = (char)46;
map[2][3] = (char)46;
map[2][4] = (char)46;
map[2][5] = (char)46;
map[2][6] = (char)46;
map[2][7] = (char)46;
map[2][8] = (char)46;
map[2][9] = (char)46;
map[2][10] = (char)46;
map[3][1] = (char)46;
map[3][2] = (char)46;
map[3][3] = (char)46;
map[3][4] = (char)46;
map[3][5] = (char)46;
map[3][6] = (char)46;
map[3][7] = (char)46;
map[3][8] = (char)46;
map[3][9] = (char)46;
map[3][10] = (char)46;
map[4][1] = (char)46;
map[4][2] = (char)46;
map[4][3] = (char)46;
map[4][4] = (char)46;
map[4][5] = (char)46;
map[4][6] = (char)46;
map[4][7] = (char)46;
map[4][8] = (char)46;
map[4][9] = (char)46;
map[4][10] = (char)46;
map[5][1] = (char)46;
map[5][2] = (char)46;
map[5][3] = (char)46;
map[5][4] = (char)46;
map[5][5] = (char)46;
map[5][6] = (char)46;
map[5][7] = (char)46;
map[5][8] = (char)46;
map[5][9] = (char)46;
map[5][10] = (char)46;
map[6][1] = (char)46;
map[6][2] = (char)46;
map[6][3] = (char)46;
map[6][4] = (char)46;
map[6][5] = (char)46;
map[6][6] = (char)46;
map[6][7] = (char)46;
map[6][8] = (char)46;
map[6][9] = (char)46;
map[6][10] = (char)46;
map[7][1] = (char)46;
map[7][2] = (char)46;
map[7][3] = (char)46;
map[7][4] = (char)46;
map[7][5] = (char)46;
map[7][6] = (char)46;
map[7][7] = (char)46;
map[7][8] = (char)46;
map[7][9] = (char)46;
map[7][10] = (char)46;
map[8][1] = (char)46;
map[8][2] = (char)46;
map[8][3] = (char)46;
map[8][4] = (char)46;
map[8][5] = (char)46;
map[8][6] = (char)46;
map[8][7] = (char)46;
map[8][8] = (char)46;
map[8][9] = (char)46;
map[8][10] = (char)46;
map[9][1] = (char)46;
map[9][2] = (char)46;
map[9][3] = (char)46;
map[9][4] = (char)46;
map[9][5] = (char)46;
map[9][6] = (char)46;
map[9][7] = (char)46;
map[9][8] = (char)46;
map[9][9] = (char)46;
map[9][10] = (char)46;
map[10][1] = (char)46;
map[10][2] = (char)46;
map[10][3] = (char)46;
map[10][4] = (char)46;
map[10][5] = (char)46;
map[10][6] = (char)46;
map[10][7] = (char)46;
map[10][8] = (char)46;
map[10][9] = (char)46;
map[10][10] = (char)46;
map[x][y] = (char)64;
system("cls");
cout << map[1][1] << map[1][2] << map[1][3] << map[1][4] << map[1][5] << map[1][6] << map[1][7] << map[1][8] << map[1][9] << map[1][10] << endl;
cout << map[2][1] << map[2][2] << map[2][3] << map[2][4] << map[2][5] << map[2][6] << map[2][7] << map[2][8] << map[2][9] << map[2][10] << endl;
cout << map[3][1] << map[3][2] << map[3][3] << map[3][4] << map[3][5] << map[3][6] << map[3][7] << map[3][8] << map[3][9] << map[3][10] << endl;
cout << map[4][1] << map[4][2] << map[4][3] << map[4][4] << map[4][5] << map[4][6] << map[4][7] << map[4][8] << map[4][9] << map[4][10] << endl;
cout << map[5][1] << map[5][2] << map[5][3] << map[5][4] << map[5][5] << map[5][6] << map[5][7] << map[5][8] << map[5][9] << map[5][10] << endl;
cout << map[6][1] << map[6][2] << map[6][3] << map[6][4] << map[6][5] << map[6][6] << map[6][7] << map[6][8] << map[6][9] << map[6][10] << endl;
cout << map[7][1] << map[7][2] << map[7][3] << map[7][4] << map[7][5] << map[7][6] << map[7][7] << map[7][8] << map[7][9] << map[7][10] << endl;
cout << map[8][1] << map[8][2] << map[8][3] << map[8][4] << map[8][5] << map[8][6] << map[8][7] << map[8][8] << map[8][9] << map[8][10] << endl;
cout << map[9][1] << map[9][2] << map[9][3] << map[9][4] << map[9][5] << map[9][6] << map[9][7] << map[9][8] << map[9][9] << map[9][10] << endl;
cout << map[10][1] << map[10][2] << map[10][3] << map[10][4] << map[10][5] << map[10][6] << map[10][7] << map[10][8] << map[10][9] << map[10][10] << endl;
cin >> input;
switch (input)
{
case 0:
quit = 1;
break;
case 2:
recenty = y;
recentx = x;
x = x + 1;
break;
case 4:
recenty = y;
recentx = x;
y = y - 1;
break;
case 6:
recenty = y;
recentx = x;
y = y + 1;
break;
case 8:
recenty = y;
recentx = x;
x = x - 1;
break;
default:
quit = 0;
break;
}
}
exit(0);
}
Dude: code tags please :)
>>Hey admins
Perhaps these solutions could reside as alternate solutions on that faq page... Just a thought.<<
Done.
Ranedhel, you should really consider learning file i/o in C++. Your code is horrible.
Learn about using loops:
Code:
for (int x=0;x<arrayLenX;x++)
for (int y=0;y<arrayLenY;y++)
array[x][y]=theValueIWant;
If you would have read what I said, I haven't gotten around to refining it yet. :p
Sorry 'bout the code tags though.
Not that I'm trying to ream you or anything, but lets call a spade a spade. No one would set every single element manually in a two-dimensional array because they were going to "refine it later." That's like writing a novel with a pencil because you wanted to type it later or perhaps blowing into a tire because you want to use a pump later. Just be honest and say that you didn't know you could do it with a nested loop... It's okay, really. No one will poke fun at you. They may even respect you more for being able to admit it.. You never know.Quote:
Originally posted by Ranedhel
If you would have read what I said, I haven't gotten around to refining it yet. :p
LoL, actually I did know I could do it with a loop, but I wasn't sure how(still not, when I try to display it it does every thing one dimensional).
BTW: Your similes aren't entirely accurate.;)Code:for (int a = 0; a <= 10; a++)
{
for (int b = 0; b <= 10; b++)
{
cout << array[a][b] << endl;
}
}
Well okay next time I say something ridiculous you come up with something better. I thought they worked just fine... What's inaccurate about them?Quote:
Originally posted by Ranedhel
BTW: Your similes aren't entirely accurate.;)