Thread: Retaining same amount of digits

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    Retaining same amount of digits

    Well, I have a number that I wish to stay at four digits and I have no idea as to how to keep an integer stick to such a scheme.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int number = 0000;
    	for ( ; number < 1000 ; number++)
        {
            printf("&#37;d\n", number);
        }
    
        return 0;
    
    }
    It should come out like this;
    Code:
    0000
    0001
    0002
    0003
    0004
    0005
    0006
    0007
    0008
    0009
    0010
    Yet it comes out like this;
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Any assistance would be appreciated

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Try using "%04d" instead of just "%d"

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Look up a reference to printf, often done today by entering "man printf" into your favorite search engine. The result will be that you will use "%04d" instead of "%d".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Worked great Thank you to all who answered my question! I have one last one before I run off into the shadows :P How do I implement this with a WinAPI? (e.g. MessageBoxA)

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    char buff[64]; // big enough...
    
    // loop
    sprintf(buff, "&#37;04d", number);
    MessageBox(hwnd, buff, "LOOK!", MB_OK);
    // end loop ;)

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by int3 View Post
    Worked great Thank you to all who answered my question! I have one last one before I run off into the shadows :P How do I implement this with a WinAPI? (e.g. MessageBoxA)
    Use sprintf() to format the data into a string like this:

    Code:
    char digits[5]; /* Space for 4 digits plus null terminator */
    sprintf(digits, "%04d", value);
    Then pass the "digits" string to the message box call.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Doesn't work, thanks for the replies however
    Code:
    sprintf(buff, "&#37;04d", spam);
    PostMessage((HWND) msWin, WM_CHAR, buff,(LPARAM)0);
    invalid conversion from `char*' to `WPARAM'
    initializing argument 3 of `BOOL PostMessageA(HWND__*, UINT, WPARAM, LPARAM)'

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    PostMessage((HWND) msWin, WM_CHAR, (WPARAM) (char *)buff, (LPARAM) 0);

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Thanks again zacs7! (second time you've helped me)

    Now the code parses correctly with no errors but it doesn't actually output anything :P

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    HANDLE msWin;
    
    DWORD pID = 0;
    DWORD buf = 0;
    DWORD memAddr = 0x007D4E28;
    
    int main(void)
    {
        int number = 0000;
        printf("Enter the number at which you wish to start at\n");
        scanf("&#37;04d", &number);
        HWND mWin = FindWindow(NULL, "Untitled - Notepad");
        if(mWin) {
                 printf("Started spamming.\n");
                 char buff[5];
                 DWORD wTP = GetWindowThreadProcessId(mWin, &pID);
                 msWin = OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
                 for (; number < 9999 ;)
                 {
                 Sleep(10);
                 ReadProcessMemory(msWin, (LPVOID)memAddr, (LPVOID)&buf, 2, NULL);
                 if ( buf != 0 ) {
                          printf("Died?");
                          system("PAUSE");
                          }
                 else {  
                          sprintf(buff, "%04d", number);
                          PostMessage((HWND) msWin, WM_CHAR, (WPARAM) (char *)buff, (LPARAM) 0);
                          PostMessage((HWND) msWin, WM_CHAR, VK_RETURN,(LPARAM)0);    
                          number =+ 1;
                 }
                 }
        else {
             printf("Victim not found D:\n");
             system("PAUSE");
             return 0;
             }
    }
    Any ideas?

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Your not asking it it output anything ?

    Your just sending WM_CHAR (buff, 0) to the window that you find. Who's to say your going to get any output?

    Also:
    Code:
    int number = 0;
    as 0000 == 0...

    Don't name 2 different variables buff and buf, its confusing. To make sure the loop is running as you want:

    Code:
    // blah..
    sprintf(buff, "&#37;04d", number);
    printf("buff is: %s\n", buff);
    PostMessage((HWND) msWin, WM_CHAR, (WPARAM) (char *)buff, (LPARAM) 0);
    PostMessage((HWND) msWin, WM_CHAR, VK_RETURN,(LPARAM)0);    
    number =+ 1;
    //.. blah

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    zacs7, thanks for you ongoing support but it appears I made a typo :P

    The program doesn't input anything into the window/application I specified.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This is more Windows related. But once you've found the window, I think you need to pinpoint the control...

    Dunno haven't done windows programming in a while.

    EDIT: Oopsies, wrong Windows message
    Last edited by zacs7; 04-29-2007 at 02:42 AM.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    I'm pretty sure it isn't, nonetheless I think I have pin-pointed my problem. Will be back to tell results D:

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well I was wrong... So your really sending different virtual keys (between [0, 9999) ?)

    Dunno what you plan to achieve by that?

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    zacs7, I plan to make a small program that will combat my friend's program :P (reasoning behind it)

    I noticed a small but in the dialog which was having messages posted to it. (msWin to mWin) I still have the same problems however

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determing the amount of change(money)
    By Kyeong in forum C Programming
    Replies: 11
    Last Post: 09-30-2008, 04:36 PM
  2. calcuations
    By redmondtab in forum C Programming
    Replies: 39
    Last Post: 09-15-2006, 08:09 PM
  3. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 01:27 PM
  4. Having a problem!
    By Zildjian in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2004, 09:40 AM
  5. I need help badly
    By taz_jiggy in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 09:36 PM