int n; while(cin>>n) 如何判断输入结束

#include<iostream>
#include<vector>
using namespace std;
int main(){
   	int n, sum = 0;
   	vector<int> input;
	   char ch;
   	while((ch = cin.get()) != '\n'){
		      cin.unget();
      		cin>>n;
      		input.push_back(n);
      		sum += n;
   	}
	   for(int i = 0; i < input.size(); i++){
		      cout<<input[i]<<" ";
   	}
   	cout<<endl;
	   cout<<sum<<endl;
   	return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *