-5

I have a file that consists of data points, and numbers associated with those data points. For example, the file looks something like:

(1,2) 45
(3,4) 12
(23,9) 6 90
(3,5) 4 8

For each data point, I want to set variables "int num1" and "int num2." As you can see, sometimes I have to read an extra number attached to my data point. When there is no extra number, I set num1 and num2 to the value given. I have no problem getting the x and y from the coordinate, but I'm not sure how to check to make sure I'm getting both numbers. I feel like I have to use getline(), but I'm not sure where to go from there. The file is saved as "ins."

char parentheses1, parentheses2, comma;
int x, y, num1, num2;
ins >> parentheses1 >> x >> comma >> y >> parentheses2;
ins >> num1 >> num2;
Evan
  • 98
  • 2
  • 11

1 Answers1

1

Take Option 2 from this answer as a basis for your code.

Read from the stringstream into x and y

iss >> parentheses1 >> x >> comma >> y >> parentheses2;

and then gather numbers until there are none left on the line

std::vector<int> numbers; // dynamic array of numbers
int temp; 
while (iss >> temp) // exits as soon as you can't read an int from the stream
{
    numbers.push_back(temp); // store in vector
}

You can simplify the above a bit if there are never more than, say, 2 numbers.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54