string s;
getline(cin,s);
ss.push_back(s);
}
not working for input:
n=2
4
5
why 5 is not being pushed to the vector?
i would guess cin.ignore drops the value, you probably need to do it once before the loop (or right after the operation that requires it)
didnt work
I don't know how the code before the loop looks like, but this example works for me: #include <iostream> #include <vector> #include <string> using namespace std; int main() { int n = 2; vector<string>ss; for(int i=0;i<n;i++) { string s; getline(cin, s); ss.push_back(s); } cout << "\n"; for (string value : ss) { cout << value << "\n"; } }
works fine, but what if you do int n; cin>>n; instead hardcoding
#include <iostream> #include <vector> #include <string> using namespace std; int main() { int n; cin>>n; cin.ignore(); vector<string>ss; for(int i=0;i<n;i++) { string s; getline(cin, s); ss.push_back(s); } cout << "\n"; for (string value : ss) { cout << value << "\n"; } }
Обсуждают сегодня