Ok, really suck at this so far, but I've managed to code enough to get a working program. The question is for a college programming class, the question wants me to write a program that allows the user to input a character and the program returns the military phonetic alphabet equivalent. Ex: A = Alpha B = Bravo
Only problem is if I type in a lower case A, the program doesn't respond. Teacher wants us to use toupper to avoid case sensitivity, which I have read and read and can't for the life of me figure it out. This is what I have so far. Thank you in advance. He also recommended isalpha, but I don't understand how that applies to this project.
#include <iostream>
#include <ctype.h>
#include <string>
usingnamespace std;
int main ()
{
string UserInput; //Declaration of letter.
cout << "Please enter letter to receive ICAO translation: ";
cin >> UserInput;
if (UserInput == "A")
if (UserInput =="a")
cout << endl;
cout << "The letter A in ICAO translates to Alpha." << endl;
return 0;
}
Note that it takes a character as an argument and returns a value. In order to use toupper(), you will want to call the function and assign the value somewhere.
1 2
char test = 'a';
test = toupper(test);
You could assign the uppercase character to a new variable if you want, or assign it back to the same variable.
Your code captures user input in a string rather than a character, but the principal is the same. Iterate through the string and convert each character via toupper.
It's also possible to use the transform() function to convert an entire string to uppercase, though that involves using the algorithm library and may be beyond the scope of your class at this point.
Yep... Still speaking Greek. Anyone able to Barney style this? Beginners forum seems pretty advanced. Not sure how that char test takes a user's input and converts it to uppercase.
Ok, those post were pretty helpful. And I'm not saying he's speaking too advanced, I'm just saying that I'm THAT bad at this. I have a friend who does this for a living and she went super confusing on me, so I think I got an idea I'm going to play with and give it a shot. Thanks gents.
Success! Now I have one more problem. User input is now case insensitive, however, now my if statements are just popping up every possible explanation. So if I type in the letter A, which should say that A translates to Alpha, it show that it translates to Alpha, and then it pops another message saying the letter A translates to Bravo. How do I fix this?
{
if (UserInput == 'a')
cout << endl;
cout << endl;
cout << "The letter "<< UserInput <<" in ICAO translates to Alpha." << endl;
}
if (UserInput == 'b')
cout << endl;
cout << "The letter "<< UserInput <<" in ICAO translates to Bravo." << endl;
Should be :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{
if (UserInput == 'a')
{
cout << endl;
cout << endl;
cout << "The letter "<< UserInput <<" in ICAO translates to Alpha." << endl;
}}
if (UserInput == 'b')
{
cout << endl;
cout << "The letter "<< UserInput <<" in ICAO translates to Bravo." << endl;
}