0

I'm trying to read a string until a ',' character is reached and store what's been read in a new string.

e.g. "5,6"

// Initialise variables.
string coorPair, xCoor, yCoor

// Ask to enter coordinates.
cout << "Input coordinates: ";

// Store coordinates.
cin >> coorPair

// Break coordinates into x and y variables and convert to integers. 
// ?

I also need to store the y value in a separate variable.

What is the best way to do this in C++?

Also, is the best way to validate the input to convert to integers and test the values range?

  • Possible duplicate of [Most elegant way to split a string?](https://stackoverflow.com/questions/236129/most-elegant-way-to-split-a-string) – vasek Aug 02 '17 at 05:21
  • Possible duplicate of https://stackoverflow.com/questions/1894886/parsing-a-comma-delimited-stdstring – cup Aug 02 '17 at 05:22

4 Answers4

1

If you have only one comma seperator in string, you can just find where comma first occurs in input and substring input with found position.

Try following:

std::size_t pos = coorPair.find_first_of(","); //Find position of ','
xCoor = coorPair.substr(0, pos); //Substring x position string
yCoor = coorPair.substr(pos + 1); //Substring y position string
int xCoorInt = std::stoi(xCoor); //Convert x pos string to int
int yCoorInt = std::stoi(yCoor); //Convert y pos string to int
suhdonghwi
  • 955
  • 1
  • 7
  • 20
1

The easiest way to do this is to just let operator>> do all the work for you:

int xCoor, yCoor;
char ch;

cout << "Input coordinates: ";

if (cin >> xCoor >> ch >> yCoor)
{
    // use coordinates as needed ...
}
else
{
    // bad input... 
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You can do this by specifying you delimiter and parsing the string

std::string delimiter = ",";

size_t pos = 0;
std::string token;
while ((pos = coorPair.find(delimiter)) != std::string::npos) {
    token = coorPair.substr(0, pos);
    std::cout << token << std::endl;
    coorPair.erase(0, pos + delimiter.length());
}

std::cout << coorPair << endl;

The last token example in {5,6} the {6} would be in the coorPair.

Another way would be to use std::getline as pointed in the comments:

std::string token; 
while (std::getline(coorPair, token, ',')) 
{ 
    std::cout << token << std::endl; 
}
Samer Tufail
  • 1,835
  • 15
  • 25
0

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

I would recommend using getline().

Below is a small example of how I use it. It takes the input from a stream, so you can either use an ifstream as an input, or do what I did below and convert the string to a stream.

// input data
std::string data("4,5,6,7,8,9");

// convert string "data" to a stream
std::istringstream d(data);

// output string of getline()
std::string val;

std::string x;
std::string y;
bool isX = true;

char delim = ',';

// this will read everything up to delim
while (getline(d, val, delim)) {
    if (isX) {
        x = val;
    }
    else {
        y = val;
    }
    // alternate between assigning to X and assigning to Y
    isX = !isX;
}
blake
  • 76
  • 1
  • 4