S
Seeker
Hi, C++ gurus
It's a naive question about cin. I had two cin calls in the code but
the second one seems not working somehow. I used Ctrl-Z/Enter to end
the first vector input. Should I reset/clear cin in order to read the
second vector? Thanks!
#include <vector>
#include <iostream>
using namespace std;
int sum(const vector<int>& x) {
int total = 0; // the sum is accumulated here
for (unsigned int i=0; i<x.size(); i++) {
total = total + x;
}
return total;
}
int main(){
int n,tmp1,tmp2,err=0;
vector<int> a,b;
cout << "Enter n: ";
cin >> n;
cout << "Enter global counting vector:";
while (cin >> tmp1){
a.push_back(tmp1);
}
cout <<sum(a);
cout << "Enter individual counting vector:";
while (cin >> tmp2){
b.push_back(tmp2);
}
cout<< sum(b);
system("PAUSE");
return(0);
}
It's a naive question about cin. I had two cin calls in the code but
the second one seems not working somehow. I used Ctrl-Z/Enter to end
the first vector input. Should I reset/clear cin in order to read the
second vector? Thanks!
#include <vector>
#include <iostream>
using namespace std;
int sum(const vector<int>& x) {
int total = 0; // the sum is accumulated here
for (unsigned int i=0; i<x.size(); i++) {
total = total + x;
}
return total;
}
int main(){
int n,tmp1,tmp2,err=0;
vector<int> a,b;
cout << "Enter n: ";
cin >> n;
cout << "Enter global counting vector:";
while (cin >> tmp1){
a.push_back(tmp1);
}
cout <<sum(a);
cout << "Enter individual counting vector:";
while (cin >> tmp2){
b.push_back(tmp2);
}
cout<< sum(b);
system("PAUSE");
return(0);
}