使用cin的问题

时间:2012-06-30 14:07:13

标签: c++

该程序将'marks'向量的第一个元素复制到'all_marks'向量中。标记向量的注释将动态更改。

#include <iostream>
#include <vector>
using namespace std;
istream& read_info(istream& in,vector<double>& vec);
int main(int argc, const char * argv[])
{

    vector<double> marks;
    vector<double> all_marks;
    while (read_info(cin,marks))  //fails aftr last iteration due to error state of cin
       { 
          all_marks.push_back(marks[1]);
       }
    vector<double>::size_type indx;

    for(indx=0;indx<all_marks.size();++indx)
    {
      cout<<all_marks[indx]<<endl;
    }
    return 0;
 }

 istream& read_info(istream& in,vector<double>& vec)
 {
     vec.clear();
     cout<<"entr mrks with * after last i/p followed by EOF at the last iteration"<<endl;
     double x;
     while(in>>x && x!='*')
     {
         vec.push_back(x);
     }
     return in;  //in will not be in error state until the end of last iteration
                 // which is followed by EOF
  }

但我无法进行第二次迭代。问题是代码。使用'Cin'进行输入似乎非常容易出错。如果问题不清楚,请注意。

0 个答案:

没有答案