I am trying to write a code in C , that does the following:
- If the user inputs A, the program outputs ALPHA
- If the user inputs B, the program outputs BRAVO
And so on with all the Nato Alphabet
Also the code must:
- If user inputs 0, program outputs Zero
- If user inputs something that is not uppercase letter or digit, the program outputs shou Not Recognized
Here is the whole code that I have created to complete these tasks.
# include <iostream>
using namespace std;
int main () {
// Declare the characters in respect to the alphabet
char A[] = "Alfa"; char B[] = "Bravo"; char C[] = "Charlie"; char D[] = "Delta"; char E[] = "Echo";
char F[] = "Foxtrot"; char G[] = "Golf"; char H[] = "Hotel"; char I[] = "India"; char J[] = "Juliet"; char K[] = "Kilo";
char L[] = "Lima"; char M[] = "Mike"; char N[] = "November"; char O[] = "Oscar"; char P[] = "Papa";
char Q[] = "Quebec"; char R[] = "Romeo"; char S[] = "Sierra"; char T[] = "Tango"; char U[] = "Uniform";
char V[] = "Victor"; char W[] = "Whiskey"; char X[] = "Xray"; char Y[] = "Yankee"; char Z[] = "Zulu";
// Declare the possible characters
string chosenCharacter;
// Create the terminal input message
cout << "Please insert a letter: "<< endl;
cin >> chosenCharacter;
// Declare the scenarios for output [letter = word]
if (chosenCharacter == A) {
cout << "Your received message is: " << A << endl;
}
else if (chosenCharacter == B)
{
cout << "Your received message is: " << B << endl;
}
else if (chosenCharacter == C)
{
cout << "Your received message is: " << D << endl;
}
else if (chosenCharacter == E)
{
cout << "Your received message is: " << E << endl;
}
else if (chosenCharacter == F)
{
cout << "Your received message is: " << F << endl;
}
else if (chosenCharacter == G)
{
cout << "Your received message is: " << G << endl;
}
else if (chosenCharacter == H)
{
cout << "Your received message is: " << H << endl;
}
else if (chosenCharacter == I)
{
cout << "Your received message is: " << I << endl;
}
else if (chosenCharacter == J)
{
cout << "Your received message is: " << J << endl;
}
else if (chosenCharacter == K)
{
cout << "Your received message is: " << K << endl;
}
else if (chosenCharacter == L)
{
cout << "Your received message is: " << L << endl;
}
else if (chosenCharacter == M)
{
cout << "Your received message is: " << M << endl;
}
else if (chosenCharacter == N)
{
cout << "Your received message is: " << N << endl;
}
else if (chosenCharacter == O)
{
cout << "Your received message is: " << O << endl;
}
else if (chosenCharacter == P)
{
cout << "Your received message is: " << P << endl;
}
else if (chosenCharacter == Q)
{
cout << "Your received message is: " << Q << endl;
}
else if (chosenCharacter == R)
{
cout << "Your received message is: " << R << endl;
}
else if (chosenCharacter == S)
{
cout << "Your received message is: " << S << endl;
}
else if (chosenCharacter == T)
{
cout << "Your received message is: " << T << endl;
}
else if (chosenCharacter == U)
{
cout << "Your received message is: " << U << endl;
}
else if (chosenCharacter == V)
{
cout << "Your received message is: " << V << endl;
}
else if (chosenCharacter == W)
{
cout << "Your received message is: " << W << endl;
}
else if (chosenCharacter == X)
{
cout << "Your received message is: " << X << endl;
}
else if (chosenCharacter == Y)
{
cout << "Your received message is: " << Y << endl;
}
else if (chosenCharacter == Z)
{
cout << "Your received message is: " << Z << endl;
}
// Check if the input is not a upper Case letter
else if (chosenCharacter =! isupper) {
cout << "Not Recognized" << endl;
}
// Check if the input is not a digit
else if (chosenCharacter =! isdigit) {
cout << "Not Recognized" << endl;
}
// Check if the input is zero
else if (chosenCharacter == 0) {
cout << "Zero" << endl;
}
}
Unfortunately I am struggling to check if the inputed information is not upperCase,digit or Zero as that part of the code seems to not be working as intended, as I am recieving the following error messeages.
// Check if the input is not upper Case letter
- cannot resolve overloaded function 'isupper' based on conversion to type 'bool'
// Check if the input is not a digit
- cannot resolve overloaded function 'isdigit' based on conversion to type 'bool'
//Check if the input is zero
- no match for 'operator==' (operand types are 'std::string' {aka 'std::__cxx11::basic_string'} and 'int')
I am stuck with this issue and would appreciate some help. Could you please provide some improvements for the code, so that all of the 3 functions would be working as inteded?
CodePudding user response:
There are several issues identified in your code in the comments. Notably your code of C-style strings and a std::string
to represent a single character.
The most straightforward way to solve this problem is going to be to use. std::map
to map a char
to a std::string
. A simple example:
#include <map>
#include <string>
#include <iostream>
int main() {
std::map<char, std::string> abc = {
{'A', "Alfa"}, {'B', "Bravo"}, {'C', "Charlie"},
{'D', "Delta"}, {'E', "Echo"}, {'F', "Foxtrot"},
{'G', "Golf"}, {'H', "Hotel"}, {'I', "India"},
{'J', "Juliet"}, {'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', "Xray"},
{'Y', "Yankee"}, {'Z', "Zulu"}, {'0', "Zero"}
};
char ch;
std::cin >> ch;
auto code = abc.find(ch);
if (code == abc.end())
std::cout << "Not recognized." << std::endl;
else
std::cout << code->second << std::endl;
return 0;
}
CodePudding user response:
This can be simplified if we can assume ASCII character set and that 'Z'
- 'A'
is a positive number.
char const * text[] =
{
"Alfa", "Bravo", "Charlie", "Delta", "Echo",
"Foxtrot", "Golf", "Hotel", "India", "Juliet",
"Kilo", "Lima", "Mike", "November", "Oscar",
"Papa", "Quebec", "Romeo", "Sierra", "Tango",
"Uniform", "Victor", "Whiskey", "Xray", "Yankee",
"Zulu",
};
//...
cin >> selection_letter;
selection_letter = std::toupper(selection_letter);
if (isalpha(selection_letter))
{
const unsigned index = selection_letter - 'A';
std::cout << selection_letter << ": " << text[index] << "\n";
}
//...
The zero check and numerics can be handled in another table.