enumerations

hello everyone!i have some problem using enumerations.
suppose we have

enum fruit{apple,banana,mango};
main()
{
fruit a=apple;
cout<<a;

the problem is that compiler understands apple as 0,banana as 1 and mango as 2.
the output here is 0
how can we output "apple" using this enumeration..

Than don't use enumeration, use pointer or a string.

like this:

1
2
char *ptr = "Apple";
std::cout << ptr;

no i want to do it using enumeration.
No, see, you've got it backwards. Your brain must work within the framework of the C++ language, not the other way around.

Try overloading the insertion operator with a fruit:
1
2
3
4
5
6
7
8
9
10
11
12
13
enum fruit { apple, banana, mango };

const char* fruit_names[] =
  {
  "apple",
  "banana",
  "mango"
  };

ostream& operator << ( ostream& outs, fruit f )
  {
  return outs << fruit_names[ f ];
  }
The solution of @Duoas is correct but this is a little bit simpler.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

using namespace std;

int main()
{
	enum fruit { apple, banana, mango };

	string fruits[3];
	fruits[0] = "Apple";
	fruits[1] = "Banana";
	fruits[2] = "Mango";

	fruit a = apple;

	cout << fruits[a] << endl;

	return 0;
}

i am using borland c++ compiler and both the solutions given here donot execute..
Try using #include<iostream.h> instead of #include<iostream>
and remove the using namespace std;
A better idea would be to update your compiler to one that isn't wretchedly out of date.

No point in learning a language that no longer exists
@ Mohamad Fouad,i tried it in that way,three errors came
undefined symbol 'string'
statement missing ;
undefined symbol 'fruits'
'a' is assigned a value that is never used

1st and 2nd point to the line string[fruits];
3rd error in line fruits[0]= "apple";
you need to #include <string> to use string.
The above code works fine on my G++ compiler.
@Mohamad Fouad..i dont use g++ compiler i am using borland c++ 5.02 compiler
Topic archived. No new replies allowed.