c++ how to advance if dont get input

shiftyape

Honorable
Feb 23, 2013
48
0
10,580
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
 
Solution
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

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
 
Solution