Helpc++ console program (simple but awkward)

Posts 1–13 of 13 · Page 1 of 1
c++ console program (simple but awkward)
Hi all,i'm still a newbie (still working with console...) so please keep it as simple as possible :3

So,i've been trying to do a (seamlessy) simple program,but neither I neither my teacher have a clue on how to do that.
The purpose of the program is: taking a laugh/useless program.
What it should do: it asks you any character,then proceeds to spam it using a cycle. However,if you press any key during the cycle,the spammed character will be swapped to the key pressed.
-IMPORTANT-
It has to don't stop the cycle running.

-hint-
kbhit() does a similiar feature,however,it just captures if you've pressed or not a key during the cycle,while i need it to set the key to a variable.

This is the starting code:
Code:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main ()
{
int y=0;
char x;
cout<<"gimmie character to spam";
cin>>x;
while (y==0)
{
     cout<<x<<" ";
}
system("pause");
return 0;
}
-hint2-
using getc/getch/getchar () will result the cycle to stop.

-curiosity-
this could be used to actually make something useful: a program to do different actions based on keypresses.
An example of this would be a wasd program,which tells where a subject would go through spamming words in the console.


-explanation-
While the code i provided does this:
Code:
x x x x x x x x x x x x x x x x x x x x 
x x x x x x x x x x x x x x x x x x x x 
x x x x x x x x x x x x x x x x x x x x 
...
I'd need it to do something like this:
Code:
x x x x x x x x x x x x x x x x x x x x 
x x x x x a a a a a a a a a a a a a a a 
a a a y y y y y e e e e e e e e e e e e 
...
Hope someone can help me out :3
Code:
int y=0;
char x;
cout<<"gimmie character to spam";
cin>>x;

while (y==0)
{
     cout<<x<<" ";

         // IF keypress
        {
         // x = pressed key; then continue as normal.
        }
}

GetAsyncKeyState() to check if a key is pressed, and set your char variable accordingly. ?

 
width

Also, if you want the output to only be a ## of characters wide you can use modulus and print a newline.

Code:
int _columnNumber = 0;
..
while (y==0)
{

  if (_columnNumber == 20)
  {
  cout << "\n"; //newline after every 20 characters
  _columnNumber = 1;
  }

cout << x;
_columnNumber +=1;
}
Quote Originally Posted by abuckau907 View Post
Code:
int y=0;
char x;
cout<<"gimmie character to spam";
cin>>x;

while (y==0)
{
     cout<<x<<" ";

         // IF keypress
        {
         // x = pressed key; then continue as normal.
        }
}

GetAsyncKeyState() to check if a key is pressed, and set your char variable accordingly. ?
That's it,but then how would the code become? ("converting" the comments to code)
Googling around i don't really seem to understand the GetAsyncKeyState function too well (GetAsyncKeyState function (Windows) the fuq is this?!?!?).

So an example would be great <3

*edit*
would it become this?
Code:
int y=0;
char x;
cout<<"gimmie character to spam";
cin>>x;

while (y==0)
{
     cout<<x<<" ";

         if (!kbhit())
        {
         x=GetAsyncKeyState()
        }
}
That's not it...i've been checking all sections of the possible functions: Keyboard Input Functions (Windows)
But yet all i've obtained is my mind full o' fucks...
Quote Originally Posted by ERGaergaergaergaergearg View Post
Here's a good thread of the use of GetAsyncKeyState()

http://www.mpgh.net/forum/31-c-c-pro...ckeystate.html
Then this means GetAsyncKeyState() Isn't useful at all for the program i'd need to make as it's just a bool variable telling wether a specified key has been pressed or not.

Instead,i'd need a function that would read a key of the keyboard,which can be any at any time,without pausing the cycle.

