Get Variable Contents

This is a discussion on Get Variable Contents within the C++ Programming forums, part of the General Programming Boards category; Really? I honestly doubt that is true. Can you give a reason?...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Really? I honestly doubt that is true. Can you give a reason?

  2. #17
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Code:
    Code:
    
    const char* var1 = "test";
    const char* var2 = "";
    const char* var3[] = {var1, var2};
    int a = 2;
    int c = 0;
    
    for(int b=0; b<a; b++) {
         if(var3[b]!="") {
              var3[c] = var3[b];
              c++;
         }
    }
    
    while(c<a) {
         var3[c] = "";
         c++;
    }
    wouldn't code like that give you a compiler error because var3 is const
    Keyboard Not Found! Press any key to continue. . .

  3. #18
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    ^^ That code works for me and I use const...

    Well, after thinking about my source code and looking at it, there are only two functions that I use that require my variables to char. The first one is the mysql_query function. I know I can use some conversion from string to char (since I already have a little bit of a string to char in my code). The other one is GetWindowText function. I dont know if the variable that it stores to can be a string or not, but I dont know if I never try.
    Last edited by stickman; 04-18-2006 at 03:15 PM. Reason: Wrong function...it should be GetWindowText and not GetDlgItem

  4. #19
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Well, I got two conversion functions, one from char to string and one from string to char...I only need these functions because the only way to GetWindowText is to use a char variable. Then I convert the char variable to a string variable and vice versa for SetWindowText. string variables make it much easier!!! I never knew string variables were as easy to use as they are (reminds me slightly of PHP variables, but not quite).

    When I tried finding string to char and char to string functions, people were saying (on other sites) that its impossible or nearly impossible. If anyone is doing the same thing, here are my functions (very short functions cuz I like short functions):

    Code:
    string convert2string(const char* char_var) {
         string string_var;
         string_var.insert(0,char_var);
         return string_var;
    }
    
    char* convert2char(string string_var) {
         int a = 0;
         int b = string_var.size();
         char* char_var = new char[b+1];
         while(a<b) {
              char_var[a] = string_var.at(a);
              a++;
         }
         char_var[b] = '\0';
         return char_var;
    }
    If anyone has any shorter functions, then please post.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    string convert2string(const char* char_var) {
        return char_var;
    }
    
    const char* convert2char(string string_var) {
        return string_var.c_str();
    }
    You don't actually need these functions. For GetWindowText, use a local character array to get the text and then assign that to the string. SetWindowText takes a const char* (not a plain char*), so you can just pass it the results of c_str().

  6. #21
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Thanks for the info on the c_str(). I didnt know that. Before, when I tried setting the string variable to a char[x] (where x is a number), it gave me a compiler error. Now that I tried it again, it actualy worked ( GetWindowText(TEXTFIELD,somevarthatsachar[x],x); string = somevarthatsachar[x]; ). I still need convert2char because I have a query string variable and mysql_query only takes const char* and I have a SendMessage where I update the status bar with a string and it has to be (LPARAM)char (I'm not quite sure exactly which type of char or const char or whatever).

    EDIT: I forgot to apply the info I learned for the c_str() to the mysql_query function, so I can use string_var.c_str() instead of converting it. Only need to find a way for the SendMessage without the functions.

    EDIT EDIT: Just tested string_var.c_str() in the SendMessage function and it worked.

    EDIT EDIT EDIT: Is there any way to store the GetWindowText into a variable without defining the size of the variable?

    THANKS for your help!!!!!!!!!!!!!!!!!!
    Last edited by stickman; 04-18-2006 at 08:56 PM.

  7. #22
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Does getWindowText() return a char*? Then you can do

    std::string str = getWindowText();

    straight away. This operation is defined for std::string and works as expected.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Is there any way to store the GetWindowText into a variable without defining the size of the variable?
    No, unless you use the MFC CString version of GetWindowText. If you'll notice, the version that takes a char* also takes a maxCount number of characters, because with C style strings you have to define the size of the array before you pass it to the function, and the function doesn't want to overrun the array bounds. So what you have to do is define the array to be the maximum number of characters you think will be returned from GetWindowText.

    >> Does getWindowText() return a char*?
    Nope, it takes a pointer as an output parameter.

  9. #24
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    getWindowText( DlgEdittextInt, char*, int )

    I believe thats the "arguments" (using a PHP word, not sure if it applies to C++). char* is the variable to store the entered text to. int is the max number of characters to retrieve from the entered text (so really, my previous question becomes stupid because I would have to define this value and I might as well define a "char array" with the int value number of values. Me being the stupid me, I just realized I would only need one char array variable, for example:

    Code:
    char var[255];
    string var1, var2, var3;
    ....
    GetWindowText( THETEXTFIELD1INT, var, 255);
    var1 = var;
    GetWindowText( THETEXTFIELD2INT, var, 255);
    var2 = var;
    ....
    Before, I had a different "char array" for each edittext box.

  10. #25
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Did I mention, by switching to use strings, I was able to decrease the size of my .exe by four times (it was 2mb and now its .5mb). I was using a regex header before to return some things within the variables, but now with string.find() and string.substr() and string.erase(), I'm able to do all the regex things I was doing before. However, its still uses approx. 4000 [units] of memory, which isnt that bad compared to some other programs.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  4. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21