Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with conversion of switch control to array...

Posted on 2008-06-20
4
Medium Priority
?
248 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 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

916 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