c++ how to advance if dont get input

shiftyape

Honorable
Feb 23, 2013
48
0
10,580
0
how to use _getch();
but instead of the program stopping at _getch();
to keep going but if you input a key while the rest of the program is executing, give the input to getch



C++:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <cstring>
#include <Windows.h>
#include <conio.h>
using namespace std;

int ran, z, i = 20, h, it = 5;
string enemy = "<";
string space[20] = {" ", "  ", "   ", "    ", "     ", "      ", "       ", "        ", "         ", "          ", "           ", "            ",
"             ", "              ", "                ", "                ", "                 ", "                  ",
"                   ", "                    "};  
char key = ' ';
string you = ":)";

int main(){

	srand(time(NULL));

	while (1){
		i = 20;
		system("cls");
		ran = rand() % 10;
		z = ran;
		h = ran;
		while (ran > 1) {
			ran--;
			cout << endl;
		}
		while (i > 0){

			key = _getch();

			system("cls");
			z = h;

			while (z > 0){
				z--;
				cout << endl;
			}

			i--;
			cout << space[i] << enemy;

			if (key != ' '){
					if (key == 'w'){
						while (it > 1){
							it--;
							cout << endl;
							z++;
							cout << you;
						}
					}
					else if (key == 's'){
						while (it < 10){
							it++;
							cout << endl;
							z--;
							cout << you;
						}
					}
				}
		}
	}

system("PAUSE");
return 0;
}


i am sorry about the format but i cant fix it... already tried, but it went in without tabs

mod-edit: formatting
 

Pinhedd

Distinguished
Moderator
Step 1: don't use conio, ever. It's an MS-DOS specific library that is obsolete and non-standard. Only use what's available in the C++ standard library stdio.h

Step 2: rework your program to use iostream instead. istream::read provides the functionality that you are looking for
 
Thread starter Similar threads Forum Replies Date
Y Apps General Discussion 1
A Apps General Discussion 3
S Apps General Discussion 2
R Apps General Discussion 3
I Apps General Discussion 2
E Apps General Discussion 1
Z Apps General Discussion 2
R Apps General Discussion 2
Varzoh Apps General Discussion 9
T Apps General Discussion 2
Akalanka Umayanga Apps General Discussion 5
L Apps General Discussion 2
G Apps General Discussion 0
S Apps General Discussion 1
S Apps General Discussion 2
S Apps General Discussion 7
cchartsell Apps General Discussion 7
geis Apps General Discussion 6
David 617 Apps General Discussion 1
G Apps General Discussion 1

ASK THE COMMUNITY