|
||||||
| Forums FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
On Sat, 31 Oct 2009 18:36:01 +0000, vendion wrote:
> I need to write a C++ program that takes input from the user, and test > what letters are in that string. Depending on what letters are in the > string determines the output of the program. For example if the user > enters "program" then my program should output "Papa Romeo Oscar Golf > Romeo Alpha Mike". I have no clue how I would even do this kind of a > test, and I guess I'm asking Google wrong because every hit I get is > talking about using isalpha from cctype. Can someone point me in the > right direction with this? Remember that in C and C++, strings are arrays, and you can iterate through the string using a loop and perform functions on those values based on some sort of lookup table. (Since this *sounds* like a homework assignment, I'm not going to provide a direct answer, but this should point you in the right direction). Jim -- Jim Henderson openSUSE Forums Moderator |
|
||||
|
Jim Henderson wrote:
> On Sat, 31 Oct 2009 18:36:01 +0000, vendion wrote: > >> I need to write a C++ program that takes input from the user, and test >> what letters are in that string. Depending on what letters are in the >> string determines the output of the program. For example if the user >> enters "program" then my program should output "Papa Romeo Oscar Golf >> Romeo Alpha Mike". I have no clue how I would even do this kind of a >> test, and I guess I'm asking Google wrong because every hit I get is >> talking about using isalpha from cctype. Can someone point me in the >> right direction with this? > > Remember that in C and C++, strings are arrays, and you can iterate > through the string using a loop and perform functions on those values > based on some sort of lookup table. > > (Since this *sounds* like a homework assignment, I'm not going to provide > a direct answer, but this should point you in the right direction). > > Jim Thanks for that, I guess I should have asked for no telling me exactly how to do it. Ok so this is what it seems like to me, it is just a matter of determining the length of the string, and using a loop (maybe a while loop) to test the values of the string (each individual letter). -- "We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper |
|
|||
|
On Sat, 31 Oct 2009 19:23:27 +0000, Adam Jimerson wrote:
> Ok so this is what it seems like to me, it is just a matter of > determining the length of the string, and using a loop (maybe a while > loop) to test the values of the string (each individual letter). That sounds like a reasonable approach to me. :-) Jim -- Jim Henderson openSUSE Forums Moderator |
|
||||
|
Ok before I go and submit this, now that I figured out how arrays
worked (didn't cover them yet) I would like to know if someone would be kind enough to look over my code and let me know if there is something that can be done better or any other kind of suggestions Code:
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>
using namespace std;
void StringTest ( string, int );
int main()
{
string letter; //Use to store the user supplied letter in
cout << "Please enter a letter or a string of text: "; //Print out the prompt
cin >> letter; //Get Letter from the user
int i=0;
while ( letter[i] ) {
if ( ! isalpha ( letter[i] ) ) {
cerr << "Error: " << letter[i] << " is not alphabetic, please enter something that is!" << endl;
abort();
}
i++;
}
int size = sizeof ( letter ) / sizeof ( letter[0] );
StringTest ( letter, size );
return 0;
}
void StringTest ( string letter, int size )
{
cout << "Phonetic version is: ";
int i=0;
while ( letter[i] ) {
//Find which letter the user has supplied and print out the corrrect callsign
//Allow for upper and lowercase letters
if ( letter[i] == 'A' || letter[i] == 'a' ) {
cout << "Alpha ";
} else if ( letter[i] == 'B' || letter[i] == 'b' ) {
cout << "Bravo ";
} else if ( letter[i] == 'C' || letter[i] == 'c' ) {
cout << "Charlie ";
} else if ( letter[i] == 'D' || letter[i] == 'd' ) {
cout << "Delta ";
} else if ( letter[i] == 'E' || letter[i] == 'e' ) {
cout << "Echo ";
} else if ( letter[i] == 'F' || letter[i] == 'f' ) {
cout << "Foxtrot ";
} else if ( letter[i] == 'G' || letter[i] == 'g' ) {
cout << "Golf ";
} else if ( letter[i] == 'H' || letter[i] == 'h' ) {
cout << "Hotel ";
} else if ( letter[i] == 'I' || letter[i] == 'i' ) {
cout << "India ";
} else if ( letter[i] == 'J' || letter[i] == 'j' ) {
cout << "Juliet ";
} else if ( letter[i] == 'K' || letter[i] == 'k' ) {
cout << "Kilo ";
} else if ( letter[i] == 'L' || letter[i] == 'l' ) {
cout << "Lima ";
} else if ( letter[i] == 'M' || letter[i] == 'm' ) {
cout << "Mike ";
} else if ( letter[i] == 'N' || letter[i] == 'n' ) {
cout << "November ";
} else if ( letter[i] == 'O' || letter[i] == 'o' ) {
cout << "Oscar ";
} else if ( letter[i] == 'P' || letter[i] == 'p' ) {
cout << "Papa ";
} else if ( letter[i] == 'Q' || letter[i] == 'q' ) {
cout << "Quebec ";
} else if ( letter[i] == 'R' || letter[i] == 'r' ) {
cout << "Romeo ";
} else if ( letter[i] == 'S' || letter[i] == 's' ) {
cout << "Sierra ";
} else if ( letter[i] == 'T' || letter[i] == 't' ) {
cout << "Tango ";
} else if ( letter[i] == 'U' || letter[i] == 'u' ) {
cout << "Uniform ";
} else if ( letter[i] == 'V' || letter[i] == 'v' ) {
cout << "Victor ";
} else if ( letter[i] == 'W' || letter[i] == 'w' ) {
cout << "Whiskey ";
} else if ( letter[i] == 'X' || letter[i] == 'x' ) {
cout << "X-ray ";
} else if ( letter[i] == 'Y' || letter[i] == 'y' ) {
cout << "Yankee ";
} else if ( letter[i] == 'Z' || letter[i] == 'z' ) {
cout << "Zulu ";
}
i++;
}
cout << endl;
return;
}
"We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper |
|
||||
|
EDIT: I overlooked the fact that I left in a int value from when I was thinking about using a for loop I have removed them now
Code:
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>
using namespace std;
void StringTest ( string );
int main()
{
string letter; //Use to store the user supplied letter in
cout << "Please enter a letter or a string of text: "; //Print out the prompt
cin >> letter; //Get Letter from the user
int i=0;
while ( letter[i] ) {
if ( ! isalpha ( letter[i] ) ) {
cerr << "Error: " << letter[i] << " is not alphabetic, please enter something that is!" << endl;
abort();
}
i++;
}
StringTest ( letter );
return 0;
}
void StringTest ( string letter )
{
cout << "Phonetic version is: ";
int i=0;
while ( letter[i] ) {
//Find which letter the user has supplied and print out the corrrect callsign
//Allow for upper and lowercase letters
if ( letter[i] == 'A' || letter[i] == 'a' ) {
cout << "Alpha ";
} else if ( letter[i] == 'B' || letter[i] == 'b' ) {
cout << "Bravo ";
} else if ( letter[i] == 'C' || letter[i] == 'c' ) {
cout << "Charlie ";
} else if ( letter[i] == 'D' || letter[i] == 'd' ) {
cout << "Delta ";
} else if ( letter[i] == 'E' || letter[i] == 'e' ) {
cout << "Echo ";
} else if ( letter[i] == 'F' || letter[i] == 'f' ) {
cout << "Foxtrot ";
} else if ( letter[i] == 'G' || letter[i] == 'g' ) {
cout << "Golf ";
} else if ( letter[i] == 'H' || letter[i] == 'h' ) {
cout << "Hotel ";
} else if ( letter[i] == 'I' || letter[i] == 'i' ) {
cout << "India ";
} else if ( letter[i] == 'J' || letter[i] == 'j' ) {
cout << "Juliet ";
} else if ( letter[i] == 'K' || letter[i] == 'k' ) {
cout << "Kilo ";
} else if ( letter[i] == 'L' || letter[i] == 'l' ) {
cout << "Lima ";
} else if ( letter[i] == 'M' || letter[i] == 'm' ) {
cout << "Mike ";
} else if ( letter[i] == 'N' || letter[i] == 'n' ) {
cout << "November ";
} else if ( letter[i] == 'O' || letter[i] == 'o' ) {
cout << "Oscar ";
} else if ( letter[i] == 'P' || letter[i] == 'p' ) {
cout << "Papa ";
} else if ( letter[i] == 'Q' || letter[i] == 'q' ) {
cout << "Quebec ";
} else if ( letter[i] == 'R' || letter[i] == 'r' ) {
cout << "Romeo ";
} else if ( letter[i] == 'S' || letter[i] == 's' ) {
cout << "Sierra ";
} else if ( letter[i] == 'T' || letter[i] == 't' ) {
cout << "Tango ";
} else if ( letter[i] == 'U' || letter[i] == 'u' ) {
cout << "Uniform ";
} else if ( letter[i] == 'V' || letter[i] == 'v' ) {
cout << "Victor ";
} else if ( letter[i] == 'W' || letter[i] == 'w' ) {
cout << "Whiskey ";
} else if ( letter[i] == 'X' || letter[i] == 'x' ) {
cout << "X-ray ";
} else if ( letter[i] == 'Y' || letter[i] == 'y' ) {
cout << "Yankee ";
} else if ( letter[i] == 'Z' || letter[i] == 'z' ) {
cout << "Zulu ";
}
i++;
}
cout << endl;
return;
}
__________________
"We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper |
|
|||
|
On Sun, 01 Nov 2009 20:12:55 +0000, Adam Jimerson wrote:
> be kind enough to look over my code and let me know if there is > something that can be done better or any other kind of suggestions That you haven't covered arrays yet means you won't have gotten into the use of arrays of strings, either - but that is something that you might investigate for the output. You also could simplify your if block (cut the number of comparisons in half) with a simple modification, and probably also look into a different way of doing the logic than using if blocks (there's another selection option that would be more efficient). But what you've written should get the job done based on your initial description. Having done ACM programming contests in the past, you may (though it's not part of the assignment, it seems) look at exception handling and validation of the input as well. For example, if someone puts a "." in the input string. (For ACM programming contests, that's something that's often done to trip programs up.) -- Jim Henderson openSUSE Forums Moderator |
|
||||
|
Jim Henderson wrote:
> On Sun, 01 Nov 2009 20:12:55 +0000, Adam Jimerson wrote: > >> be kind enough to look over my code and let me know if there is >> something that can be done better or any other kind of suggestions > > That you haven't covered arrays yet means you won't have gotten into the > use of arrays of strings, either - but that is something that you might > investigate for the output. You also could simplify your if block (cut > the number of comparisons in half) with a simple modification, and > probably also look into a different way of doing the logic than using if > blocks (there's another selection option that would be more efficient). > Are you talking about switch statements? If so then yes I have considered using it, but all this assignment basically is a rewrite of an earlier assignment. -- "We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper |
|
|||
|
On Mon, 02 Nov 2009 05:24:16 +0000, Adam Jimerson wrote:
> Are you talking about switch statements? If so then yes I have > considered using it, but all this assignment basically is a rewrite of > an earlier assignment. That is what I was thinking, along with using a case conversion to simplify comparisons. Jim -- Jim Henderson openSUSE Forums Moderator |
|
|||
|
Thats what I would recommend, convert the entire string to upper using std::transform.
I would also recommend that instead of using a while statement that you use the string class iterator: Code:
string alphabetLC="abcdefghijklmnopqrstuvwxyz";
string::const_iterator cii;
int ii;
for(cii=alphabetLC.begin(); cii!=alphabetLC.end(); cii++)
{
cout << ii++ << " " << *cii << endl;
}
Linux Tutorial: - C++ String Class Tutorial |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|