c++ - while(cin) cycle DOUBLE displaying -


here code i've written. there simple template print function see.. works int type vector, double doesn't what's issue?

 #include <iostream>  #include <vector>  using namespace std;   template <typename t>  void print (vector<t> &v) {      (int i=0; i<v.size(); i++)          cout<<v[i]<<'\t';   }   int main() {     vector<int> vec;     int a;     while (cin>>a)        vec.push_back(a);     print(vec);     vector<double> vec1;     double b;     while (cin>>b)        vec1.push_back(b);    print(vec1);    return 0;    system("pause"); } 

i had tested defined while cycle.. while (some_integer<10) , works, doesn't work without defined value of how many times should run can me out solve it? couldn't figure out

your loop follows:

// continue reading things while cin in state , read succeeds (int datatype) while (cin >> a) // ... 

to exit loop read non-integer (such letter or .) , cin enter fail state.

while in fail state reads after fail silently.

you'll want cin.clear() clear fail state before attempt read after it!


Comments

Popular posts from this blog

How can I fetch data from a web server in an android application? -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -

jquery - How can I dynamically add a browser tab? -