problem with classes and functions in them

Started by
2 comments, last by iansane 15 years, 11 months ago
Hi, I'll try to make this short since I'm posting the code. I found this set of classes and functions that supposedly clears the input stream for different situations. http://www.daniweb.com/forums/thread90228.html I don't understand all of it but I had a simalar problem with the last class I tried to copy and paste from a site. When I call the function the way this guy says to, I get compile errors. "instantiated from here" and "ignoreline not declared(first use this function". I'm thinking since it happened with another class definition, mabey it's cause I'm not puting it in the right part of the program, or there is something else I'm supposed to do. Thanks for any help in understanding this. BTW, if I use getline() in this case, I don't need the function but in other cases, I have used getline() and still have the same problem as cin >> with characters in the stream messing things up. //clearing the input stream #include <ios> #include <istream> #include <limits> #include <iostream> using namespace std; //ignoreline class ignoreline { bool _always_discard; mutable std::streamsize _nread; public: ignoreline( bool always_discard = false ) : _always_discard ( always_discard ), _nread ( 0 ) {} std::streamsize gcount() const { return _nread; } template <typename CharT> friend std::basic_istream<CharT>& operator>> ( std::basic_istream<CharT>& in, const ignoreline& manip ) { manip._nread = ignore_line ( in, manip._always_discard ); return in; } }; class pause { ignoreline _ignore; public: pause ( bool always_discard = false ) : _ignore ( always_discard ) {} std::streamsize gcount() const { return _ignore.gcount(); } template <typename CharT> friend std::basic_istream<CharT>& operator>> ( std::basic_istream<CharT>& in, const pause& manip ) { if ( !( in>> manip._ignore ) ) in.clear(); std::cout<<"Press Enter to continue . . ."; return in.ignore(); } }; int main() { string something; char yesNo; cout << "Enter a string with spaces"; cin >> something; cout << "\nDo you want me to print the string?"; cout << " the stream must be clear cause it did "; cout << "not mess up the yesNo question with characters "; cout << "still in the stream."; cin >> ignoreline(); cin >> yesNo; if (yesNo == 'y') cout << something; system("pause"); return 0; }
Advertisement
well sorry for that. The error said ignore_line, not ignoreline and sure enough, part of the code containing the ignore_line() function was left off. The whole thing looked so confusing and was hard to see where it starts and ends since I have no idea how most of the code works.

Here's what I left off at the top.

template <typename CharT>
std::streamsize ignore_line (
std::basic_istream<CharT>& in, bool always_discard = false )
{
std::streamsize nread = 0;

if ( always_discard
|| ( in.rdbuf()->sungetc() != std::char_traits<CharT>::eof()
&& in.get() != in.widen ( '\n' ) ) )
{
// The stream is good, and we haven't
// read a full line yet, so clear it out
in.ignore ( std::numeric_limits<std::streamsize>::max(), in.widen ( '\n' ) );
nread = in.gcount();
}

return nread;
}

Quote:Original post by iansane
Hi, I'll try to make this short since I'm posting the code.

Please use [source] [/source] tags when posting code. Not only does it make it shorter (by adding a scroll-bar), but it preserves your formatting so that it's actually readable ;)
 ok sorry, wasn't sure how to do that. 


Thanks for the tip.

This topic is closed to new replies.

Advertisement