resurrecting cin

F

float_dublin

Hi All,
I just have found out (for myself) that the program:

//**************************************
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here I input "a" eg char
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I'm dying....\n";
std::cin.clear(); /// I want just to make another turn and not
cycle for eternity
}
std::cout<<a<<"\n";
}
return 0;
}
//**************************************

cycles forever if i input some char instead of numbers. Fast look throw
books have not showed me any solution for this but inputing into some
string and then parsing. All i want is just resurrect cin to make
another turn, is it possible?
 
A

Anand

float_dublin said:
Hi All,
I just have found out (for myself) that the program:

//**************************************
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here I input "a" eg char
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I'm dying....\n";
std::cin.clear(); /// I want just to make another turn and not
cycle for eternity
// you need to do a sync
std::cin.sync();
 
K

Karl Heinz Buchegger

float_dublin said:
Hi All,
I just have found out (for myself) that the program:

//**************************************
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here I input "a" eg char
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I'm dying....\n";
std::cin.clear(); /// I want just to make another turn and not
cycle for eternity
}
std::cout<<a<<"\n";
}
return 0;
}
//**************************************

cycles forever if i input some char instead of numbers. Fast look throw
books have not showed me any solution for this but inputing into some
string and then parsing. All i want is just resurrect cin to make
another turn, is it possible?

Yes, it is.
If you input eg. the character a, then cin >> a recognizes that it can't
use this character for constructing a number. This the conversion stops
and since no usable character for forming a number was entered the input
operation does 2 things: it stops the conversion with an error. it feeds
back that character to the input stream to be processed later (probably
by another >> operation). So when the loop turns over the next time, the-> You need to get rid of that input. Look up stream member function ignore().
 
K

Karl Heinz Buchegger

Anand said:
// you need to do a sync
std::cin.sync();

sync() has nothing to do with the problem.
The OP needs to get rid of the erronous input. ignore() does this.
 
F

float_dublin

Karl said:
Yes, it is.
If you input eg. the character a, then cin >> a recognizes that it can't
use this character for constructing a number. This the conversion stops
and since no usable character for forming a number was entered the input
operation does 2 things: it stops the conversion with an error. it feeds
back that character to the input stream to be processed later (probably
by another >> operation). So when the loop turns over the next time, the


-> You need to get rid of that input. Look up stream member function ignore().

I did that:
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here i input "a" eg char
std::cin.ignore();
std::cout<<a<<"\n";
}
return 0;
}

but it still loooops.
PS I've tried from std::cin.ignore(); up to std::cin.ignore(25); just
to be certain :)
And by the way i've done it before starting topic - eg clear & ignore
mixture don't works for me. Any qlues?
 
J

John Harrison

float_dublin said:
I did that:
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a; /// Here i input "a" eg char
std::cin.ignore();
std::cout<<a<<"\n";
}
return 0;
}

but it still loooops.
PS I've tried from std::cin.ignore(); up to std::cin.ignore(25); just
to be certain :)
And by the way i've done it before starting topic - eg clear & ignore
mixture don't works for me. Any qlues?

clear

followed by

ignore(some_big_number);

if that doesn't work for you your compiler is broken.

John
 
F

float_dublin

John said:
>
> clear
>
> followed by
>
> ignore(some_big_number);
>
> if that doesn't work for you your compiler is broken.
>
> John
thanks it finally works :) and I am happy.
 
F

float_dublin

followed by
ignore(some_big_number);

if that doesn't work for you your compiler is broken.

John
hmm not so good as i expected. If I ignore(8) - cin waits 8 characters
and only after this my happy int is recognized.
sample:

Input a NUMBER!!!
aaaaa
Oouch, you have not listend me... I'm dying....
1
0 /*<- here 1 is ignored as well as aaaaa*/
Input a NUMBER!!!
1
1
Input a NUMBER!!!

this simple input makes me mad. My prog is 10 lines, and i'm forced to
string input with parsing (eg stringstreams) :(
All I want is int input ]:(
PS I've tried playing with seekg(n,ios::end) and gcount, but it didn't
work.

PPS code:

#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a;
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I'm dying....\n";
std::cin.clear();
std::cin.ignore(8);
}
std::cout<<a<<"\n";
}
return 0;
}
 
A

Anand

float_dublin wrote:
[..]
#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a;
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I'm dying....\n";
std::cin.clear();
std::cin.ignore(8);
// the following works for me
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
// Also the previously said std::cin.sync() worked for me !!!!
// need to figure out why???
 
A

Anand

float_dublin said:
followed by

ignore(some_big_number);

if that doesn't work for you your compiler is broken.

John

hmm not so good as i expected. If I ignore(8) - cin waits 8 characters
and only after this my happy int is recognized.
sample:

Input a NUMBER!!!
aaaaa
Oouch, you have not listend me... I'm dying....
1
0 /*<- here 1 is ignored as well as aaaaa*/
Input a NUMBER!!!
1
1
Input a NUMBER!!!

this simple input makes me mad. My prog is 10 lines, and i'm forced to
string input with parsing (eg stringstreams) :(
All I want is int input ]:(
PS I've tried playing with seekg(n,ios::end) and gcount, but it didn't
work.

PPS code:

#include <iostream>

int main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
std::cin>>a;
if(!std::cin)
{
std::cout<<"Oouch, you have not listend me... I'm dying....\n";
std::cin.clear();
std::cin.ignore(8);
/* try giving a limit to ignore...
ignore X number of characters until '\n'
*/
std::cin.ignore(big_number, '\n');
 
K

Karl Heinz Buchegger

float_dublin said:
hmm not so good as i expected. If I ignore(8) - cin waits 8 characters
and only after this my happy int is recognized.

:)
You have not read the documentation if ignore()

ignore() can have 2 arguments:
1. the number of characters to ignore
2. a special character. When this character appears in the
input stream, ignore() stops ignoring further characters.

In your situation a
std::cin::ignore( MAX_INT, '\n' );

would be what you want.
You want to ignore everything up and including a '\n' character.
That is: from this point on everything up to the end of line the user
has input.
 
F

float_dublin

Thanks everybody.
If anyone is interested I'm happy with this :)

#include <iostream>
#include <stdexcept>

struct __resurrect_cin
{
__resurrect_cin(){}
/**
\brief Resurrect std::cin only, never touch other istreams
*/
static void
resurrect()
{
if(!std::cin)
{
std::cin.clear();
std::cin.ignore(4096,'\n');
throw(std::runtime_error("Wrong input.\n"));
}
}
};

const __resurrect_cin resurrect_cin;

std::istream&
operator>>(std::istream& in, const __resurrect_cin&)
{
__resurrect_cin::resurrect();
return in;
}

int
main()
{
int a=0;
while(true)
{
std::cout << "Input a NUMBER!!!\n" << std::endl;
try
{
std::cin>>a>>resurrect_cin;
}
catch(std::runtime_error& a)
{
std::cout<<a.what();
}
std::cout<<a<<"\n";
}
return 0;
}

/**
Output:

Input a NUMBER!!!

a
Wrong input.
0
Input a NUMBER!!!

aaaa
Wrong input.
0
Input a NUMBER!!!

1
1
Input a NUMBER!!!

*/
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top