Converting a String to a Variable?

Hello. I'm wondering how I could convert a string that I got from GetWindowText into a variable so I can use it in non-string functions.

What I'm trying to do is get the window text from an editbox which only allows numbers to be inputted and convert it into a variable so I can use it in SetCursorPos function. Right now, it gets the string from the window but SetCursorPos only allows numbers to be inputted.

If you could post an example of how to do it, it would be great.

Any help is appreciated.

Thanks.
What kind of variable? strings are variables too, you know.
Well, any type of variable that would work with SetCursorPos command.
I'm still kind of a noob at C++ so I don't know alot about it all.

Thanks.
You mean changing a string to numerical? You could use stringstreams.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

int main()
{
     stringstream ss(stringstream::in | stringstream::out);

     string numAsString = "84";
     ss<<numAsString;

     int numAsInt = 0;
     ss>>numAsInt;

     cout<<numAsInt<<endl;

     ss.clear();

     const int NUM_INTS = 5;
     string numsAsString = "67 5 202 333 567";
     ss<<numsAsString;

     int numsAsInts[NUM_INTS];

     for(int i=0; i < NUM_INTS; i++)
     {
          ss>>numsAsInts[i];
          cout<<numsAsInts[i]<<endl;
     }

     return 0;
}
closed account (S6k9GNh0)
Boost also provides lexical_cast and a more powerful coerce_cast.
@Shacktar Yes, I'm trying to convert it to numerical. Also, will that code work in a Win32 application? I'm building my program as a Win32 program not console.
Last edited on
Yes, the code will work, as long as you #include <sstream> . Of course your Win32 application would not start with int main() and you wouldn't be couting the results.
How would I implement that into a Win32 program? Like, where in the code should I put it?
closed account (S6k9GNh0)
... wherever it's needed. If you're struggling with this, perhaps you should start off with basic console applications?
Where ever I put the "stringstream ss(stringstream::in | stringstream::out);", I get

'stringstream' was not declared in this scope
expected ';' before 'ss'

errors.

I was hoping for a Win32 code not a console code. For me, trying to convert console code into Win32 code is just a time waster.
Last edited on
closed account (S6k9GNh0)
Seriously, if you don't understand the difference between console and windowed applications, you should probably start with the more basic of the two which are console applications. It's better to start off simple when learning a new language.
I already know I should start off with the basics with C++ which is the console apps. But that isn't why I posted this. All I wanted was to know how to convert a string from GetWindowText into a numerical number so that SetCursorPos would be able to use it.
We've given you code that will work whether or not it is in a Win32 project. If you understand how to make windowed applications but can't understand how to use the code we gave you, then you've probably skipped a huge learning step and are in way over your head.
All I wanted to know was, how could I convert a string that I got from GetWindowText command into a numerical number. I know I should be doing console programming and not Win32 right now, but that isn't what I asked. Is it impossible for you to even tell me part of what need to do? Telling me something I already know doesn't help. All I wanted was a little help, is that really that hard to do? Wasting 3 hours to be told to go back to something that I already know is no help.
stringstreams have nothing to do with console programming, you just have to include the right header.
But knowing this is part of the basics that you need to know before attempting anything more complex.
@Coderfail:

In any .cpp where you're going to be using a stringstream, put #include <sstream> at the top (or in at least one of the .h files the .cpp includes).

Observe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//fail.cpp
#include <windows.h>
using namespace std;
//...
HWND hWndEditBox1;
HWND hWndEditBox2;
//...
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_LBUTTONDOWN:
        char coordinateAsCharArr[256];
        int coordinates[2];
        stringstream ss(stringstream::in | stringstream::out);

        GetWindowText(hWndEditBox1, coordinateAsCharArr, sizeof(coordinateAsCharArr));
        ss<<coordinateAsCharArr;
        ss>>coordinates[0];
        ss.clear();

        GetWindowText(hWndEditBox2, coordinateAsCharArr, sizeof(coordinateAsCharArr));
        ss<<coordinateAsCharArr;
        ss>>coordinates[1];
        ss.clear();

        SetCursorPos(coordinates[0], coordinates[1]);
        break;
    case WM_DESTROY:
        PostQuitMessage(WM_QUIT);
        break;
    default:
        break;
    }
    return 0;
}//... 


The above WndProc is for some window that has two edit boxes. It is part of "fail.cpp" which may contain more code. When the user clicks the left mouse button, it will attempt to get the text from each of the edit boxes, and attempt to use the text as coordinates for SetCursorPos. The code should work just fine, except for one thing: stringstream is not defined anywhere!. The coder of the above forgot to put #include <sstream> under #include <windows.h> . This code will compile fine after that.

1
2
3
4
5
//win.cpp
#include <windows.h>
#include <sstream>
using namespace std;
//... 

I don't know how I can be more clear than this. As stated before, if you're having trouble with the concept of header files, then you should start off with easier problems before tackling Win32. Besides the program entry points and the fact that Win32 programs run as threads waiting for messages, there is no lack of interchangability for code between console and Win32 GUI apps.
Last edited on
Topic archived. No new replies allowed.