Convert char to int

Hi,
I am writing a file parser and I want to implement the Mouse_Move function. The function itself I have worked out, but I want to be able to pull numbers out of a string:
Example:
movemouse 140 78;

The above example is a string. How do I parse the two numbers (140 & 78) into integer form?
Thankyou for your help!
You could use a stringstream.
http://www.cplusplus.com/reference/sstream/istringstream/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <sstream>

    using namespace std;

int main()
{
    istringstream ss("movemouse 140 78;");
    string dummy;
    int x, y;
    ss >> dummy >> x >> y;
    cout << "x: " << x << "    y: " << y << endl;
    return 0;
}

Output:
x: 140    y: 78
You could use a stringstream.
http://www.cplusplus.com/reference/sstream/istringstream/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
istringstream ss("movemouse 140 78;");
string dummy;
int x, y;
ss >> dummy >> x >> y;
cout << "x: " << x << " y: " << y << endl;
return 0;
}

Output:
x: 140 y: 78

How do I put a string value into the custom stream?
EG: istringstream ss(value.c_Str());
Last edited on
How do I put a string value into the custom stream?

either use the constructor:
1
2
string value = " the cat sat on the mat";
istringstream ss(value);

or the str() function.
1
2
istringstream ss;
ss.str(value);

http://www.cplusplus.com/reference/sstream/stringstream/str/
Last edited on
Topic archived. No new replies allowed.