Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

cin une énumération

#include <iostream>
#include <stdexcept>

using namespace std;

enum X { A, B, C };

istream& operator>> ( istream& in, X& x )
{
  int val;

  if ( in>> val ) {
    switch ( val ) {
    case A: case B: case C:
      x = X(val); break;
    default:
      throw out_of_range ( "Invalid value for type X" );
    }
  }

  return in;
}

int main()
{
  X x;

  try {
    cin>> x;
    cout<< x <<endl;
  } catch ( out_of_range& ex ) {
    cerr<< ex.what() <<endl;
  }
}
Source by www.daniweb.com #
 
PREVIOUS NEXT
Tagged: #cin #une
ADD COMMENT
Topic
Name
8+4 =