4

Look t this code plz:

#include <iostream>
using namespace std;
int main()
{

    enum object {s,k,g};
    object o,t;

    cout << "Player One: "; cin >> o;
    cout << "Player Two: "; cin >> t;

    if (o==s && t==g) cout << "The Winner is Player One.\n";
    else if (o==k && t==s) cout << "The Winner is Player One.\n";
    else if (o==g && t==k) cout << "The Winner is Player One.\n";
    else if (o==g && t==s) cout << "The Winner is Player Two.\n";
    else if (o==s && t==k) cout << "The Winner is Player Two.\n";
    else if (o==k && t==g) cout << "The Winner is Player Two.\n";
    else cout << "No One is the Winner.\n";
        return 0;
}

while compiling I will get this error:no match for 'operator>>' in 'std::cin >> o I'm using code-blocks. so what is wrong with this code?

Inside Man
  • 4,194
  • 12
  • 59
  • 119
  • 1
    Try looking here: http://stackoverflow.com/questions/9220599/error-no-match-for-operator-in-stdcin-stopat Should provide some insight. – Scott F. Apr 29 '12 at 11:21

3 Answers3

10

There is no operator>>() for enum. You can implement one yourself:

std::istream& operator>>( std::istream& is, object& i )
{
    int tmp ;
    if ( is >> tmp )
        i = static_cast<object>( tmp ) ;
    return is ;
}

Of course, it would be easier if you just cin an integer and cast yourself. Just want to show you how to write an cin >> operator.

Matchman
  • 231
  • 2
  • 4
4

Do you expect to be able to type "s", "k", or "g" and have it parse those into your enum type? If so, you need to define your own stream operator, like this:

std::istream& operator>>(std::istream& is, object& obj) {
    std::string text;
    if (is >> text) {
        if (text == "s") {
            obj = s;
        }
        // TODO: else-if blocks for other values
        // TODO: else block to set the stream state to failed
    }
    return is;
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    If it's just a character, a `char` + `switch` would make for easier code to read. Otherwise, an `unordered_map`. – Matthieu M. Apr 29 '12 at 14:21
  • 1
    I agree with the switch thing, but didn't want to complicate my answer with the check for the input being of length 1. I do not agree with using unordered_map, though, as it is not an efficient data structure for small numbers of values. If you wanted something more data-driven, I'd suggest an array of std::pairs and just using std::find for linear search if there are just a few elements. It's easy to step from there to binary search, or even constructing a hash table if someday there are many thousands of players. :) – John Zwinck Apr 30 '12 at 11:19
  • 2
    Don't worry about efficient say, 97% of the time ;) `unordered_map` offers a natural interface for look-up by key, so it just works out of the box. If you want efficiency, you need to avoid constructors and build you array out of PODs so that its initialization can be done entirely during the static initialization and not require any `malloc`/`new` calls... but it's extra-work for little reward. KISS says: `unordered_map` or `map`. – Matthieu M. Apr 30 '12 at 11:43
  • Shouldn't the inner if condition be `if (text == "s")`? – shobhu Nov 23 '21 at 07:14
  • @shobhu Yes of course, fixed, thank you. Took 9 years for someone to notice! – John Zwinck Nov 23 '21 at 10:20
0

If you're not familiar with Operator Overloading concept and want a quick-fix, simply use:

scanf("%d%d", &o, &t);