473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

resurrecting cin

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?

--
float_dublin
Sep 14 '05 #1
11 1274
float_dublin wrote:
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(); }
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?

--
float_dublin

Sep 14 '05 #2
float_dublin wrote:

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
operation sees that very same character again and again and again.

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

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 14 '05 #3
Anand wrote:
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();


sync() has nothing to do with the problem.
The OP needs to get rid of the erronous input. ignore() does this.
--
Karl Heinz Buchegger
kb******@gascad.at
Sep 14 '05 #4
Karl Heinz Buchegger wrote:
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
operation sees that very same character again and again and again.


-> 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?
Sep 14 '05 #5
float_dublin wrote:
Karl Heinz Buchegger wrote:
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
>>operation sees that very same character again and again and again.


-> 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?


clear

followed by

ignore(some_big_number);

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

John
Sep 14 '05 #6
John Harrison wrote:

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.

--
float_dublin
Sep 14 '05 #7
> 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;
}
Sep 15 '05 #8
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::streamsiz e>::max(),'\n');
// Also the previously said std::cin.sync() worked for me !!!!
// need to figure out why???
}
std::cout<<a<<"\n";
}
return 0;
}

Sep 15 '05 #9
float_dublin wrote:
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'); }
std::cout<<a<<"\n";
}
return 0;
}

Sep 15 '05 #10
float_dublin wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 15 '05 #11
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!!!

*/
Sep 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cal_2pac | last post by:
Resurrecting a month old thread.. (listed at http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/2f4e50e1e316eef4/5924203f822f7f4b?q=cal_2pac&rnum=3#5924203f822f7f4b) Somehow...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
7
by: Pat Sheen | last post by:
I'm a novice at designing web pages so I'm not sure how complicated this is. I'd like a page that helps me take notes for research. It would have something like two frames. One with a word...
14
by: Robert | last post by:
I have some questions about these meta tags: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> What charset should I use? Is this latin-1? I have MacOS 10.2.6. Where...
28
by: Stefano Masini | last post by:
On 8 Sep 2005 08:24:50 -0700, Fuzzyman <fuzzyman@gmail.com> wrote: > What is pythonutils ? > ===================== > ConfigObj - simple config file handling > validate - validation and type...
32
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I need it to accept any number of parameters (one needs to be the number of the other parameters) Can you help me out here? Here's...
0
by: Dean Slindee | last post by:
Before a delete on any table, I would like to write the contents of the soon-to-be-deleted row to that application's single "graveyard" table (alternate: or document as coded below). SQL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.