getline not working for converting a string to a vector array
Jul 11, 2021 at 8:13am UTC
I am trying to convert a string from getline into a char vector but the size is 0. I am sure that I am inputting the word test into the terminal correctly.
I used this website to converting the string to a char vector
https://www.techiedelight.com/convert-string-vector-chars-cpp/ and it works fine for a string that initialized with two quotes and a string inside it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
string s;
// Doesn't work
getline(cin, s);
vector<char > convert(s.begin(), s.end());
for (int i = 0; i < convert.size(); i++)
{
cout << "nothing happens here & the size is 0 " << convert[i] << endl;
}
// Works fine
string st = "Hello World!" ;
vector<char > v(st.begin(), st.end());
for (const char &c: v)
cout << "test " << c << endl;
Jul 11, 2021 at 8:30am UTC
Works for me:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
#include <vector>
int main(){
using namespace std;
string s;
// Doesn't work
getline(cin, s);
vector<char > convert(s.begin(), s.end());
for (int i = 0; i < convert.size(); i++)
{
cout << "nothing happens here & the size is 0 " << convert[i] << endl;
}
return 0;
}
Jul 11, 2021 at 8:31am UTC
What comes BEFORE the snippet of code you have shown?
Show COMPLETE, COMPILEABLE code that demonstrates your problem.
Topic archived. No new replies allowed.