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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#inlcude <iostream>
#include <cctype>
using namespace std;
int main() {
//I know that the numbers '9' and '7' have four letters, but I'd rather not create a
//two-dimensional array with the dimensions of 8 * 4 and waste space
char Number_Letter[8][3] = {{'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}, {'J', 'K', 'L'}, {'M', 'N', 'O'}, {'P', 'Q', 'R'}, {'T', 'U', 'V'}, {'W', 'X', 'Y'}};
//We have a to initialize the char array "Number_Name" with 8 elements since
// the \0 terminator is stored in the last element
char Number_Name[8];
int Final_Number[7];
cout << "Input a name: ";
cin >> Number_Name;
//We have to capitalize the char array "Number_Name" to be able to make comparisons
//with the "Number_Letter" array. The "toupper" function is defined in the cctype library.
Number_Name = toupper(Number_Name);
for (int Index = 0, Name = 0; Index < 8, Name < 7; Index++) {
for (int Letter = 0; Letter < 3; Letter++) {
if (Number_Name[Name] == Number_Letter[Index][Letter]) {
Final_Number[Name] = Index + 2;
Name++;
Letter = 0;
}
if (Number_Name[Name] == 'S') {
Final_Name[Name] = 7;
Name++;
Letter = 0;
}
if (Number_Name[Name] == 'Z')
Final_Name[Name] = 9;
Name++;
Letter = 0;
}
}
}
cout << "Your number is: " << Final_Number << endl;
cin.get();
return 0;
}
|