Follow us on Twitter!
It is the path of least resistance that makes rivers and men crooked. - Bj Palmer
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 18
Guests Online: 15
Members Online: 3

Registered Members: 82886
Newest Member: The Slummy
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Page 1 of 2 1 2 >
Author

c++ array phonetic alphabet program


Member

Your avatar

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.

Sorry for any stupidity in my writing, as it is 4am. Wink

thanks in advance




Edited by on 09-03-11 09:32
Author

RE: c++ array phonetic alphabet program

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 09-03-11 12:48
A char is represented as it's ASCII value: an integer. Look at http://asciitable.com/ and see why subtracting 'A' makes sense.


...
- - -
Author

RE: c++ array phonetic alphabet program

starofale
Member



Posts: 218
Location: England
Joined: 05.12.07
Rank:
Moderate
Posted on 09-03-11 14:18
vegeta_man111 wrote:
string phonetic[25] = {Alpha, Bravo, Charlie, ... , Zulu};

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.


Try a new search engine
Author

RE: c++ array phonetic alphabet program

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 09-03-11 16:25
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.

OP, this is what you want, right?
Code
string phonetic[26] = {"Alpha","Bravo",...,"Zulu"};
string word = "blah";
for(int i = 0; i < word.length(); ++i)
    cout << phonetic[blah.at(i)-96] << endl;



This goes horribly wrong when word contains non-[a-z] characters though.

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 ;-)


...

Edited by GTADarkDude on 09-03-11 22:45
- - -
Author

RE: c++ array phonetic alphabet program

Xunxen
Member



Posts: 30
Location:
Joined: 06.03.11
Rank:
Apprentice
Posted on 09-03-11 16:36
@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

Your avatar

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.

OP, this is what you want, right?
Code
string phonetic[26] = {"Alpha","Bravo",...,"Zulu"};
string word = "blah";
for(int i = 0; i < word.length(); ++i)
    cout << phonetic[blah.at(i)-96] << endl;



This goes horribly wrong when word contains non-[a-z] characters though.

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

Your avatar

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.


indeed

Code

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    string word, phonetic[] = { "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" };
   
    if(argc == 2) {
         word = argv[1];
    } else return(-1);
   
    for(int x = 0; x < word.length(); x++)
    {
         switch(word.at(x))
         {
              case 'a':
              case 'A': cout << phonetic[0] << endl; break;
              case 'b':
              case 'B': cout << phonetic[1] << endl; break;
              //etc
              default: cout << "Invalid character" << endl; break;
         }
    }
   
    return(0);
}






Code

[me@local ~]# g++ -o h h.cpp
[me@local ~]# ./h aAbBz
Alpha
Alpha
Bravo
Bravo
Invalid character






Author

RE: c++ array phonetic alphabet program


Member

Your avatar

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:

Code

char toUpper(char letter)
{
      if(letter >= 'a' && letter <= 'z')
            return (letter - ('a' - 'A'));
      return (letter)
}





this way if the is in lower case it gets in to upper and if it is in anything but lower case it just returns it's current value.

and if you had problems with understanding why you subtract 'A' from the letter that you currenently have is because you are going to end up in the right array index.

for exampel
char hello[] = {'A', 'B', 'C'.......'Z'};
// hello[0] == 'A' hello[1] == 'B' ... hello[26] == 'Z'
// your letter is for exampel D, d in ascii is 67(A)+4 = 71
// so when you write hello[letter - 'A'] what you really is writing is
// hello[71 - 67] == hello[3] which is the index position for the D letter

hope this clarifies something instead of just making it worse.

// shazrah;)

Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 09-03-11 21:26
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

Your avatar

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

Your avatar

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

Your avatar

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

Your avatar

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.


I agree and cant wait to do the quarterly survey.


Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 09-03-11 22:40
this is what I have so far, but it has a runtime error.

Code


// Chapter X Assignment X
// Corey Hartshorn

#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;

int main ()
{
    string phonetic[] = { "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" };
    string word;
    char letter;
   
   
   
    cout <<"Input a word or sentence:" << endl;
    cin >> word;

   
    for(int x = 0; x <= word.length(); x++)
    {
        letter = word.at(x);
        if (isalpha(letter))
        {
             putchar (toupper(letter));
             cout << phonetic[letter - 65] << " - ";
        }
        else
        {
        cout << letter << " is not a letter" << endl;
        x = word.length() + 1;
        }
    }
    system("pause");
    return 0;
}
//******************************************************************************








Author

RE: c++ array phonetic alphabet program

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 09-03-11 22:55
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

Your avatar

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?

updated code:

Code


// Chapter X Assignment X
// Corey Hartshorn

#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;

int main ()
{
    string phonetic[] = { "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" };
    string word;
    char letter;
   
   
   
    cout <<"Input a word or sentence:" << endl;
    cin >> word;

   
    for(int x = 0; x < word.length(); x++)
    {
        letter = word.at(x);
        if (isalpha(letter))
        {
             
             cout << phonetic[putchar (toupper(letter)) - 65] << " - ";
        }
        else
        {
        cout << letter << " is not a letter" << endl;
        break;
        }
    }
    system("pause");
    return 0;
}
//******************************************************************************










Edited by on 09-03-11 23:06
Author

RE: c++ array phonetic alphabet program


Member

Your avatar

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?




Edited by on 09-03-11 23:35
Author

RE: c++ array phonetic alphabet program

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 10-03-11 00:30
To "pause", like DOS does, just waiting for keyboard input:

C
Code

printf("Press any key to [something]...\n");
while(!(getch() > 0)) {}





C++
Code

cout << "Press any key to [something]..." << endl;
while(!(cin.get() > 0)) {}





I am not sure if this is really the most optimal solution but at least reduces the dependancy on Shell fuctions/DOS functions.

Hope that helps...

Jim,
Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 10-03-11 01:13
j4m32 wrote:
To "pause", like DOS does, just waiting for keyboard input:

C
Code

printf("Press any key to [something]...\n");
while(!(getch() > 0)) {}





C++
Code

cout << "Press any key to [something]..." << endl;
while(!(cin.get() > 0)) {}





I am not sure if this is really the most optimal solution but at least reduces the dependancy on Shell fuctions/DOS functions.

Hope that helps...

Jim,


seems like this wont work for me.


Author

RE: c++ array phonetic alphabet program

Xunxen
Member



Posts: 30
Location:
Joined: 06.03.11
Rank:
Apprentice
Posted on 10-03-11 01:53
why not just use getline(null,cin)?
then you would have to press return to continue.




Edited by Xunxen on 10-03-11 01:55
Page 1 of 2 1 2 >