Thread: 'getintchar' Project

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Question 'getintchar' Project

    I've made this program that shows the ACSII value and the character, heres a pic from the DOS version:

    http://subsoap.com/ck/getintchar.PNG
    NOTE: thats the old version that shows the line feed (10)

    I'm learning WinAPI and I want to get this string from a edit box then put the string into a varaible then every char in the string I want to convert to a int, so that when theres a 'c' in the string it has it as '99'.

    heres some of the code for it:
    Code:
    BOOL bSuccess;
    int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
    if(bSuccess) 
    {
        	// Then we get the string they entered
        	// First we need to find out how long it is so that we can
        	// allocate some memory
        
        int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
        if(len > 0)
        {
        	// Now we allocate, and get the string into our buffer
        
        	int i,mychr;
        	str buf;
    
        	buf = (str)GlobalAlloc(GPTR, len + 1);
        	GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
    
        	// Now we add the string to the list box however many times
    	    // the user asked us to.
    
    	    for(;*buf;){
        		int mychr=(int)*buf;
        		SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)mychr);
        		//SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
        	}
    
    	/*for(i = 0;i < nTimes; i++)
    	{
    		int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
    
    		// Here we are associating the value nTimes with the item 
    		// just for the heck of it, we'll use it to display later.
    		// Normally you would put some more useful data here, such
    		// as a pointer.
    		SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
    	}*/
    
    	// Dont' forget to free the memory!
        	GlobalFree((HANDLE)buf);
        }
        else 
        {
    	MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
        }
        //and the rest of the stuff..
    }
    I also need add a new item into the list that has the int number AND the char it belongs too*, don't know how I'll do this..

    *the dos version of showing the int and the char:
    Code:
        int mychr;
        while((mychr = getchar()) != 9) {
          if (mychr != 10) printf("\n%i\t%c",mychr,mychr); else nl;
        }
    I want to see for this WinAPI prog if I can do basicly the same with the list box BUT with the WinAPI version it's a string not a intso I'll need to covert it and show it with the character.

    HOPEFULLY this made sence/someone can help me.
    I CAN post the other code if needed.
    CK4R1 0U7
    Last edited by Budgiekarl; 04-21-2004 at 07:25 PM.
    THERE IS NO PLACE LIKE 127.0.0.1

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    for(;*buf;){
    This for loop is just gonna keep looping, because that condition will always be true, as you're not changing the value in buf.

    You do need a loop though. In each iteration, take a character, convert it to an int, then use itoa() to convert that back to a string, and store that in a temporary string. Then use LB_ADDSTRING. Eg:

    Code:
    char *lpTemp=new char[8];
    for (int =0;i<nStringLength;i++)
    {
        itoa((int)TheString[i],lpTemp,10);
        SendMessage(hList,LB_ADDSTRING,0,(LPARAM)lpTemp);
    }
    delete [] lpTemp;
    That should do it.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    how come all answers are in C++...? I guess I'll have to switch to C++ then to have this work...
    THERE IS NO PLACE LIKE 127.0.0.1

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    agh the help you gave didn't really help me...can you use real variables that are from the example above? and do it in C?

    if need be you can copy the entire part then edit the stuff... I want to see if I really do need to transfer to C++...
    THERE IS NO PLACE LIKE 127.0.0.1

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You might as well move to C++ programming. It's more powerful, and more widely used. If I get a chance however I might try to write it in C.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >It's more powerful
    If by powerful, you mean bloated, then yes, it's more powerful.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So you use C?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Just replace:
    Code:
    	    for(;*buf;){
        		int mychr=(int)*buf;
        		SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)mychr);
        		//SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
        	}
    with:
    Code:
    /* Step through buf until we find a null terminator character */
    for (i=0;buf[i] != '\0';i++)
    {
        /* Declare a buffer to hold our formatted string */
        char txt[32];
    
        /* Format a string with sprintf */
        sprintf(txt, "%c = %d", buf[i], (int) buf[i]);
    
        /* Now add it to the list box */
        SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM) txt);
    }
    sprintf() is similar to printf() except that it puts its output into a string instead of writing it to the console. You can use the same format identifiers, but you must make sure the receiving buffer is big enough.

    In your example, there is no reason to use GlobalAlloc(). You are free to use malloc() or calloc() like in a console program.

    EDIT: It's fine to use C. However, when you post on the windows board you may wish to specify that you want a C only answer. You could put it in your signature.

    EDIT2: Added cast for SendDlgItemMessage. The send message functions take the message parameters as numbers. Therefore, if you want to pass a pointer you must cast it into an LPARAM or WPARAM to avoid warnings (as shown in your original post).
    Last edited by anonytmouse; 04-24-2004 at 06:35 AM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    thx very much! I'll add that thingy on my sig..

    I'll NEVER thought about useing sprintf! I knew of it I'll just never thought of useing a char array as a buffer for it and useing sprintf for useing it to put the needed numbers in it... thx!!!
    THERE IS NO PLACE LIKE 127.0.0.1

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    I got theres Warnings:

    D:\Dev-Cpp\getintChar\GUI\getintchar.c(53) : warning C4047: 'function' : 'long ' differs in levels of indirection from 'char [32]'
    D:\Dev-Cpp\getintChar\GUI\getintchar.c(53) : warning C4024: 'SendDlgItemMessageA' : different types for formal and actual parameter 5
    D:\Dev-Cpp\getintChar\GUI\getintchar.c(173) : warning C4101: 'mychr' : unreferenced local variable


    let me see if I can fix them...
    THERE IS NO PLACE LIKE 127.0.0.1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM