Thread: Not sure if I should be using seekg()

  1. #1
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Not sure if I should be using seekg()

    Just a simple question. I created a function to validate numeric data coming in from cin. I read the posts and some say to use ignore(5000,'\n') or some other huge number. I found that seekg(o,ios::end); seems to work as well but I am not sure if it is going to cause any problems? I would think that it seeks to the end of the stream and effectively ignores all data in the stream. I am just not sure if this is the proper way to use seekg(). Anyway here is my simple function.

    Code:
    int validate_input(const char*message)
    	{
    		int num;
    		char ch;
    		cout<<message;
    		while( (ch=	cin.get() )!='\n')
    			{
    			if((ch>='0' ) && (ch<='9'))
    				{
    					cin.putback(ch);
    					cin>>num;
    				}
    					else
    				{
    					cin.clear();
    					//cin.ignore (50000,'\n');
    					cin.seekg(0,ios::end);
    					cout<<"invalid input Please reenter: \n";
    					cout<<message;
    				
    				}
    			}
    		return num;
    	}
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Thanks for the info.

    I had already read up on those thanks though. I guess I am just trying to answer the question if seekg() can be used the same as the ignore statement. The ignore reads in the characters and then discards where seekg() seeks to the end. What happens to the contents of the buffer when it reaches the end? Are they discarded? seekg doesn't read each character does it? it just goes to the end.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if seekg() can be used the same as the ignore statement
    It really depends on the semantics of seeking with cin. To my knowledge, whether cin is seekable or not (and what happens if it is) is unspecified. Ignore would be the more appropriate solution:
    Code:
    #include <limits>
    ...
    cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File copying using get() and seekg()
    By tonycorruption in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2006, 01:05 PM
  2. Problem with fstream and seekg()
    By hdragon in forum C++ Programming
    Replies: 10
    Last Post: 09-24-2005, 01:50 PM
  3. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  4. seekg woos
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2002, 08:51 PM
  5. seekp(), seekg()
    By Dual-Catfish in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2002, 11:27 AM