C++ Enumeration from A-Z, convert to the String on cout
So I have an Enumeration from A-Z such as Enum = (A, B, C, D, E, F.... Z)
When I go into a loop, the enumeration needs to + each iteration through the loop and give the name
For ex. Loop 1:
Name = 'A'
But print 'A' and not 1
++ Enumeration(Dont know if this works or not)
Loop 2:
Name = 'B'
but print 'B' and not 2
Loop 3:
Name = 'C'
but print 'C' and not 3
I believe this needs ascii conversion? Im just not sure how to get the enumeration to + each iteration? and if I need a seperate function? I NEED to use enum as a type.
Thank you!
// C++ program to Enumeration from A-Z, convert to the String on cout
#include <iostream>
using namespace std;
// we assign A to 65, all unassigned names gets value as value of previous name + 1
// the values assigned to enum names are integral constants
// Here A = 65, B=66, C= 67, ...., Z=90
enum Letter
{
A=65,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
};
int main()
{
int x =A; // since enum values are integral constants, we can use int type variable to loop over the names of enum
// loop to print the name (letter)
for(;x<=Z;x++)
cout<<(char)(x)<<endl; // we cast the integer to char while displaying
return 0;
}
//end of program
Output:
Get Answers For Free
Most questions answered within 1 hours.