switch case with enum types help pls
Jul 4, 2014 at 7:57pm UTC
I'm trying to convert the enum type {PG, R, G, PG-13, etc.} to strings so i can use it in cout statement but no matter what i put inside switch(), the compiler keeps saying
Error: expression must have integral or enum type. What am I doing wrong exactly?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Movie covert_rating(Movie r) {
switch (r) {
case PG:
return "PG" ;
break ;
case R:
return "R" ;
break ;
case G:
return "G" ;
break ;
case PG13:
return "PG-13" ;
break ;
case NC17:
return "NC-17" ;
break ;
case NR:
return "NR" ;
break ;
default :
cout << "Error in movie_rating() function" << endl;
exit (1);
}
Last edited on Jul 4, 2014 at 10:26pm UTC
Jul 4, 2014 at 8:54pm UTC
How is Movie defined?
Jul 4, 2014 at 10:15pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#ifndef MOVIE_
#define MOVIE_
#include <iostream>
#include <string>
using namespace std;
enum Movie_Rating {G,PG,PG13,R,NC17,NR} ;
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
class Movie
{
public :
// ------------------------------------------------------
// ----- Constructors -----------------------------------
// ------------------------------------------------------
//Default constructor
Movie();
//Copy Constructors
Movie(const string& title) ;
Movie(const string& title,
const string& director,
Movie_Rating rating,
unsigned int year,
const string& path) ;
// ------------------------------------------------------
// ----- Inspectors -------------------------------------
// ------------------------------------------------------
string getTitle() const ;
string getDirector() const ;
Movie_Rating getRating() const ;
unsigned int getYear() const ;
string getURL() const ;
// ------------------------------------------------------
// ----- Mutators ---------------------------------------
// ------------------------------------------------------
void setDirector(const string& director) ;
void setRating(Movie_Rating rating) ;
void setYear(unsigned int year) ;
void setURL(const string& path) ;
//----------------------------------------------------------
//----- Facilitators ---------------------------------------
//----------------------------------------------------------
string Movie::toString ();
void output(ostream & out);
// ----------------------------------------------------------
// ----------------------------------------------------------
private :
string title_ ;
string director_ ;
Movie_Rating rating_ ;
unsigned int year_ ;
string url_ ;
};
#endif
im trying to make it so that it will output the enum by name like PG, NC-17
Last edited on Jul 4, 2014 at 10:15pm UTC
Jul 4, 2014 at 10:48pm UTC
Your enum's type is Movie_Rating
so it needs to look like:
1 2 3 4 5 6 7 8
std::string covert_rating(Movie_Rating r) {
switch (r) {
..
}
}
Note that the return type must also be std::string (or equivalent), not Movie.
Last edited on Jul 4, 2014 at 10:49pm UTC
Jul 4, 2014 at 11:25pm UTC
Oooooooh wow. this was a really stupid mistake on my end thanks for pointing it out :)
Topic archived. No new replies allowed.