Use string input for enum

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 4
Reputation: hray35 is an unknown quantity at this point 
Solved Threads: 0
hray35 hray35 is offline Offline
Newbie Poster

Use string input for enum

 
0
  #1
Jun 21st, 2007
I need help so bad. I don't understand enum and I have tried to take the code below and change it from the Switch to using an enum & an array to supposedly make it easier. I don't know how to take the user input that is string and put it to the myEnum(a, b, c, d,);

This is the code I have but no way to convert:

enum Alphabet {A = 0, B = 1, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};
Alphabet whichone;

I also have an array

string phoneticName[] = {"Alpha ", "Beta ", "Charlie ", "Delta ",
"Echo ", "Foxtrot ", "Golf ", "Hotel ", "India ", "Juliet ",
"Kilo ", "Lima ", "Mike ", "November ", "Oscar ", "Papa ",
"Quebec ", "Romeo ", "Sierra ", "Tango ", "Uniform ", "Victor ",
"Whiskey ", "X-Ray ", "Yankee","Zulu" };

This is my original code:

It take a string input and output each character Aviation Term:


#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>


using namespace std;

void GetAlpha(char);
int main()
{
string inputword;
int charcount;
string displayeachletter;
char oneletter;
int convertocharnum;
cout << "Enter a string and press enter" << endl;
cin >> inputword;
cout << "The phonetic version of this word is: " << endl;


// Loop through the inputword and decrease counter
charcount = 0;
for (charcount = inputword.length() - 1; charcount >= 0; charcount--)
{
// Determine the position number of each word
convertocharnum = inputword.length() - 1 - charcount;
cout << convertocharnum << endl;
// Create single character for each word
displayeachletter = inputword.substr(convertocharnum, 1);
// Convert the string char to a real char for the switch function
oneletter = displayeachletter[0];
// Call Switch Function
GetAlpha(oneletter);
}

return 0;
}

void GetAlpha ( /* in */ char oneletter)
{

//string letter


switch (oneletter)
{
case 'a' : cout << "A = Alpha" << endl; break;
case 'A' : cout << "A = Alpha" << endl; break;
case 'b' : cout << "B = Beta" << endl; break;
case 'B' : cout << "B = Beta" << endl; break;
case 'c' : cout << "C = Charlie" << endl; break;
case 'C' : cout << "C = Charlie" << endl; break;
case 'd' : cout << "D = Delta" << endl; break;
case 'D' : cout << "D = Delta" << endl; break;
case 'e' : cout << "E = Echo" << endl; break;
case 'E' : cout << "E = Echo" << endl; break;
case 'f' : cout << "F = Foxtrot" << endl; break;
case 'F' : cout << "F = Foxtrot" << endl; break;
case 'g' : cout << "G = Golf" << endl; break;
case 'G' : cout << "G = Golf" << endl; break;
case 'h' : cout << "H = Hotel" << endl; break;
case 'H' : cout << "H = Hotel" << endl; break;
case 'i' : cout << "I = India" << endl; break;
case 'I' : cout << "I = India" << endl; break;
case 'j' : cout << "J = Juliet" << endl; break;
case 'J' : cout << "J = Juliet" << endl; break;
case 'k' : cout << "K = Kilo" << endl; break;
case 'K' : cout << "K = Kilo" << endl; break;
case 'l' : cout << "L = Lima" << endl; break;
case 'L' : cout << "L = Lima" << endl; break;
case 'm' : cout << "M = Mike" << endl; break;
case 'M' : cout << "M = Mike" << endl; break;
case 'n' : cout << "N = November" << endl; break;
case 'N' : cout << "N = November" << endl; break;
case 'o' : cout << "O = Oscar" << endl; break;
case 'O' : cout << "O = Oscar" << endl; break;
case 'p' : cout << "P = Papa" << endl; break;
case 'P' : cout << "P = Papa" << endl; break;
case 'q' : cout << "Q = Quebec" << endl; break;
case 'Q' : cout << "Q = Quebec" << endl; break;
case 'r' : cout << "R = Romeo" << endl; break;
case 'R' : cout << "R = Romeo" << endl; break;
case 's' : cout << "S = Sierra" << endl; break;
case 'S' : cout << "S = Sierra" << endl; break;
case 't' : cout << "T = Tango" << endl; break;
case 'T' : cout << "T = Tango" << endl; break;
case 'u' : cout << "U = Uniform" << endl; break;
case 'U' : cout << "U = Uniform" << endl; break;
case 'v' : cout << "V = Victor" << endl; break;
case 'V' : cout << "V = Victor" << endl; break;
case 'w' : cout << "W = Whiskey" << endl; break;
case 'W' : cout << "W = Whiskey" << endl; break;
case 'x' : cout << "X = X-Ray" << endl; break;
case 'X' : cout << "X = X-Ray" << endl; break;
case 'y' : cout << "Y = Yankee" << endl; break;
case 'Y' : cout << "Y = Yankee" << endl; break;
case 'z' : cout << "Z = Zulu" << endl; break;
case 'Z' : cout << "Z = Zulu" << endl; break;
default : cout << "try again" << endl;
}

}

