string

how can i convert the x, y, and z string values to integers
Last edited on
Use std::stoi
how can i separate string, at comma location, into two strings (for x and yz part)
Last edited on
Your questions sure are more than a bit vague.

If you are using std::string, to find a character use either std::string::find or std::string::find_first_of.

http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/find_first_of/

If you are using a C-style string (char array), to find a character you could use strchr or strbrk.

http://www.cplusplus.com/reference/cstring/strchr/
http://www.cplusplus.com/reference/cstring/strpbrk/

To generate sub-strings from a std::string, use std::stringsubstr.

http://www.cplusplus.com/reference/string/string/substr/

To generate sub-strings from a C-style string you have several options. You could use strcspn, strchr, strbrk or strtok.

http://www.cplusplus.com/reference/cstring/strcspn/
http://www.cplusplus.com/reference/cstring/strtok/

Time to do some reading.
Maybe you need to clarify your question, @Waxbee999.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void getXYZ( const string &str, int &x, int &y, int &z )
{
   char comma;
   stringstream( str ) >> x >> comma >> y >> comma >> z;
}


int main()
{
   int x, y, z;
   string test = "1, 10, 100";
   getXYZ( test, x, y, z );
   cout << "x: " << x << "     y: " << y << "     z: " << z << '\n';
}


x: 1     y: 10     z: 100
Topic archived. No new replies allowed.