cin.seekg(ios::end);

Discussion in 'C++' started by redbox, Mar 30, 2007.

  1. redbox

    redbox Guest

    i want some code to get a int from user;
    code as show follow:
    #include <iostream>
    using namespace std;
    int main()
    {
    int c;
    cin>>c;
    while(cin.fail())
    {
    cout<<"Need a int,try again!"<<endl;
    cin.clear();
    cin.seekg(ios::end);
    cin>>c;
    }

    }
    but it work well in vc6 and ibm xlC,buf failed in g++;
    who can tell me why ?
    thank you !
     
    redbox, Mar 30, 2007
    #1
    1. Advertising

  2. redbox

    BobR Guest

    redbox <> wrote in message ...
    > i want some code to get a int from user;
    > code as show follow:
    > #include <iostream>
    > using namespace std;
    > int main(){
    > int c;
    > cin>>c;
    > while(cin.fail()){
    > cout<<"Need a int,try again!"<<endl;
    > cin.clear();
    > cin.seekg(ios::end);
    > cin>>c;
    > }
    > }
    > but it work well in vc6 and ibm xlC,buf failed in g++;


    Instead of 'seekg(end)', use 'cin.ignore(<somebigvalue>);'.

    > who can tell me why ?
    > thank you !


    FAQ: http://www.parashift.com/c -faq-lite/

    --
    Bob R
    POVrookie
     
    BobR, Mar 30, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,152
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,268
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,800
    Alex Vinokur
    Mar 6, 2005
  4. mathieu
    Replies:
    2
    Views:
    2,451
    mathieu
    Apr 8, 2009
  5. Fernando
    Replies:
    4
    Views:
    1,856
    Fernando
    Nov 16, 2011
Loading...

Share This Page