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

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

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -