Extra help

Hello again:
I posted a question on the forum a few days ago, and have run into another issue. While it is another assignment, the version I have is final and is ready to be turned in, since I have checked off all of the "requirements" for this particular assignment.

However, there is one part that I would like to figure out how to fix and incorporate an added feature into the program so that it will be more user-friendly.

The thing I am trying to find out is taking the user input, and adding "constraints" of sorts on it to limit what the user can and can not input.

The instructions are simply to design a program that takes user input in the form of a string and transform it into corresponding phrases from the ICAO phonetic alphabet. The user input I want to limit to strictly characters of the English alphabet, excluding spaces, punctuation, symbols, other language characters, and numbers. If any of these are input, I would like to ignore the user input altogether from that point and display a message prompting for the user input again.

I have simplified the language a bit for this forum, simply for the reason that the original has a lot of extra things that are visually appealing in the code and in the output, but are unnecessary for the results.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>         // Needed for input and output streams
#include <string>           // Needed for string functionality

using namespace std;

void convert(string);       // Function heading

int main()
{
    string input;

        cout << "   Through this application, you can input any single word using the English" << endl;
        cout << "   alphabet and it will display the corresponding phrase from the ICAO phonic" << endl;
        cout << "   alphabet." << endl << endl << endl << endl;

        cout << "   Please enter your choice of any single word:" << endl;
        cout << "   ";
        cin >> input;

        // Display corresponding output for a character
        cout << endl << "   The phonetic version is:" << endl << "   ";

    convert(input);
    cout << endl;
}

// Function definition
void convert (string input)
{
    int i = 0;
    char alphabet;      // Declaring character variable
    int length = input.length();

    if (!isalpha(input[i]))
    {
    // This loop will convert each character into corresponding phonetic alphabet phrases
    // until the end of the string
    while (i < length)
    {
        alphabet = input.at (i);

        /* Verifying input character with all possible characters */
        if (alphabet == 'a' || alphabet == 'A')
            cout << "Alpha ";
        else if (alphabet == 'b' || alphabet == 'B')
                 cout << "Bravo ";
        else if (alphabet == 'c' || alphabet == 'C')
                 cout << "Charlie ";
        else if (alphabet == 'd' || alphabet == 'D')
                 cout << "Delta ";
        else if (alphabet == 'e' || alphabet == 'E')
                 cout << "Echo ";
        else if (alphabet == 'f' || alphabet == 'F')
                 cout << "Foxtrot ";
        else if (alphabet == 'g' || alphabet == 'G')
                 cout << "Golf ";
        else if (alphabet == 'h' || alphabet == 'H')
                 cout << "Hotel ";
        else if (alphabet == 'i' || alphabet == 'I')
                 cout << "India ";
        else if (alphabet == 'j' || alphabet == 'J')
                 cout << "Juliet ";
        else if (alphabet == 'k' || alphabet == 'K')
                 cout << "Kilo ";
        else if (alphabet == 'l' || alphabet == 'L')
                 cout << "Lima ";
        else if (alphabet == 'm' || alphabet == 'M')
                 cout << "Mike ";
        else if (alphabet == 'n' || alphabet == 'N')
                 cout << "November ";
        else if (alphabet == 'o' || alphabet == 'O')
                 cout << "Oscar ";
        else if (alphabet == 'p' || alphabet == 'P')
                 cout << "Papa ";
        else if (alphabet == 'q' || alphabet == 'Q')
                 cout << "Quebec ";
        else if (alphabet == 'r' || alphabet == 'R')
                 cout << "Romeo ";
        else if (alphabet == 's' || alphabet == 'S')
                 cout << "Sierra ";
        else if (alphabet == 't' || alphabet == 'T')
                 cout << "Tango ";
        else if (alphabet == 'u' || alphabet == 'U')
                 cout << "Uniform ";
        else if (alphabet == 'v' || alphabet == 'V')
                 cout << "Victor ";
        else if (alphabet == 'w' || alphabet == 'W')
                 cout << "Whiskey ";
        else if (alphabet == 'x' || alphabet == 'X')
                 cout << "X-ray ";
        else if (alphabet == 'y' || alphabet == 'Y')
                 cout << "Yankee ";
        else if (alphabet == 'z' || alphabet == 'Z')
                 cout << "Zulu ";
                 i++;
    }
}
Last edited on
Untested idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void convert( string input )
{
    const char *words[] = {
        "Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf","Hotel","India",
        "Juliet","Kilo","Lima","Mike","November","Oscar","Papa","Quebec","Romeo",
        "Sierra","Tango","Uniform","Victor","Whiskey","X-ray","Yankee","Zulu"
    }
    for( size_t i = 0; i < input.size(); ++i )
        if( isalpha( input[ i ] ) )
            cout << words[ toupper( input[ i ] ) - 'A' ] << ' ';
        else
            cout << input[ i ] << ' ';
    cout << '\n';
}


Or, of course,
1
2
3
4
5
    for( auto ch : input )
        if( isalpha( ch ) )
            cout << words[ toupper( ch ) - 'A' ] << ' ';
        else
            cout << ch << ' ';

Last edited on
The user input I want to limit to strictly characters of the English alphabet, excluding spaces, punctuation, symbols, other language characters, and numbers


That is what the isalpha function does, although you don't quite have it correct in your program. And because the while is inside the if statement, the program ends if an invalid character is encountered. See my comment below about default: or else

There is an improvement you could make. Use the std::toupper function to convert the input, then you could have:

1
2
3
if (alphabet == 'A')
            cout << "Alpha ";
// .... 


Alternatively you could have a switch statement:

1
2
3
4
5
6
7
8
9
switch (alphabet) {
    case 'a':
    case 'A':
         std::cout << "Alpha ";
         break;
// ....
   default:
// ....
}


Note that the default: case does the same thing as an else: it catches bad/invalid input. It's a good thing to have.

Another way to do this is to have a std::vector of the phonetic words. Convert the char input to a number which is the subscript of the vector. Then return the correct word.

Good Luck !!
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <map>
using namespace std;


//======================================================================


map<char,string> setMap()
{
   map<char,string> code;
   code['a'] = code['A'] = "Alpha";
   code['b'] = code['B'] = "Bravo";
   code['c'] = code['C'] = "Charlie";
   code['d'] = code['D'] = "Delta";
   code['e'] = code['E'] = "Echo";
   code['f'] = code['F'] = "Foxtrot";
   code['g'] = code['G'] = "Golf";
   code['h'] = code['H'] = "Hotel";
   code['i'] = code['I'] = "India";
   code['j'] = code['J'] = "Juliet";
   code['k'] = code['K'] = "Kilo";
   code['l'] = code['L'] = "Lima";
   code['m'] = code['M'] = "Mike";
   code['n'] = code['N'] = "November";
   code['o'] = code['O'] = "Oscar";
   code['p'] = code['P'] = "Papa";
   code['q'] = code['Q'] = "Quebec";
   code['r'] = code['R'] = "Romeo";
   code['s'] = code['S'] = "Sierra";
   code['t'] = code['T'] = "Tango";
   code['u'] = code['U'] = "Uniform";
   code['v'] = code['V'] = "Victor";
   code['w'] = code['W'] = "Whiskey";
   code['x'] = code['X'] = "X-ray";
   code['y'] = code['Y'] = "Yankee";
   code['z'] = code['Z'] = "Zulu";
   return code;
}


//======================================================================


bool convert( const map<char,string> &code, string input, string &output )
{
   output.clear();
   bool firstChar = true;

   for ( char c : input )
   {
      auto it = code.find( c );
      if ( it == code.end() ) return false;

      if ( !firstChar ) output += ' ';
      firstChar = false;

      output += it->second;
   }

   return true;
}


//======================================================================


int main()
{
   map<char,string> code = setMap();
   string input, output;
   bool ok = false;

   while ( !ok )
   {
      cout << "Enter a string: ";
      getline( cin, input );
      if ( convert( code, input, output ) )
      {
         cout << "Result: " << output << '\n';
         ok = true;
      }
      else
      {
         cout << "Invalid input\n";
      }
   }
}


//====================================================================== 


Enter a string: 007
Invalid input
Enter a string: JamesBond
Result: Juliet Alpha Mike Echo Sierra Bravo Oscar November Delta
Topic archived. No new replies allowed.