0

I have a string of different integers separated by a comma. For example "45,67,2,3,8,9,123,5,6,3,9,4,7,2,4,4,5,69,9,99". I want to read this line as a stringstream and put integers to the vector of integers. On the console, it displays "no response on stdout".

The code looks as follows:

#include <sstream>
#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str) {
   stringstream myString;
   vector<int> of_integers;
   char ch;
   int a;
   for(int k = 0; k<str.size(); k++)
   {
      myString >> a >> ch;
      of_integers[k] = a;
   }
return of_integers;
}

int main() {
   string str;
   cin >> str;
   vector<int> integers = parseInts(str);     //function parseInts
   for(int i = 0; i < integers.size(); i++) {
       cout << integers[i] << "\n";
   }

return 0;
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Rimita Goni
  • 61
  • 1
  • 8
  • 1
    Maybe you would like to initialize your stringstream object to non-empty. Like: `stringstream myString{str};` – jpmarinier Nov 21 '21 at 12:45
  • I followed your suggestion but the Visual Studio still gives the error message. On lines of the for loop where i used str.size() and integers.size() - C4018. I think there might be some issue with that. Do you have other advice? – Rimita Goni Nov 21 '21 at 15:12
  • So I needed to give a size to the integer vector of_integers. And, it all worked out, sadly wrong. uhmm I gave it the size of the str string. But there might be multiple digit integers which makes the array HUGE. I don't know how to solve this one. At first, I used vector because I read that it is dynamiccc, you can assign values to it without first assigning size – Rimita Goni Nov 21 '21 at 15:34
  • Like I gave it on input "45,67,2,3,8,9,123,5,6,3,9,4,7,2,4,4,5,69,9,99". And got this on output 45 67 2 3 8 9 123 5 6 3 9 4 7 2 4 4 5 69 9 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 – Rimita Goni Nov 21 '21 at 15:40
  • You don't know in advance how many integers you will be reading. As the code is, if you read a line with 23 characters, you attempt to read 23 numbers, this is what `str.size()` does for you. That does not make sense. It cannot be a for(;;) loop. It has to be a while() loop, or something similar. And you probably want to default-initialize to an empty vector, and then use the [`vec.push_back(a)`](https://en.cppreference.com/w/cpp/container/vector/push_back) method to add the integer values into your vector. – jpmarinier Nov 21 '21 at 15:59
  • ooh, I understand, now! Thank you very much. I have encountered push_back() function a couple of times but understood what it does only now. – Rimita Goni Nov 21 '21 at 16:46
  • Do you have some idea on how to write/ organize the while loop? I think knowing whether 'int a' that we are reading at the input is empty will help to be a condition for the loop. Something like "while(a != null) { myString>>a>>ch; of_integers.push_back(a);}" – Rimita Goni Nov 21 '21 at 17:00
  • The stringstream classes have error detection methods such as eof() and fail(). See [that other SO question](https://stackoverflow.com/questions/20572203/detect-when-at-the-end-of-a-stringstream) for example. – jpmarinier Nov 21 '21 at 17:09
  • Thank you so much! I finally worked it all out. I am so happy right now. Should I post my code here as an answer? Thanks for directing me to the right thread. Because, I did not know even how to formulate my question :P – Rimita Goni Nov 21 '21 at 17:48
  • Congratulations ! Well yes, SO rules are such that you are welcome to answer your own question. After a reasonable delay, say a couple of days, you can even accept/approve your own answer, if you find that nothing better has come up in the meantime. – jpmarinier Nov 21 '21 at 22:30

1 Answers1

1

The working code:

#include <sstream>
#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str) {
    stringstream myString(str);
    int size = str.size();
    vector<int> of_integers;
    char ch = ',';
    int a=0;
    while(myString.eof()!=1)
    {
        myString >> a >> ch;
        of_integers.push_back(a);
    }
    return of_integers;
}

int main() {
    string str;
    cin >> str;
    vector<int> integers;
    integers = parseInts(str);
    for (int i = 0; i < integers.size(); i++) {
        cout << integers[i] << "\n";
    }

    return 0;
}

Thanks for @jpmarinier, for referencing an useful stringstream function .eof() that detects when the stringstream ended. The code worked well.

Input:

45,67,2,3,8,9,123,5,6,3,9,4,7,2,4,4,5,69,9,99

Output:

45
67
2
3
8
9
123
5
6
3
9
4
7
2
4
4
5
69
9
99
Rimita Goni
  • 61
  • 1
  • 8