Hi all!

I've been searching the internet and various C++ books for quite some time now, and I appear to have reached a brick wall.

As suggested in a given algorithm written by my professor, during input validation, if the stream goes into failure, I am to "reset cin" followed by "clear cin.."

Even though I'm still learning the ins and outs of C++, I have found this way of doing it:

cin.clear();
cin.ignore(250, '/n')

Now, this would be fine and dandy, however that does not exactly match the given algorithm (as mentioned above).

After spending way too much time trying to find a reset function, I came across" cin.reset(); on one lone website.

Upon trying to implement my hopeful find, I my complier spits out "‘struct std::istream’ has no member named ‘reset’" which, if I'm not mistaken, refers to a missing header file.

And, yes, I have tried numerous header files.

Aside from CIN reset, which appears to be a totally different bowl of cheerios, I can only find a tiny handful of cin.reset(); mentioned elsewhere on the internet. Even my C++ textbooks, and "C++ in a Nutshell" make no mention of cin.reset(); Can anyone shed some insight on cin.reset() ? Does it really exist?

If so, what header file do I need to include?

Am I worrying about the exact wording of my prof's algorithm too much and just use

cin.clear();
cin.ignore(250, '/n')

?

I'd really appreciate any insight I can get on this.

Thanks in advance and have a wonderful Saturday!

Recommended Answers

All 6 Replies

>Can anyone shed some insight on
>cin.reset()? Does it really exist?
No. By "reset", hopefully your professor meant for you to flush it.

>Am I worrying about the exact wording of my prof's algorithm too much and just use
You are, but he also didn't really communicate the task to you very well. Your solution is quite likely what he wanted, and you can improve it by reading the thread I linked to above.

Thanks a lot for responding. I really appreciate it.

Ok...here we go...

Does it really exist?

No.

Could it maybe exist in the world of Visual C++?

By "reset", hopefully your professor meant for you to flush it.

Wow what a great link!

Unfortunately most of what you discussed in your very through post is a little beyond my scope right now.

I'm not sure I'm understanding the concept of flushing the input stream correctly, but from what I've gathered, flushing is useful

When you want to remove extraneous characters from an input stream

I'm not exactly sure that's the goal I'm trying to accomplish. I need to reset the input steam after it goes into failure. Maybe that's exactly what you were talking about and I just couldn't catch on.

To my little newbie mind, the sync member function seems to come the closest to what I percieve I'm trying to do. But that seems a little sketchy.

Am I worrying about the exact wording of my prof's algorithm too much

and just use
You are, but he also didn't really communicate the task to you very well. Your solution is quite likely what he wanted, and you can improve it by reading the thread I linked to above.

I thought so, heh. After posting, I had a moment of clarity: When she says "reset cin" does she mean to clear the stream status flags, in essence, "reseting" the input stream? And the ever so useful cin.ignore(); consumes, or "clears" the characters in the input stream.?

Thanks again! Back to coding!

>Could it maybe exist in the world of Visual C++?
It could, just like cin.imalittleteapot(); could exist as well. If your professor is teaching you compiler extensions as if they were standard C++, I'd question her competence.

>Unfortunately most of what you discussed in your very
>through post is a little beyond my scope right now.
It works toward an advanced solution. You only need to read the first quarter of the thread for the two simple and common solutions.

>I need to reset the input steam after it goes into failure.
Not meaning to oversimplify, but it goes into failure because there are unexpected characters in the stream. The only way to deal with that is to remove the unexpected characters (ie. read and discard everything you can) so that the stream buffer is empty.

>When she says "reset cin" does she mean to clear the
>stream status flags, in essence, "reseting" the input stream?
Probably, now that I think about it.

Could it maybe exist in the world of Visual C++?

It could, just like cin.imalittleteapot(); could exist as well. If your professor is teaching you compiler extensions as if they were standard C++, I'd question her competence.

heh. Fortunately my prof didn't teach cin.reset(); My husband (who's also a computer science student) recalled it from somewhere. After seeing it, as I mentioned earlier, on a very small handful of websites, I've become quite curious about this seemingly mysterious member function.

Here's a few examples of cin.reset(); that I've stumbled upon:

#include <conio.h>
#include <iostream>
#include "menu.h"
.
.
.
int Menu::getSelection() {
  // I have fixed this up to handle the case where something is input that
  // isn't even a number...
  int selection = 0; // make sure it's out of range to start
  cout << ">>>" << flush;
  cin >> selection;
  // whether or not we read something ok, we should clean up the stream
  // because there could have been garbage after the number, for example
  cin.reset();
  cin.sync();

post from GameDev.Net
and

1) The variable contents are not actually changed. (Thus if it was uninitialized before, it will remain so.)
2) The stream "fails"; at this time you must "reset" the stream before you can read any more from it (e.g. cin.reset()).
3) After resetting the stream, no data will have actually been taken off of the stream - thus you will need to skip past it before you try to read into the same variable again (otherwise it will just fail again). This can be done in a variety of ways.

post from GameDev.Net
From what I can tell, the first example appears to be a DOS based C++ programming library due to the <conio.h> header file, however I have found no evidence that <conio.h> contains cin.reset(). The second example appears to be standard C++. Upon implementing cin.reset(), (if it actually exists, which I'm not entirely convinced) it seems that you might need a header file? The compiler error is as follows:

error: ‘struct std::istream’ has no member named ‘reset’

We've tried a few different i/o headers however nothing contains that elusive member function. 'Though, we haven't tried every header file in existence.

shrug

This is bugging me!

Unfortunately most of what you discussed in your very
through post is a little beyond my scope right now.

It works toward an advanced solution. You only need to read the first quarter of the thread for the two simple and common solutions.

I gotcha! I feel like a giant idiot, but I can't seem to get your ignore function to compile. ;x cin.clear() and cin.ignore(250, '\n') is working quite well for me.

Thanks again for your response! It's much appreciated!

>the first example appears to be a DOS based C++
>programming library due to the <conio.h> header file
conio.h is available outside of DOS-based C++. What you should be looking for is <iostream.h>, which says that the program is expecting to use pre-standard C++. If you see <iostream> instead (without the .h), the program at least recognizes standard C++.

>Upon implementing cin.reset(), (if it actually exists, which I'm
>not entirely convinced) it seems that you might need a header file?
cin.reset() (assuming it exists, which it doesn't) is a member function of cin. So you would only need the header that includes the cin object, which is <iostream>.

>the first example appears to be a DOS based C++
>programming library due to the <conio.h> header file
conio.h is available outside of DOS-based C++. What you should be looking for is <iostream.h>, which says that the program is expecting to use pre-standard C++. If you see <iostream> instead (without the .h), the program at least recognizes standard C++.

Wow- I never knew that! Thanks for the mini lesson!

>Upon implementing cin.reset(), (if it actually exists, which I'm
>not entirely convinced) it seems that you might need a header file?
cin.reset() (assuming it exists, which it doesn't) is a member function of cin. So you would only need the header that includes the cin object, which is <iostream>.

So it's pretty much settled there's no such thing. Maybe I can sleep at night knowing that.

Thanks you ver much for sharing you knowledge with me! Even though I've said it before, it's worth saying again: I really appreciate you time and wisdom!

Have a great Sunday!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.