WORKAROUND for >> to work with __int64??

This is a discussion on WORKAROUND for >> to work with __int64?? within the C++ Programming forums, part of the General Programming Boards category; It seems that the << and >> operators (which I am using with cin and cout) weren't defined to allow ...

  1. #1
    brian0918
    Guest

    WORKAROUND for >> to work with __int64??

    It seems that the << and >> operators (which I am using with cin and cout) weren't defined to allow __int64 operands, so you can cout << __int64 anything, and you can't cin >> __int64 anything.

    I found a Workaround on the net that allows you to use << so I can cout << __int64, but I can't find one for >>, and I have no clue how to make one.

    The workaround for << is:

    Code:
    #define WORKAROUND
    
    #include<iostream>
    using namespace std;
    
    #ifdef WORKAROUND
    std::ostream& operator<<(std::ostream& os, __int64 i )
    {
        char buf[20];
        sprintf(buf,"%I64d", i );
        os << buf;
        return os;
    }
    
    #endif
    Anyone know how to make a similar one for >> so I could do something like:

    __int64 choice;
    cout << "What's your choice?" << endl;
    cin >> choice;

  2. #2
    brian0918
    Guest

    OOPS

    The first sentence in the post should read:

    It seems that the << and >> operators (which I am using with cin and cout) weren't defined to allow __int64 operands, so you can't cout << __int64 anything, and you can't cin >> __int64 anything.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    5

    another OOPS

    the code should read:

    Code:
    #define WORKAROUND   //Uncomment this line to workaround
    
    #include<iostream>
    using namespace std;
    
    #ifdef WORKAROUND
    std::ostream& operator<<(std::ostream& os, __int64 i )
    {
        char buf[20];
        sprintf(buf,"%I64d", i );
        os << buf;
        return os;
    }
    
    #endif

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Best Bet...define an INT64 class yourself and overload the >> & << streams.......

    You have half of it done for you there....but personally I wouldnt use the sprintf......

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    the whole point of C++ is that you don't have to make stoopid workarounds like this, just make yourself a new int64 class or something and overload the << and >> so you can use it in cout and cin
    hello, internet!

  6. #6
    brian0918
    Guest
    This would be easy if I knew how to do any of this. Unfortunately, I had a horrible professor (doesn't seem to know c++ himself), and everything is vague and undefined. All I can do are basic math programs, and the simplest of things.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    it may not behave exaclty like cin >> (int) or something when an error occurs since I dont know how all errors are handled, but this should serve your purpose just fine.

    Code:
    std::istream& operator>>(std::istream& is, __int64 &i )
    {
       int neg = 0, c;
    
       is.eatwhite();
       
       if ((c = is.get()) == '-' && isdigit(is.peek()))
          neg = 1;
       else if (!isdigit(c)) {
          is.setf(ios::failbit);
          is.putback(c);
          return is;
       }
       else
          is.putback(c);
    
       i = 0;
       for (int k = 0; k < 19; k++) {
          if (isdigit((c = is.get()))) {
             if (unsigned __int64(i*10 + c-'0') < 0x7fffffffffffffff) {
                i = i*10 + c-'0';
             }
          }
          else {
             is.putback(c);
             break;
          }
       }
    
       if (neg) i *= -1;
       return is;
    }
    and remember, as with all input through the extraction operator, its good to check the state of ios::fail (<istream>.rdstate & ios::fail) to make sure the user has inputted what you expect (ie. not a char when you expected an int)

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    Or you could be lazy and upgrade your libraries. The newer MSVC dinkumware standard libraries (that VS.NET ships with) support istream/ostream __int64 operations. They're not free though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dlopen, dlsym does not work in Linux as expected
    By balakv in forum C Programming
    Replies: 2
    Last Post: 04-03-2007, 12:44 AM
  2. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  3. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21