How to take a whole line as input using vector in c++? -


**example:-animals ( reptiles birds ( eagles pigeons crows ) ) input? **

not getting answer input animals ( reptiles birds ( eagles pigeons crows ) )

#include<vector> #include<string> #define ll long long  #include <iostream> using namespace std;  int main()  { ll n,m,k,cb=0,ob=0; cin>>n; string c; vector<string> s; while(cb!=ob) {     cin>>c;     if(c=="(")     ob++;     else if(c==")")     cb++;     s.push_back(c); } for(ll i=0;i<s.size();i++) cout<<s[i];  return 0; } 

first problem first thing read long long value. animal isn't.

you checking while(cb!=ob). lines above cb , ob both initialized 0. loop never run.

even if fix loop next problem input doesn't start (. after reading animal loop exit.

the third problem output. don't flush cout when done. also: why use long long i? std::vector::size() returns size_t. please stick that.

one "easy" solution this:

#include<vector> #include<string> #include <iostream> using namespace std;  int main() {     int cb = 0, ob = 0;     string c;     vector<string> s;     while((ob==cb && ob==0) || ob!=cb)     {         cin >> c;         if (c == "(")             ob++;         else if (c == ")")             cb++;         s.push_back(c);     }     (size_t = 0; < s.size(); i++)     {         cout << s[i];     }     cout << endl;     return 0; } 

one other thing: code won't work if parenthesis won't match (endless loop or premature break) , won't handle text behind last closing parenthesis.

as question isn't specific want answer showing main problems.

i suggest check input stream errors.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -