| | |
Use string input for enum
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jun 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jun 2007
Posts: 4
Reputation:
Solved Threads: 0
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;
}
//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;
}
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
If you need the character, just use
inputword[0] (after you have trimmed the string). If you need the number, do something like this: C++ Syntax (Toggle Plain Text)
string inputword; cout << "Enter a string and press enter: "; if(!getline(cin, inputword)) exit(1); char x = inputword[0]; if(isupper(x)) cout << "\nThis is it " << (x - 'A') << '\n'; else cout << "\nWrong / bad input." << '\n';
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- user input into a string (C++)
- string input (Java)
- Masking a string input (Python)
- struct array and enum problems (C++)
Other Threads in the C++ Forum
- Previous Thread: GUIs with c++
- Next Thread: C++ help
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