ANY help or explanation would be greatly appreciated. This is my assignment for class and I can make it work but not with enum and array.

Thanks,

Hope
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Use string input for enum

 
0
  #2
Jun 21st, 2007
Have you read your notes about using enums?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 4
Reputation: hray35 is an unknown quantity at this point 
Solved Threads: 0
hray35 hray35 is offline Offline
Newbie Poster

Re: Use string input for enum

 
0
  #3
Jun 21st, 2007
I have read and reread my notes, my book, anythig I can find on the web and searched this website. As I stated, I am confused on how to get the user input int the enum.

Hope
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Use string input for enum

 
0
  #4
Jun 21st, 2007
I can think of a way but I'm not allowed to tell you since that would be helping you cheat.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 4
Reputation: hray35 is an unknown quantity at this point 
Solved Threads: 0
hray35 hray35 is offline Offline
Newbie Poster

Re: Use string input for enum

 
0
  #5
Jun 21st, 2007
I am not asking you to help me cheat. I am asking for advice on what I am doing wrong or what I don't understand. As you see in my code, I have wrote the code one way and now I want to write it using an array, I know how to set up the array and how to call what I want from the array but that is an integer value and I am having the using type in string. I know that if I use enum and I place the letters in there I can ask for the integer value, meaning A = 1, B=2.

When I compile by convert the string to char then making that equual to my enum. It compiles but I get the ASCI value. a = 66.

You have to believe I don't want the code I want an better understanding why my code doesn't work. I plan to use C++ so cheating is never going to help me and I am not 18 I am a 40 year female who has a use for knowing how to use C++ properly

Pardon me if you think I am being smart, but I am not, I just want to understand
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 4
Reputation: hray35 is an unknown quantity at this point 
Solved Threads: 0
hray35 hray35 is offline Offline
Newbie Poster

Re: Use string input for enum

 
0
  #6
Jun 21st, 2007
This is the code that I have written already that doesn't work

//This program Hope Ray
//******************************************************************
#include <iostream>
#include <cctype>
#include <fstream> // For file I/O

using namespace std;



int main()
{
string inputword;
enum Alphabet {A = 0, B = 1, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};
Alphabet whichone;
char inputword2;
int x;
string phoneticName[] = {"Alpha ", "Beta ", "Charlie ", "Delta ",
"Echo ", "Foxtrot ", "Golf ", "Hotel ", "India ", "Juliet ",
"Kilo ", "Lima ", "Mike ", "November ", "Oscar ", "Papa ",
"Quebec ", "Romeo ", "Sierra ", "Tango ", "Uniform ", "Victor ",
"Whiskey ", "X-Ray ", "Yankee","Zulu" };


cout << "Enter a string and press enter" << endl;
cin >> inputword;
x = inputword[0];
enum Alphabet(whichone) = x;
cout << "this is it" << whichone << endl;


cout<< Alphabet(whichone) << endl;







return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Use string input for enum

 
0
  #7
Jun 22nd, 2007
Maybe this will help you in understanding what enums _can_ and _can't_ do. BTW, why would you need enums? Do you need the first character of the symbols? Do you need the number representing the symbols?

If you need the character, just use inputword[0] (after you have trimmed the string). If you need the number, do something like this:

  1. string inputword;
  2. cout << "Enter a string and press enter: ";
  3. if(!getline(cin, inputword))
  4. exit(1);
  5. char x = inputword[0];
  6. if(isupper(x))
  7. cout << "\nThis is it " << (x - 'A') << '\n';
  8. else
  9. cout << "\nWrong / bad input." << '\n';
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC