Newbie question about cin

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);
}
 
S

Seeker

Seeker said:
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!

No.  Don't use Ctrl-Z to "end the first vector".  Either have a magic
value, or just enter a non-digit.  The input stream is likely closed
after it encounters Ctrl-Z.  If there is a way to re-open it, you need
to look for it in the compiler documentation or the OS documentation,
the same place you looked to find the effects of Ctrl-Z.

V

Thanks for the hint. I tried to use comma or period to end the first
vector but the 2nd cin was still skipped.
 
S

Seeker

Seeker said:
Seeker wrote:
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!
No.  Don't use Ctrl-Z to "end the first vector".  Either have a magic
value, or just enter a non-digit.  The input stream is likely closed
after it encounters Ctrl-Z.  If there is a way to re-open it, you need
to look for it in the compiler documentation or the OS documentation,
the same place you looked to find the effects of Ctrl-Z.
[..]
V
Thanks for the hint. I tried to use comma or period to end the first
vector but the 2nd cin was still skipped.

If the first operation ends with an error (like an attempt to convert a
comma or a period into an integer) then you *have* to clear the stream
from the error condition:

    cin.clear();

to be able to read from it again, and also read out the garbage by using
the 'ignore' member.

RTFM about the members 'good', 'ignore', 'eof', etc.

V

It works now. Thanks for clarification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top