c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 09:29

My last program before finals week involves inputting a string and then outputting it in phonetic form. I have to use an array to get the values, so i figured a pre dtermined value array would work like such:

string phonetic[25] = {Alpha, Bravo, Charlie, ... , Zulu};

but it seems I am still a little confused on another thing. I can get each char by using nameofstring.at(x) and then increment that to keep going in a loop, but how do I decide a value for letters? I got very confused from my online book on this matter, and it seems like it isnt just 1-26 for the alphabet, but like goes to 9 then something wierd. Can someone tell me how to get a value?

Also, I belive to get values lowered from uppercase to lower I can subtract A, but if this is wrong, please tell me now so i can say something to the professor because this quarter has been the worst programming I have don in 6 years, as far as doing a chapter a week and not getting my full potential from it.

RE: c++ array phonetic alphabet program

There are 26 letters in the alphabet.
Also, you need speech marks around each word to make it a string:

Code

string phonetic[26] = {"Alpha", "Bravo", "Charlie", ... , "Zulu"};

You could just let the compiler work out how big the array is as well:

Code

string phonetic[] = {"Alpha", "Bravo", "Charlie", ... , "Zulu"};

vegeta_man111 wrote:
but it seems I am still a little confused on another thing. I can get each char by using nameofstring.at(x) and then increment that to keep going in a loop, but how do I decide a value for letters?

What is letters? Post your code or more some detailed information please.

EDIT:
Xunxen: I know, that's why I said a-z and not a-zA-Z...
Com: Of course it's a class encasing of a C-style string, but that's the whole point. I was just explaining that putting quotes around something doesn't automatically make it a string object. And thanks for pointing out my typo, word.at indeed ;-)

RE: c++ array phonetic alphabet program

@GTA, that also goes horribly wrong if the letters are upper case, as the values range from 65-90.
it's best to check that it's between 'A' and 'Z' and if it is, subtract 'A' and check if it's between 'a' and 'z' and subtract 'a', otherwise, maybe throw an exception? you're trying to get the phonetic letters, so you don't really need numbers, just make sure they're not there.

Author

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 16:46

GTADarkDude wrote:

starofale wrote:
Also, you need speech marks around each word to make it a string:

Code

string phonetic[26] = {"Alpha", "Bravo", "Charlie", ... , "Zulu"};

Actually, with the quotes you make a null-terminated character array. Luckily the string class has a constructor with a char pointer as its argument.

Of course it makes a null terminated string, the C++ string is just a class encasing C-style strings with more functionality last I checked. The other way he'd be sending variables, you can't even tell if they exist or not.
Also, I assume that you mean word.at and not blah.at.

Xunxen wrote:
@GTA, that also goes horribly wrong if the letters are upper case, as the values range from 65-90.
it's best to check that it's between 'A' and 'Z' and if it is, subtract 'A' and check if it's between 'a' and 'z' and subtract 'a', otherwise, maybe throw an exception? you're trying to get the phonetic letters, so you don't really need numbers, just make sure they're not there.

It is also not properly portable in the case that the ranges would be different. Personally, I'd do a switch case for this with a fallthrough for upper and lower case letters.

Author

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 19:06

Personally, I'd do a switch case for this with a fallthrough for upper and lower case letters.

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 20:34

if the code is suppose to work in a similar manner as iBlaze's code then i should also use a tolower/toupper function to get all the letter in to lower or upper case and then write the switch case on that letter.

i believe that there is such a function in the ctype.h header file and if there is not then you could alway write your own that could look something like this:

RE: c++ array phonetic alphabet program

I understand everything being said so far. Thanks for your help everyone. Now, onto the letter issue. My professor says to use isalpha() to check if a letter is actually a letter. Is this correct?

Author

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 21:39

vegeta_man111 wrote:
Now, onto the letter issue. My professor says to use isalpha() to check if a letter is actually a letter. Is this correct?

What letter issue? Why would you need to use that function for a switch case approach?

Author

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 21:44

sorry, i forgot that part. This program is a revision of a switch based program and we are not allowed to use switches.

Author

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 21:54

vegeta_man111 wrote:
sorry, i forgot that part. This program is a revision of a switch based program and we are not allowed to use switches.

Oh christ... why would they do that? Why would they try to teach you something that will probably end up being effectively worse? The switch statement is specifically designed to be fast; I see absolutely no reason for not using it here.
But sigh, fine. If they insist then you may as well use the isalpha() function to check, make it lower or uppercase as per your preference and then search for that in a string of the abc, using the returned value as the index for your string array. Yes, this is really fucking convoluted in comparison and pointless when you could use a switch instead, but whatever.

Author

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 21:58

COM wrote:

vegeta_man111 wrote:
sorry, i forgot that part. This program is a revision of a switch based program and we are not allowed to use switches.

Oh christ... why would they do that? Why would they try to teach you something that will probably end up being effectively worse? The switch statement is specifically designed to be fast; I see absolutely no reason for not using it here.
But sigh, fine. If they insist then you may as well use the isalpha() function to check, make it lower or uppercase as per your preference and then search for that in a string of the abc, using the returned value as the index for your string array. Yes, this is really fucking convoluted in comparison and pointless when you could use a switch instead, but whatever.

RE: c++ array phonetic alphabet program

1. Loop < word.length(), not <=. A length of 5 means that indexes 0 up to and including 4 are usable.
2. Just so to let you know, letter doesn't change by calling toupper. So in phonetic[letter - 65], you're still using the lowercase letter (unless the letter was uppercase anyways of course).
3. Well this is just some strong advice: don't mix C and C++ I/O functions, it's messy.
4. Some other advice: x = word.length() + 1 is ugly. Just use break;, or, if you want to avoid that too, rewrite your code in such a way that you can use isalpha(letter) in the loop condition.
5. Even more advice: don't use system() calls. For one thing it's pretty slow, but even worse is that is makes your program platform dependent. "pause" is a Windows-only command.

...

Author

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 23:00

thanks for the advice. I have been trying to find a good way around using system() but I dont have a linux based computer yet so I cant test anything out. ANy suggestions?

RE: c++ array phonetic alphabet program

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 09-03-11 23:33

also, the other problems I am running into are that:
a.) when i input abcd i get output of "AAlpha - BBravo - ... etc."
b.) when I have a space it stops at it and ends, how do you think I can set it up to out something like *SPACE* and then keep going?