I'm having troubles with a cin statement in my code and need some guidance. I need my code to refresh an array with completely different ASCII characters, then have the user enter a keystroke and have the refresh speed increase by a few hundred miliseconds every time. Now my C++ teacher gave me a code to use (using the windows.h library) but i have no idea how to apply it or if theres an easier way.
that's the code he gave me but i don't know how to apply it to my code. (will post below)
any help would be great! if you have any other questions let me know! this is only the portion of the game that this involves.
void func2()
{
cout << "----------------------------------------------------------------------------" << endl;
cout << "| You have chosen the Moving Menace game. |" << endl;
cout << "| You will be looking for a specific character in this game. |" << endl;
cout << "| When you see the character, enter the key 'G'. |" << endl;
cout << "| Just as a warning the more you find the character, the faster it moves. |" << endl;
cout << "| Are you ready? |" << endl;
cout << "----------------------------------------------------------------------------" << endl;
Sleep(10000);
//It's over 9000!
cout << "3" << endl;
Sleep(1000);
cout << "2" << endl;;
Sleep(1000);
cout << "1" << endl;
Sleep(1000);
system("cls");
cout << "GO!";
Sleep(1000);
system("cls");
// initialize the array
while(1)
{
cout << "Look for this character: '*' \n" << endl;
char cTest;
iSize = 16;
for (int x = 0; x < iSize; x++)
{
for (int y = 0; y < iSize; y++)
{
cTest = char(rand() % 223 + 33);
cArray[x][y] = cTest;
}
}
// display everything in array
for (int x = 0; x < iSize; x++)
{
for (int y = 0; y < iSize; y++)
{
cTest = cArray[x][y];
cout<< cTest;
}
cout << endl;
}
/*my problems start here! well maybe earlier...*/
Sleep(1500);
system("cls");
}
}
That function will place the cursor at the (x, y) you specify, where x = column and y = row and (0, 0) is the upper corner of the screen. This way you can avoid using the (very slow) system("cls");
The Sleep() function takes an unsigned integer argument, so you just need a variable to hold how long to sleep. Each time through the loop you can reduce its value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
unsigned timeout = 1500; // 1.5 seconds
...
while (1)
{
SetCoord( 0, 0 );
// draw stuff here
...
if (timeout)
{
Sleep(timeout);
if (timeout > 200) timeout -= 200;
else timeout = 0;
}
}
BTW, the comment you have on line 23 is not correct...
i know about line 23 i just haven't changed it yet. and this is wonderful! it solves a problem i was going to have to solve later in the code but im still stuck on running 2 processes at the same time :/ like i said i need the array to refresh at the same speed UNTIL the user enters his keystroke then speeds up..if thats even possible..
Once you get that first ENTER (and read it) you can either use the Sleep() function as you have or continue using WaitForSingleObject() so that the user can press a key at any time to stop everything.
hey duoas, I tried your "http://www.cplusplus.com/forum/beginner/5619/#msg25047" code, and nothing really happend. Is it supposed to print "." while it waits for input?
What do you mean, "nothing really happend"?
Did it not print "."s every half second while waiting for you to begin typing?
If not, tell me more about your compiler and OS.
I'm really not sure what the problem is.
Please don't take this the wrong way, but more often than not when a person says something like "I did it exactly as you did" he missed something anyway.
Are you sure you started with a Windows console application and linked with the correct libraries, that you haven't messed with the iostream::sync_with_stdio() or other properties, that you are compiling for the proper platform, that you are using a standard Windows console to run the application (and not, even, running it from the IDE), that you don't have any extraneous semicolons in your code, etc?
I cannot get VS to install on my PC (apparently I'm one of the few for which .NET will not install or work properly), so I cannot test with modern versions.
Maybe someone else here knows what may have gone wrong?
The IDE does have nice features, but it is not uncommon for the "run from the IDE" functions to modify your process in unexpected ways with various hooks...