Awkward <.<
Fixed doing this:
Code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <Windows.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char x;
	cout<<"inserisci x: ";  //optional.
	cin>>x;
	while (1)
	{
		cout<<x;
		if(kbhit()) //if a key is pressed,then check which one
		{
		if(GetAsyncKeyState(0x30))
		{x='0';}
		else if (GetAsyncKeyState(0x31))
			{x='1';}
		else if(GetAsyncKeyState(0x32))
			{x='2';}
		else if (GetAsyncKeyState(0x33))
			{x='3';}
		else if (GetAsyncKeyState(0x34))
			{x='4';}
		else if (GetAsyncKeyState(0x35))
			{x='5';}
		else if (GetAsyncKeyState(0x36))
			{x='6';}
		else if (GetAsyncKeyState(0x37))
			{x='7';}
		else if (GetAsyncKeyState(0x38))
			{x='8';}
		else if (GetAsyncKeyState(0x39))
			{x='9';}
		else if (GetAsyncKeyState(0x41))
			{x='a';}
		else if (GetAsyncKeyState(0x42))
			{x='b';}
		else if (GetAsyncKeyState(0x43))
			{x='c';}
		else if (GetAsyncKeyState(0x44))
			{x='d';}
		else if (GetAsyncKeyState(0x45))
			{x='e';}
		else if (GetAsyncKeyState(0x46))
			{x='f';}
		else if (GetAsyncKeyState(0x47))
			{x='g';}
		else if (GetAsyncKeyState(0x48))
			{x='h';}
		else if (GetAsyncKeyState(0x49))
			{x='i';}
		else if (GetAsyncKeyState(0x4a))
			{x='j';}
		else if (GetAsyncKeyState(0x4b))
			{x='k';}
		else if (GetAsyncKeyState(0x4c))
			{x='l';}
		else if (GetAsyncKeyState(0x4d))
			{x='m';}
		else if (GetAsyncKeyState(0x4e))
			{x='n';}
		else if (GetAsyncKeyState(0x4f))
			{x='o';}
		else if (GetAsyncKeyState(0x50))
			{x='p';}
		else if (GetAsyncKeyState(0x51))
			{x='q';}
		else if (GetAsyncKeyState(0x52))
			{x='r';}
		else if (GetAsyncKeyState(0x53))
			{x='s';}
		else if (GetAsyncKeyState(0x54))
			{x='t';}
		else if (GetAsyncKeyState(0x55))
			{x='u';}
		else if (GetAsyncKeyState(0x56))
			{x='v';}
		else if (GetAsyncKeyState(0x57))
			{x='w';}
		else if (GetAsyncKeyState(0x58))
			{x='x';}
		else if (GetAsyncKeyState(0x59))
			{x='y';}
		else if (GetAsyncKeyState(0x5a))
			{x='z';}
		}
	}
	system("pause");
	return 0;
}
Quote Originally Posted by IV2B View Post
Fixed doing this:
Please, look at the code you just wrote. Look at it and ask yourself, "is there a pattern?", because you'll notice that:

-The value you are using to check the key state of is counting upward
-The value you are setting x to is counting upward

At the very least you can note that they parallel each other in some way, and perhaps offset it by a bit and set x to that value; however, if you do this test for the offset by comparing the ASCII value of "0" to the number you're checking for, 0x30, you'll find that they're exactly the same. I really hope those hex values weren't found by consulting an ASCII table. Regardless, these 72 lines of code:
Code:
		if(GetAsyncKeyState(0x30))
		{x='0';}
		else if (GetAsyncKeyState(0x31))
			{x='1';}
		else if(GetAsyncKeyState(0x32))
			{x='2';}
		else if (GetAsyncKeyState(0x33))
			{x='3';}
		else if (GetAsyncKeyState(0x34))
			{x='4';}
		else if (GetAsyncKeyState(0x35))
			{x='5';}
		else if (GetAsyncKeyState(0x36))
			{x='6';}
		else if (GetAsyncKeyState(0x37))
			{x='7';}
		else if (GetAsyncKeyState(0x38))
			{x='8';}
		else if (GetAsyncKeyState(0x39))
			{x='9';}
		else if (GetAsyncKeyState(0x41))
			{x='a';}
		else if (GetAsyncKeyState(0x42))
			{x='b';}
		else if (GetAsyncKeyState(0x43))
			{x='c';}
		else if (GetAsyncKeyState(0x44))
			{x='d';}
		else if (GetAsyncKeyState(0x45))
			{x='e';}
		else if (GetAsyncKeyState(0x46))
			{x='f';}
		else if (GetAsyncKeyState(0x47))
			{x='g';}
		else if (GetAsyncKeyState(0x48))
			{x='h';}
		else if (GetAsyncKeyState(0x49))
			{x='i';}
		else if (GetAsyncKeyState(0x4a))
			{x='j';}
		else if (GetAsyncKeyState(0x4b))
			{x='k';}
		else if (GetAsyncKeyState(0x4c))
			{x='l';}
		else if (GetAsyncKeyState(0x4d))
			{x='m';}
		else if (GetAsyncKeyState(0x4e))
			{x='n';}
		else if (GetAsyncKeyState(0x4f))
			{x='o';}
		else if (GetAsyncKeyState(0x50))
			{x='p';}
		else if (GetAsyncKeyState(0x51))
			{x='q';}
		else if (GetAsyncKeyState(0x52))
			{x='r';}
		else if (GetAsyncKeyState(0x53))
			{x='s';}
		else if (GetAsyncKeyState(0x54))
			{x='t';}
		else if (GetAsyncKeyState(0x55))
			{x='u';}
		else if (GetAsyncKeyState(0x56))
			{x='v';}
		else if (GetAsyncKeyState(0x57))
			{x='w';}
		else if (GetAsyncKeyState(0x58))
			{x='x';}
		else if (GetAsyncKeyState(0x59))
			{x='y';}
		else if (GetAsyncKeyState(0x5a))
			{x='z';}
