?
Solved

Help with conversion of switch control to array...

Posted on 2008-06-20
4
Medium Priority
?
239 Views
Last Modified: 2011-09-20
Hello,

For a class I am in, I am trying to set up an application that outputs the phonetic alphabet equivalent of a user input letter. It needs to use an array of strings and index the array by letter positions of the alphabet, but I can only get this to work as a switch structure (see attached).

This is an assignment, not a test. Actually, it's an assignment that begs student and teacher collaboration, but high turnaround for communication in this particular online course makes collaboration more frustrating than helpful. Figured I might as well call the experts.

The book usually gives similar coding examples, and until now I have been able to decipher and bend the code to my needs; this is how I learn. However, as far as I can tell the book gives not such coding examples for this, at least none I have been able to figure out.

Any kickstart would be most appreciated. Thanks.
#include <iostream> // Standard I/O
 
using namespace std;
 
int main()
{
    // Solicit input
    char LETTER;
    cout << "Enter the CAPITALIZED letter for which you require the ICAO equivalent:" << endl;
    cin >> LETTER;
 
// Begin switch control
        switch (LETTER)
        {
        case 'A' : cout << "A = Alpha" << endl;
                   break;
        case 'B' : cout << "B = Bravo" << endl;
                   break;
        case 'C' : cout << "C = Charlie" << endl;
                   break;
        case 'D' : cout << "D = Delta" << endl;
                   break;
        case 'E' : cout << "E = Echo" << endl;
                   break;
        case 'F' : cout << "F = Foxtrot" << endl;
                   break;
        case 'G' : cout << "G = Golf" << endl;
                   break;
        case 'H' : cout << "H = Hotel" << endl;
                   break;
        case 'I' : cout << "I = India" << endl;
                   break;
        case 'J' : cout << "J = Juliet" << endl;
                   break;
        case 'K' : cout << "K = Kilo" << endl;
                   break;
        case 'L' : cout << "L = Lima" << endl;
                   break;
        case 'M' : cout << "M = Mike" << endl;
                   break;
        case 'N' : cout << "N = November" << endl;
                   break;
        case 'O' : cout << "O = Oscar" << endl;
                   break;
        case 'P' : cout << "P = Papa" << endl;
                   break;
        case 'Q' : cout << "Q = Quebec" << endl;
                   break;
        case 'R' : cout << "R = Romeo" << endl;
                   break;
        case 'S' : cout << "S = Sierra" << endl;
                   break;
        case 'T' : cout << "T = Tango" << endl;
                   break;
        case 'U' : cout << "U = Uniform" << endl;
                   break;
        case 'V' : cout << "V = Victor" << endl;
                   break;
        case 'W' : cout << "W = Whiskey" << endl;
                   break;
        case 'X' : cout << "X = X-ray" << endl;
                   break;
        case 'Y' : cout << "Y = Yankee" << endl;
                   break;
        case 'Z' : cout << "Z = Zulu" << endl;
                   break;
        }      
  return 0;
}

Open in new window

0
Comment
Question by:carnesc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 21832189
You can do that quite easily using the letter as an index into the array. Since array indices usually start a '0', you need to 'normalize' the letter entered by the user by subtracting the numeric value of 'A' (ASCII 6x), e.g.
    char* phonetic[] = {"A = Alpha","B = Bravo","C = Charlie" /*...*/};
 
    // Solicit input
    char LETTER;
    cout << "Enter the CAPITALIZED letter for which you require the ICAO equivalent:" << endl;
    cin >> LETTER;
 
    cout << phonetic[LETTER - 'A'];

Open in new window

0
 
LVL 16

Accepted Solution

by:
imladris earned 1000 total points
ID: 21832196
One way to approach this would be to use the letters numeric value as an index into an array.

That is, an ASCII 'A' is represented by decimal 65, B by 66 etc. So if you use the letters numeric equivalent, subtract 65, and use that as an index into an array of strings, you can easily get the equivalent. Something like:

String phonearray: {  "Alpha", "Bravo", "Charlie" etc. }

cin >> LETTER;

cout<< phonearray[LETTER-65];

0
 

Author Closing Comment

by:carnesc
ID: 31469204
Well, that about does it, doesn't it! :)

You both honestly would not believe the dark places into which I was taking this simple program. It is truly a testament to how tired I am right now; my mind has shut down. I could scarf a barrel of whole espresso beans and sleep like a baby. Thank you both.
0
 

Author Comment

by:carnesc
ID: 21843291
Thank you both for the help.

The following code is what I handed in for the assignment. It's a little longer, but had fun with it. I created the phonetic array of strings but also a simple string of all characters in the alphabet. The input character is used in my example as a substring search; the match position is stored in another variable; and that integer variable becomes the array instance to print out.
#include <iostream>
 
// Declare variables and array that holds phonetic alphabet
char letter;
int matchPOS;
char* icao[] = {"A is spoken \"Alpha\"","B is spoken \"Bravo\"","C is spoken \"Charlie\"","D is spoken \"Delta\"", "E is spoken \"Echo\"","F is spoken \"Foxtrot\"","G is spoken \"Golf\"","H is spoken \"Hotel\"", "I is spoken \"India\"","J is spoken \"Juliet\"","K is spoken \"Kilo\"","L is spoken \"Lima\"","M is spoken \"Mike\"","N is spoken \"November\"", "O is spoken \"Oscar\"","P is spoken \"Papa\"","Q is spoken \"Quebec\"","R is spoken \"Romeo\"","S is spoken \"Sierra\"","T is spoken \"Tango\"","U is spoken \"Uniform\"", "V is spoken \"Victor\"" ,"W is spoken \"Whiskey\"","X is spoken \"X-Ray\"","Y is spoken \"Yankee\"","Z is spoken \"Zulu\""};
 
using namespace std;
 
int main()
{
      // Solicit input
      cout << endl << "Enter the !!!CAPITALIZED!!! letter for which" << endl <<
                      "you want to know the ICAO phonetic: ";
      cin >> letter; // Store user input
      string searchSTRING; // Declare the Alphabet search string
      searchSTRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Populate it
      matchPOS = searchSTRING.find(letter); // Store the matched position
 
      // Use the matched position to pull from array
      cout << endl << icao[matchPOS] << ". Give it a try!" << endl; 
      
  return 0;
}

Open in new window

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question