:confused:I got this assignment to write a program using the switch statement or the functional decomposition, that inputs a letter and outputs thr corresponding international Alpabet.

A Alpha
B Bravo
C Charlie

so on and so on

Is there anybody that can help me with this program?

Dani AI

Generated

A concise, maintainable approach is to separate the mapping (data) from the logic (function) rather than hard‑coding 26 switch cases. That follows the requested "functional decomposition" and keeps the program easy to read and change. This complements 's suggestion (switch works) and 's idea (parallel data) by showing a cleaner data→lookup pattern. 's pointer to read a char is already covered by the sample function below.

#include <iostream>
#include <map>
#include <cctype>
#include <string>

static const std::map<char, std::string> NATO = {
  {'A',"Alpha"},{'B',"Bravo"},{'C',"Charlie"},{'D',"Delta"},{'E',"Echo"},
  {'F',"Foxtrot"},{'G',"Golf"},{'H',"Hotel"},{'I',"India"},{'J',"Juliett"},
  {'K',"Kilo"},{'L',"Lima"},{'M',"Mike"},{'N',"November"},{'O',"Oscar"},
  {'P',"Papa"},{'Q',"Quebec"},{'R',"Romeo"},{'S',"Sierra"},{'T',"Tango"},
  {'U',"Uniform"},{'V',"Victor"},{'W',"Whiskey"},{'X',"X-ray"},{'Y',"Yankee"},
  {'Z',"Zulu"}
};

std::string phonetic(char ch){
  unsigned char uch = static_cast<unsigned char>(ch);
  char upper = static_cast<char>(std::toupper(uch));
  auto it = NATO.find(upper);
  return (it != NATO.end()) ? it->second : std::string();
}

int main(){
  char c;
  std::cout << "Enter a letter: ";
  if(!(std::cin >> c) || !std::isalpha(static_cast<unsigned char>(c))) {
    std::cout << "Input is not a letter\n";
    return 1;
  }
  std::cout << phonetic(c) << '\n';
  return 0;
}

Note about the Visual C++ error reported by (fatal error C1010): that usually means the project expects a precompiled header. Either (a) add the precompiled header include as the first include (typical name is "stdafx.h" or "pch.h" depending on the VS version), or (b) change the project settings to disable precompiled headers so a plain source file will compile. Also ensure standard headers (<iostream>, etc.) are present and that the source file is inside the correct project.

Extra tips: always cast char to unsigned char before calling toupper/tolower to avoid undefined behavior on signed chars; validation of non-letter input prevents crashes; mapping via a table or std::map is easier to maintain than 26 switch cases. This keeps the program compact and fits the assignment's functional decomposition option.

Recommended Answers

All 11 Replies

switch(value)
{
  case possibleValue1:
    // do something
    break;
  case possibleValue2:
    // do something
    break;
  /* ... */
  default: // value wasn't any of possibleValue[1,n]
    // do something
}

Post some code, or some sort of attempt if you want more help ;)

what do you mean by do something is that the alpabet word like Alpha
for the A character if it was entered?

use something like this:

switch(tolower(letter))
{
  case 'a':
    cout << "Alpha"; // or return, if this is just a function
    break; // don't need this if you use return above

and repeat for all the letters.

That's icky, but it's a start.

How about you post an attempt, wheelz?

Member Avatar for Member #46692

One quick solution would be to create a parallel array.

array one =
[ "a",
  "b",
 
]
 
array two =
[ "alpha",
  "bravo",
]

Then you match each by their corresponding index offset.

:confused:I got this assignment to write a program using the switch statement or the functional decomposition, that inputs a letter and outputs thr corresponding international Alpabet.

A Alpha
B Bravo
C Charlie

so on and so on

Is there anybody that can help me with this program?

This is what I have so far but it still doesn't work! I don't have a clue of what or how I am to start this program:confused: I don't even know how to put the code into this chat box so I just attached the file will that work??

Add this before switch

char letter;
cin>>letter;

I add The
char letter;
cin>>letter;

and now I have a fatal error C1010 unexpected end of file
while looking for precompiled header directive

Member Avatar for Member #46692

I add The
char letter;
cin>>letter;

and now I have a fatal error C1010 unexpected end of file
while looking for precompiled header directive

Did you change anything else?

not that I know of!!!!

I must be really screwing this up bad A.....

That error looks like you've got a Visual C++ project expecting a precompiled header (VC++ usually calls it "StdAfx.h" or something, IIRC). I havent used VC++ in a long time, but you'll probably have to recreate your project to not expect that. The code seems to work fine for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.