Solved

Help with conversion of switch control to array...

Posted on 2008-06-20
4
230 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
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now