Results 1 to 13 of 13
  1. #1
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired

    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

  2. #2
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    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. ?

     

    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;
    }
    Last edited by abuckau907; 10-30-2013 at 10:17 AM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  3. #3
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    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()
            }
    }
    Last edited by IV2B; 10-30-2013 at 10:23 AM.

  4. #4
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    idk man sry

  5. #5
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    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...

  6. #6
    ERGaergaergaergaergearg's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Posts
    172
    Reputation
    19
    Thanks
    30
    My Mood
    Amazed
    Here's a good thread of the use of GetAsyncKeyState()

    https://www.mpgh.net/forum/31-c-c-pro...ckeystate.html

  7. #7
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    Quote Originally Posted by gresan6 View Post
    Here's a good thread of the use of GetAsyncKeyState()

    https://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 <.<

  8. #8
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    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;
    }

  9. #9
    Shanks_'s Avatar
    Join Date
    Jan 2013
    Gender
    male
    Posts
    60
    Reputation
    45
    Thanks
    8
    My Mood
    Pensive
    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;
        }
    }
    ♪♪ ♪ ♪doign it rite , evrybuddy will be dancin and were feeling it rite , evrybuddy will be dancin and be doign' it rite , evrybuddy will be dancign' wen were feeling all rite , evrybuddy will be dancig tonit♪e♪ ♪♪ ♪♪♪

  10. #10
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    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)

  11. The Following User Says Thank You to Fovea For This Useful Post:

    Hell_Demon (11-05-2013)

  12. #11
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    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")

  13. #12
    Shanks_'s Avatar
    Join Date
    Jan 2013
    Gender
    male
    Posts
    60
    Reputation
    45
    Thanks
    8
    My Mood
    Pensive
    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.
    ♪♪ ♪ ♪doign it rite , evrybuddy will be dancin and were feeling it rite , evrybuddy will be dancin and be doign' it rite , evrybuddy will be dancign' wen were feeling all rite , evrybuddy will be dancig tonit♪e♪ ♪♪ ♪♪♪

  14. #13
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    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)

Similar Threads

  1. simple but USEFULLLL TIP !
    By mwarlock in forum Piercing Blow Tutorials
    Replies: 10
    Last Post: 03-28-2012, 09:50 PM
  2. [Release] Random Password Generator (Simple But Very Usefull) - My first project ever!
    By SilentHunter7 in forum CrossFire Spammers, Injectors and Multi Tools
    Replies: 8
    Last Post: 03-21-2012, 08:49 AM
  3. SOMETHING SIMPLE BUT MAGNIFACENT
    By killuhmerc in forum Combat Arms Hack Requests
    Replies: 3
    Last Post: 10-21-2011, 05:39 AM
  4. Going for something simple but elegent
    By Azathᴏth in forum Showroom
    Replies: 13
    Last Post: 05-18-2011, 06:42 AM
  5. [RELEASE] Small, Simple, But Handy Wall Hack
    By Stranger00 in forum WarRock - International Hacks
    Replies: 0
    Last Post: 07-06-2007, 12:02 AM