can be shortened to:

Code:
for(unsigned char i=0x5a;i>=0x30;i--) {
    if(GetAsyncKeyState(i)) {
        x=i;
        break;
    }
}
Quote Originally Posted by Shanks_ View Post
can be shortened to:

Code:
for(unsigned char i=0x5a;i>=0x30;i--) {
    if(GetAsyncKeyState(i)) {
        x=i;
        break;
    }
}
It does indeed work,also,i've got those hex values consulting the MSDN.
I didn't do that on my own as i yet have no idea how such thing would work out at all.

I'd be happy if you could enlight me over a few aspects:
1) Why the i--?
2) What should i change the code to if i'd want to use low cap keys instead/along with the others? (with my code,typing a spams "a",while with yours it spams "A")
Quote Originally Posted by IV2B View Post
It does indeed work,also,i've got those hex values consulting the MSDN.
I didn't do that on my own as i yet have no idea how such thing would work out at all.

I'd be happy if you could enlight me over a few aspects:
1) Why the i--?
2) What should i change the code to if i'd want to use low cap keys instead/along with the others? (with my code,typing a spams "a",while with yours it spams "A")
1) I made the code equivalent to yours, in that if two or more keys are pressed, the key with the highest ASCII value "wins" and x gets set to it. Doing i-- was just one of my programming habits- you could always make it count upward instead and get rid of the break line, but that ends up making it run longer than it needs to. If you counted upwards, it has to check every single pressed key and then remember only the last one, so why not take the easy way instead, and count downward, stopping on the very first one you get to?

2) I apologize for not matching up the letters exactly; what you'd need to do in that situation is offset it to the lower case ASCII alphabet, which starts at 0x61 instead of 0x41. Essentially, you only want to add 0x20 to it whenever it's alphabetical, not numeric, so you can write
Code:
x=i>=0x41?i+0x20:i;
or equivalently
Code:
if(i>=0x41) {
    x=i+0x20;
} else {
    x=i;
}
instead of
Code:
x=i;
in the loop.
Quote Originally Posted by Shanks_ View Post
1) I made the code equivalent to yours, in that if two or more keys are pressed, the key with the highest ASCII value "wins" and x gets set to it. Doing i-- was just one of my programming habits- you could always make it count upward instead and get rid of the break line, but that ends up making it run longer than it needs to. If you counted upwards, it has to check every single pressed key and then remember only the last one, so why not take the easy way instead, and count downward, stopping on the very first one you get to?

2) I apologize for not matching up the letters exactly; what you'd need to do in that situation is offset it to the lower case ASCII alphabet, which starts at 0x61 instead of 0x41. Essentially, you only want to add 0x20 to it whenever it's alphabetical, not numeric, so you can write
Code:
x=i>=0x41?i+0x20:i;
or equivalently
Code:
if(i>=0x41) {
    x=i+0x20;
} else {
    x=i;
}
instead of
Code:
x=i;
in the loop.
Thank you a lot for your answer,it helped alot!
Even tho i don't fully understand it in every part...but i've grasped the main parts and for sure it's much more than i should know by now.(As in my classroom we've barely started using do and while lol... xP)
Quote Originally Posted by MSDN
Return Value

Type: SHORT

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.
Quote Originally Posted by MSDN
Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.
Code:
if (GetAsyncKeyState(VK_SLUTEVER) & 0x8000)
Posts 1–13 of 13 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us