Follow us on Twitter!
Few are those who can see with their own eyes and hear with their own hearts. - Albert Einstein
Thursday, April 24, 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: 13
Guests Online: 12
Members Online: 1

Registered Members: 82889
Newest Member: Geriztul
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Page 2 of 2 < 1 2
Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 10-03-11 01:57
Xunxen wrote:
why not just use getline(null,cin)?
then you would have to press return to continue.


it isnt working either.

I did find this from something Jim posted earlier in another of my threads I seemed to have missed (Thanks Jim!)

Code
cout << "Press any key to continue..." << endl;
cin.ignore(1,'\x10');
while(!(cin.get() > 0)) {}




Now I just need to figure out how to get it to skip over spaces and continue as well as stop saying AAlpha and BBravo and so on.




Edited by on 10-03-11 04:09
Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 10-03-11 04:41
updated! fixed the letter problem. Putchar makes it print the char so i was literally printing the uppercase letter then printing the phonetic right after it.

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 to be output in phonetic form:" << endl;
    cin >> word;

   
    for(int x = 0; x < word.length(); x++)
    {
        letter = word.at(x); // gets each letter by itself
        if (isalpha(letter)) // checks to make sure an alphabetical symbol
        {
             
             cout << phonetic[(toupper(letter)) - 65] << endl;
        }          // outputs phonetic alphabet based on alphabetical value
        else if (letter == " ");
        {
             cout << "*SPACE*" << endl;
             word = word.substr(x, word.length());
             x = 0;   
        }
        else
        {
        cout << letter << " is not a letter" << endl;
        break; // declares not a variable and ends the 'for' loop
        }
    }
    cout << "Press the ENTER key to continue..." << endl;
    cin.ignore(1,'\x10');
    while(!(cin.get() > 0)) {} // pauses at the end
    return 0;
}





started coding a little bit of the else if statement to determine if letter is a space so i can skip over it, but realized i cant compare pointers to integers, which im assuming is meaning i cant compare " " (space) to a character.


Author

RE: c++ array phonetic alphabet program

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 10-03-11 14:21
letter is a character while " " is a character array, so indeed, you can't compare them like this. You need single quotes if you want to compare to the space character: ' '. Furthermore, word will never contain any spaces. The >> operator reads until the first whitespace it encounters. (Space, newline, tab..) So when you enter "Hey there!", word contains "Hey", and " there!" remains in the input buffer. This is also why you need to call ignore() first, before calling get().
Use getline() if you want to enter a full sentence.


...
- - -
Author

RE: c++ array phonetic alphabet program

Xunxen
Member



Posts: 30
Location:
Joined: 06.03.11
Rank:
Apprentice
Posted on 10-03-11 14:24
try using ' ' instead of " ". double quotes usually mean a string, while single quotes are a char.
you could also do this:
Code
if(!(letter>='A'&&letter<='Z')){
continue;
}



assuming you convert everything to uppercase before hand, this will skip that iteration of the loop if the character is not a capital letter.
just make sure you use getline() if you want spaces.




Edited by Xunxen on 10-03-11 14:24
Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 10-03-11 16:28
it seems that when I use getline() that my pause now needs you to enter twice to exit at the end, but nonetheless it works. 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 to be output in phonetic form:" << endl;
    getline(cin, word);

   
    for(int x = 0; x < word.length(); x++)
    {
        letter = word.at(x); // gets each letter by itself
        if (isalpha(letter)) // checks to make sure an alphabetical symbol
        {
             
             cout << phonetic[(toupper(letter)) - 65] << endl;
        }          // outputs phonetic alphabet based on alphabetical value
        else if (letter == ' ')
        {
             cout << "*SPACE*" << endl; // places a space wherever one is
             word = word.substr(x, word.length());
             x = 0;   
        }
        else
        {
        cout << letter << " is not a letter" << endl;
        break; // declares not a variable and ends the 'for' loop
        }
    }
    cout << "Press the ENTER key to continue..." << endl;
    cin.ignore(1,'\x10');
    while(!(cin.get() > 0)) {} // pauses at the end
    return 0;
}






Author

RE: c++ array phonetic alphabet program

Xunxen
Member



Posts: 30
Location:
Joined: 06.03.11
Rank:
Apprentice
Posted on 11-03-11 01:21
instead of doing this:
Code
word=word.substring(x, word.length());
x=0;



wouldn't it be better to just leave the string as it is? that way you have less overhead from the substring method.


Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 11-03-11 08:27
fixed, thanks. anyone know why i still have to press enter twice at the end to close it? Maybe if I changed to a loop where I could keep inputting new strings until I type # or some symbol like it.


Author

RE: c++ array phonetic alphabet program

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 11-03-11 13:39
You can safely remove your cin.ignore() now. getline() discards the newline symbol at the end, so in this case it also empties your input buffer. And hey, surprise, you only have to press enter once now... ;-)


...
- - -
Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 11-03-11 13:47
Why use while-loop for pause? Why not:

Code
cin.sync();
cin.get();


Author

RE: c++ array phonetic alphabet program

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 11-03-11 19:00
I'd given him a bit of a crap C++ conversion from what I've used in C.

I honestly didn't know about cin.sync();, sorry about that...

Jim,
Author

RE: c++ array phonetic alphabet program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 12-03-11 00:03
Don't worry about it;)
Just thought it might come in handy.

Edit:
oh yeah, I don't know what you wanted to do with:
Code
cin.ignore();



(I only read the last page) but what cin.sync() effectively does is discard all unread characters in buffer.

Edited by on 12-03-11 00:10
Author

RE: c++ array phonetic alphabet program

Xunxen
Member



Posts: 30
Location:
Joined: 06.03.11
Rank:
Apprentice
Posted on 12-03-11 04:46
this whole project seems like it'd be easier with an STL map, then you could just dump the the current character in the string into the map as a key and get the phonetic character back.
Code
cout << phonetic[toupper(word.at(x))] << endl;



seems much neater than
Code
letter=word.at(x);
cout << phonetic[(toupper(letter)) - 65] << endl;



(I feel like that last line needs an explicit type cast, but that could just be me.)
although a map would probably defeat the purpose of the project in the first place.
also, you could use mod 26 to get the integer value of the letter (after converting to upper),but 'A' would be 13, so your phonetic array would have to start with "Alpha" in the 13th index, then "November" would be in the 0th index. it could be fun just to see how much you could confuse your teacher with that...


Page 2 of 2 < 1 2