How can i read an unknown number of integers until space character is reached ? (C++)

I need to read numbers from console separated by ; until i get to the space character. The input should look something like this: 3;1 -2;-1;1 1;1;3;2 . There can by more than one space between each set of numbers and i need to insert each set of numbers into a vector . My initial ideea was something like this:

char c;
std::vector<double> coordinates;
while(std::cin >> c){
    if(c != ' ' & c != ';'){
        double a = c - 48;
        coordinates.push_back(a);
    }
}

The problem with this is that converting a char to an integer using ASCII can't be made if I have negative numbers. If anyone could give me another method to do this type of reading or can give me some advice I would appreciate it very much!



Read more here: https://stackoverflow.com/questions/64944195/how-can-i-read-an-unknown-number-of-integers-until-space-character-is-reached

Content Attribution

This content was originally published by Ciobanoiu Theodor at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: