|  | 
|  | 
| back to board | why am i wrong? on c++ #include <iostream>#include <vector>
 #include <math.h>
 #include <stdio.h>
 using namespace std;
 int main()
 {
 vector<int> v;
 int value;
 while(true)
 {
 cin >> value;
 if(cin.eof())
 {
 break;
 }
 else if(cin.fail())
 {
 cin.clear();    // 清空错误状态
 cin.ignore();   // 忽略掉下一个字符
 continue;
 }
 else if(value >= 0)
 {
 v.push_back(value);
 }
 }
 cout << fixed;
 cout.precision(4);
 for(vector<int>::reverse_iterator iter = v.rbegin(); iter != v.rend(); ++iter)
 {
 if(*iter == 0)
 cout << (double)0 << endl;
 else
 cout << sqrt(*iter) << endl;
 }
 return 0;
 }
Re: why am i wrong? on c++ Posted by cures  18 May 2014 10:02Do not break on cin.eof(), it may be set even if a good value is read (in case the last line is not terminated by newline, I guess). Or maybe they terminate data by non-numeric symbols? Nobody would say :)Break on if (!cin), do not clear cin.
 And why do you check for (*iter==0)? It works fine as in general case.
 | 
 | 
|