Help with clearing cin

I wrote a test program to test istream capabilities.
I wanted the program to accept both piped and terminal input.
However, cin fails after reading from the pipe.
For your information, I use Windows Vista with Watcom compiler.
Watcom doesn't have the library for input and output for strings.

The following is the program I wrote, named testpipe.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string>
#include<string.h>
  using namespace std;

int main(int argc,char*argv[])
{
int c=0;
char a[100];
string b;
while(cin.getline(a,100))
{
b=string(a);
if(c>=50){break;}
cout<<a<<"\n";
cout<<b.c_str()<<"\n";
c++;
}
cin.ignore(2,'\0');
cin.clear();
cout<<endl<<"Please enter something to test terminal input:"<<endl;
cin.getline(a,100);
b=a;
cout<<"RDstate function: "<<cin.rdstate()<<endl;
cout<<"Input:\t"<<a<<endl;
cout<<"Input(string):\t"<<b.c_str();
return 0;
}


and output1 from pipe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
C:\WATCOM\WORK>type testpipe.cpp|testpipe
#include<iostream>
#include<iostream>
#include<string>
#include<string>
#include<string.h>
#include<string.h>
  using namespace std;
  using namespace std;


int main(int argc,char*argv[])
int main(int argc,char*argv[])
{
{
int c=0;
int c=0;
char a[100];
char a[100];
string b;
string b;
while(cin.getline(a,100))
while(cin.getline(a,100))
{
{
b=string(a);
b=string(a);
if(c>=50){break;}
if(c>=50){break;}
cout<<a<<"\n";
cout<<a<<"\n";
cout<<b.c_str()<<"\n";
cout<<b.c_str()<<"\n";
c++;
c++;
}
}
cin.ignore(2,'\0');
cin.ignore(2,'\0');
cin.clear();
cin.clear();
cout<<endl<<"Please enter something to test terminal input:"<<endl;
cout<<endl<<"Please enter something to test terminal input:"<<endl;
cin.getline(a,100);
cin.getline(a,100);
b=a;
b=a;
cout<<"RDstate function: "<<cin.rdstate()<<endl;
cout<<"RDstate function: "<<cin.rdstate()<<endl;
cout<<"Input:\t"<<a<<endl;
cout<<"Input:\t"<<a<<endl;
cout<<"Input(string):\t"<<b.c_str();
cout<<"Input(string):\t"<<b.c_str();
return 0;
return 0;
}
}

Please enter something to test terminal input:
RDstate function: 6
Input:
Input(string):

note that it doesn't wait for input after accepting the piped content

output 2 from purely terminal input works fine
1
2
3
4
5
6
7
8
9
10
11
C:\WATCOM\WORK>testpipe
salkdfjl
salkdfjl
salkdfjl
^Z

Please enter something to test terminal input:
dfslkajlsdf
RDstate function: 0
Input:  dfslkajlsdf
Input(string):  dfslkajlsdf


Thanks for reading. I would like to point out that the cin.clear() does not work as I expect. Any suggestions to solve this problem is appreciated. I noticed that my post is kind of lengthy, and I hope not to see it quoted in replies below.
Last edited on
Is this what you're looking for?

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')


http://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer

As an aside, <cstring> is generally preferable to <string.h>

Also, I note that here:
cout<<"Input(string):\t"<<b.c_str();

you take a proper grown-up C++ string, use c_str() to get an old-school C style string from it, and then output it. Why? You can just output the C++ string:
cout<<"Input(string):\t"<<b;
Is that what you meant by "Watcom doesn't have the library for input and output for strings."? If your C++ compiler can't handle outputting a C++ string using std::cout, abandon it.
Last edited on
Thank you Moschops.
I tried to use ignore before clear but nope it does not work either. As you can see from above,
I used rdstate() to see for errors and it returns a six even after ignore()ing and clear()ing. I have some thoughts about this myself. I wonder if I need to make some calls to get cin to read from terminal after finishing with the piped content?

By the way, the Watcom compiler can't handle string input and output, they said so at the bottom of their website. (http://www.openwatcom.org/index.php/Open_Watcom_FAQ) However, I do need it to compile some pure DOS programs for my old computer. But I am open to better compilers that satisfy my needs.
Topic archived. No new replies allowed.