Solved

Help with conversion of switch control to array...

Posted on 2008-06-20
4
231 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
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

21 Experts available now in Live!

Get 1:1 Help Now