Problem with enum

1
2
3
4
5
6
7
8
9
10
<...>
const unsigned short int PETROL_MARK_NUMBER = 4;
enum PETROL_MARK {STANDART=0, REGULAR, PREMIUM, SUPER};
const float COST_PER_GALLON[PETROL_MARK_NUMBER] = {2.94, 3.15, 3.37, 3.61};
<...>
unsigned int GetTotalRefuel(PETROL_MARK mark) const {return TotalRefuel[mark];}
<...>
for(PETROL_MARK Mark=STANDART; Mark<PETROL_MARK_NUMBER; ++Mark)
	PumpCash += it->GetTotalRefuel(Mark) * COST_PER_GALLON[Mark];
<...>

H:\C++\Univer\KURS\main.cpp(155) : error C2675: unary '++' : 'enum PETROL_MARK' does not define this operator or a conversion to a type acceptable to the predefined operator


If I change
1
2
for(PETROL_MARK Mark=STANDART; Mark<PETROL_MARK_NUMBER; ++Mark)
	PumpCash += it->GetTotalRefuel(Mark) * COST_PER_GALLON[Mark];

to
1
2
for(unsigned int Mark=STANDART; Mark<PETROL_MARK_NUMBER; ++Mark)
	PumpCash += it->GetTotalRefuel(Mark) * COST_PER_GALLON[Mark];


I'll must change
unsigned int GetTotalRefuel(PETROL_MARK mark) const {return TotalRefuel[mark];}
to
unsigned int GetTotalRefuel(unsigned int mark) const {return TotalRefuel[mark];}

Is there other way to solve this problem?
I'm not sure, but I think you can't do arithmetic operations on enumerations. Maybe if you convert it to an integer?
You could overload the ++ operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PETROL_MARK operator ++ ( PETROL_MARK &pm )
{
     switch (pm)
     {
          case STANDART:
               pm=REGULAR;
               break;
          case REGULAR:
               pm=PREMIUM;
               break;
          case PREMIUM:
               pm=SUPER;
               break;
          case SUPER:
               pm=STANDART;
     }
     return pm;
}
Last edited on
if you convert it to an integer?
PetrolMark = (int) PetrolMark + 1
H:\C++\Univer\KURS\main.cpp(153) : error C2440: '=' : cannot convert from 'int' to 'enum PETROL_MARK'
PetrolMark = (PETROL_MARK) ((int) PetrolMark + 1)
0 error(s).... Funny code

Bazzy, thanks.

Last edited on
Oh, no!!!

const unsigned short int PETROL_MARK_NUMBER = 4; case SUPER: pm=STANDART; for(PETROL_MARK Mark=STANDART; Mark<PETROL_MARK_NUMBER; ++Mark)


..... Oh, yes

case SUPER: pm=(PETROL_MARK) PETROL_MARK_NUMBER;
Last edited on
This seems to be a really hideous use of an enum.

I'm going to suggest a couple of better alternatives.

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
#include <iostream>
#include <string>
#include <map>

using namespace std;

typedef enum PETROL_MARK {STANDART=0, REGULAR, PREMIUM, SUPER};

int main() {

  map<PETROL_MARK, double> costPerGallon;
  costPerGallon[STANDART] = 2.94;
  costPerGallon[REGULAR]  = 3.15;
  costPerGallon[PREMIUM]  = 3.37;
  costPerGallon[SUPER]    = 3.61;

  cout << "You have loaded " << costPerGallon.size() << " fuels" << endl;

  map<PETROL_MARK, double>::iterator mPtr = costPerGallon.begin();
  while (mPtr != costPerGallon.end()) {
    cout << "Fuel " << (int)mPtr->first << " costs $" << mPtr->second << endl;
    mPtr++;
  }

  return 0;
}

This is a good solution. Unfortunately you cannot convert from an enum to a string for displaying the name of the fuel. So it's really not much use.

A better approach would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {

  map<string, double> costPerGallon;
  costPerGallon["STANDART"] = 2.94;
  costPerGallon["REGULAR"]  = 3.15;
  costPerGallon["PREMIUM"]  = 3.37;
  costPerGallon["SUPER"]    = 3.61;

  cout << "You have loaded " << costPerGallon.size() << " fuels" << endl;

  map<string, double>::iterator mPtr = costPerGallon.begin();
  while (mPtr != costPerGallon.end()) {
    cout << "Fuel " << mPtr->first << " costs $" << mPtr->second << endl;
    mPtr++;
  }

  return 0;
}


I think you've really missed the point of an enum. It's not there to be used as an integer. It's there to allow you to assign human-readable custom values to types. Then you can switch of if-else these values to control the flow of logic within your application.



Zaita,
Fascinating code. My teacher will not understand it (blonde). Thanks very much.
Upon re-reading your original cost. Your code is a terrible mis-match of Enums and Ints.

This is how I would do your code, based on what you have supplied.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const unsigned short int PETROL_MARK_NUMBER = 4;
typedef enum PETROL_MARK {STANDART=0, REGULAR, PREMIUM, SUPER};
const float COST_PER_GALLON[PETROL_MARK_NUMBER] = {2.94, 3.15, 3.37, 3.61};
const int TotalRefuel[PETROL_MARK_NUMBER] = { 1, 2, 3, 4 };

unsigned int GetTotalRefuel(int mark) {
  return TotalRefuel[mark];
}

int main() {

  float PumpCash = 0.0;

  for (int i = (int)STANDART; i <= (int)SUPER; ++i) {
    PumpCash += GetTotalRefuel(i) * COST_PER_GALLON[i];
  }

  return 0;
}


I still prefer either of my solutions in the previous post.

I tried to achieve clearness... Bad attempt.
I would also tidy it up a little like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
typedef enum {
    STANDART = 0,
    REGULAR,
    PREMIUM,
    SUPER,
    // now you could someday add new ones without touching the mark number
    PETROL_MARK_NUMBER
};

const float costPerGallon[PETROL_MARK_NUMBER] = { 2.94, 3.15, 3.37, 3.61 };

const int totalRefuel[PETROL_MARK_NUMBER] = { 1, 2, 3, 4 };

int main() {

    float pumpCash = 0;

    for( unsigned int i = STANDART; i < PETROL_MARK_NUMBER; ++i ) {
        pumpCash += totalRefuel[i] * costPerGallon[i];
    }

    return 0;
}

Topic archived. No new replies allowed.