Need to test a string for certain letters in C++

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?

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. :slight_smile:

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

#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* ) {
                if ( ! isalpha ( letter* ) ) {
                        cerr << "Error: " << letter* << " 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* ) {
                //Find which letter the user has supplied and print out the corrrect callsign
                //Allow for upper and lowercase letters
                if ( letter* == 'A' || letter* == 'a' ) {
                        cout << "Alpha ";
                } else if ( letter* == 'B' || letter* == 'b' ) {
                        cout << "Bravo ";
                } else if ( letter* == 'C' || letter* == 'c' ) {
                        cout << "Charlie ";
                } else if ( letter* == 'D' || letter* == 'd' ) {
                        cout << "Delta ";
                } else if ( letter* == 'E' || letter* == 'e' ) {
                        cout << "Echo ";
                } else if ( letter* == 'F' || letter* == 'f' ) {
                        cout << "Foxtrot ";
                } else if ( letter* == 'G' || letter* == 'g' ) {
                        cout << "Golf ";
                } else if ( letter* == 'H' || letter* == 'h' ) {
                        cout << "Hotel ";
                } else if ( letter* == 'I' || letter* == 'i' ) {
                        cout << "India ";
                } else if ( letter* == 'J' || letter* == 'j' ) {
                        cout << "Juliet ";
                } else if ( letter* == 'K' || letter* == 'k' ) {
                        cout << "Kilo ";
                } else if ( letter* == 'L' || letter* == 'l' ) {
                        cout << "Lima ";
                } else if ( letter* == 'M' || letter* == 'm' ) {
                        cout << "Mike ";
                } else if ( letter* == 'N' || letter* == 'n' ) {
                        cout << "November ";
                } else if ( letter* == 'O' || letter* == 'o' ) {
                        cout << "Oscar ";
                } else if ( letter* == 'P' || letter* == 'p' ) {
                        cout << "Papa ";
                } else if ( letter* == 'Q' || letter* == 'q' ) {
                        cout << "Quebec ";
                } else if ( letter* == 'R' || letter* == 'r' ) {
                        cout << "Romeo ";
                } else if ( letter* == 'S' || letter* == 's' ) {
                        cout << "Sierra ";
                } else if ( letter* == 'T' || letter* == 't' ) {
                        cout << "Tango ";
                } else if ( letter* == 'U' || letter* == 'u' ) {
                        cout << "Uniform ";
                } else if ( letter* == 'V' || letter* == 'v' ) {
                        cout << "Victor ";
                } else if ( letter* == 'W' || letter* == 'w' ) {
                        cout << "Whiskey ";
                } else if ( letter* == 'X' || letter* == 'x' ) {
                        cout << "X-ray ";
                } else if ( letter* == 'Y' || letter* == 'y' ) {
                        cout << "Yankee ";
                } else if ( letter* == 'Z' || letter* == '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

#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* ) {
                if ( ! isalpha ( letter* ) ) {
                        cerr << "Error: " << letter* << " 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* ) {
                //Find which letter the user has supplied and print out the corrrect callsign
                //Allow for upper and lowercase letters
                if ( letter* == 'A' || letter* == 'a' ) {
                        cout << "Alpha ";
                } else if ( letter* == 'B' || letter* == 'b' ) {
                        cout << "Bravo ";
                } else if ( letter* == 'C' || letter* == 'c' ) {
                        cout << "Charlie ";
                } else if ( letter* == 'D' || letter* == 'd' ) {
                        cout << "Delta ";
                } else if ( letter* == 'E' || letter* == 'e' ) {
                        cout << "Echo ";
                } else if ( letter* == 'F' || letter* == 'f' ) {
                        cout << "Foxtrot ";
                } else if ( letter* == 'G' || letter* == 'g' ) {
                        cout << "Golf ";
                } else if ( letter* == 'H' || letter* == 'h' ) {
                        cout << "Hotel ";
                } else if ( letter* == 'I' || letter* == 'i' ) {
                        cout << "India ";
                } else if ( letter* == 'J' || letter* == 'j' ) {
                        cout << "Juliet ";
                } else if ( letter* == 'K' || letter* == 'k' ) {
                        cout << "Kilo ";
                } else if ( letter* == 'L' || letter* == 'l' ) {
                        cout << "Lima ";
                } else if ( letter* == 'M' || letter* == 'm' ) {
                        cout << "Mike ";
                } else if ( letter* == 'N' || letter* == 'n' ) {
                        cout << "November ";
                } else if ( letter* == 'O' || letter* == 'o' ) {
                        cout << "Oscar ";
                } else if ( letter* == 'P' || letter* == 'p' ) {
                        cout << "Papa ";
                } else if ( letter* == 'Q' || letter* == 'q' ) {
                        cout << "Quebec ";
                } else if ( letter* == 'R' || letter* == 'r' ) {
                        cout << "Romeo ";
                } else if ( letter* == 'S' || letter* == 's' ) {
                        cout << "Sierra ";
                } else if ( letter* == 'T' || letter* == 't' ) {
                        cout << "Tango ";
                } else if ( letter* == 'U' || letter* == 'u' ) {
                        cout << "Uniform ";
                } else if ( letter* == 'V' || letter* == 'v' ) {
                        cout << "Victor ";
                } else if ( letter* == 'W' || letter* == 'w' ) {
                        cout << "Whiskey ";
                } else if ( letter* == 'X' || letter* == 'x' ) {
                        cout << "X-ray ";
                } else if ( letter* == 'Y' || letter* == 'y' ) {
                        cout << "Yankee ";
                } else if ( letter* == 'Z' || letter* == 'z' ) {
                        cout << "Zulu ";
                }
                i++;
        }
        cout << endl;
        return;
}


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:

string alphabetLC="abcdefghijklmnopqrstuvwxyz";

   string::const_iterator cii;
   int ii;

   for(cii=alphabetLC.begin(); cii!=alphabetLC.end(); cii++)
   {
      cout << ii++ << " " << *cii << endl;
   }

You may also want to check the reference for the string class:
Linux Tutorial: - C++ String Class Tutorial

Yet another rewrite, yet another problem. This time I have to offically use arrays. I have a good start but when I compile it I get this error "invalid conversion from ‘const char*’ to ‘char*’

I don’t know of what type of converstion it is talking about, if someone here can point me to what I am doing wrong I would be greatful!


#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* ) {
        if ( ! isalpha ( letter* ) ) {
            cerr << "Error: " << letter* << " is not alphabetic, please enter something that is!" << endl;
            abort();
        }
        i++;
    }
    cout << "User input was " << letter << endl;
    int size = sizeof ( letter ) / sizeof ( letter[0] );
    int* letterPlace;
    letterPlace = new int[size=];
    for ( i=0; i<size; i++) {
        letterPlace* = 0;    // Initialize all elements to zero.
    }
    i=0;
    for ( i=0; i<size; i++) {
        switch ( letter* ) {
        case 'A':
        case 'a':
            letterPlace* = 0;
            break;
        case 'B':
        case 'b':
            letterPlace* = 1;
            break;
        case 'C':
        case 'c':
            letterPlace* = 2;
            break;
        case 'D':
        case 'd':
            letterPlace* = 3;
            break;
        case 'E':
        case 'e':
            letterPlace* = 4;
            break;
        case 'F':
        case 'f':
            letterPlace* = 5;
            break;
        case 'G':
        case 'g':
            letterPlace* = 6;
            break;
        case 'H':
        case 'h':
            letterPlace* = 7;
            break;
        case 'I':
        case 'i':
            letterPlace* = 8;
            break;
        case 'J':
        case 'j':
            letterPlace* = 9;
            break;
        case 'K':
        case 'k':
            letterPlace* = 10;
            break;
        case 'L':
        case 'l':
            letterPlace* = 11;
            break;
        case 'M':
        case 'm':
            letterPlace* = 12;
            break;
        case 'N':
        case 'n':
            letterPlace* = 13;
            break;
        case 'O':
        case 'o':
            letterPlace* = 14;
            break;
        case 'P':
        case 'p':
            letterPlace* = 15;
            break;
        case 'Q':
        case 'q':
            letterPlace* = 16;
            break;
        case 'R':
        case 'r':
            letterPlace* = 17;
            break;
        case 'S':
        case 's':
            letterPlace* = 18;
            break;
        case 'T':
        case 't':
            letterPlace* = 19;
            break;
        case 'U':
        case 'u':
            letterPlace* = 20;
            break;
        case 'V':
        case 'v':
            letterPlace* = 21;
            break;
        case 'W':
        case 'w':
            letterPlace* = 22;
            break;
        case 'X':
        case 'x':
            letterPlace* = 23;
            break;
        case 'Y':
        case 'y':
            letterPlace* = 24;
            break;
        case 'Z':
        case 'z':
            letterPlace* = 25;
            break;
        default:
            cerr << "Invalid letter given!" << endl;
            break;
        }
    }
    cout << "That is now "; //Debugging use only
    for ( i=0; i<size; i++ ) {
        cout << letterPlace* << ", ";
    }
    cout << "
"; //Debugging use only
    StringTest ( letter, size );
    delete ] letterPlace;
    return 0;
}

void StringTest ( string letterPlace, int size )
{
    int tempNumber;
    cout << "Phonetic version is: ";
    int i=0;
    char* callsigns;
    callsigns = new char[size=];
    callsigns = ( "Alpha ", "Bravo ", "Charile ", "Delta ", "Echo ", "Foxtrot ", "Golf ", "Hotel ", "India ", "Juliet", "Kilo ", "Lima ", "Mike ", "November ", "Oscar ", "Papa ", "Quebec ", "Romeo ", "Sierra ", "Tango ",
                  "Uniform ", "Victor ", "Whiskey ", "Xray ", "Yankee ", "Zulu ");
    while ( letterPlace* ) {
        tempNumber = letterPlace*;
        cout << callsigns[tempNumber];
        i++;
    }
    cout << endl;
    delete ] callsigns;
    return;
}

[/size]************************[/size]

Ok so I managed to solve that problem but in doing so I managed to make things worse

#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* ) {
        if ( ! isalpha ( letter* ) ) {
            cerr << "Error: " << letter* << " is not alphabetic, please enter something that is!" << endl;
            abort();
        }
        i++;
    }
    cout << "User input was " << letter << endl;
    int size = sizeof ( letter ); //Doesn't correctly get  the size
	cout << size << endl; //Debugging use
    int* letterPlace;
    letterPlace = new int[size=];
    for ( i=0; i<=size; i++) {
        letterPlace* = 0;    // Initialize all elements to zero.
    }
    i=0;
    for ( i=0; i<=size; i++) {
        switch ( letter* ) {
        case 'A':
        case 'a':
            letterPlace* = 0;
            break;
        case 'B':
        case 'b':
            letterPlace* = 1;
            break;
        case 'C':
        case 'c':
            letterPlace* = 2;
            break;
        case 'D':
        case 'd':
            letterPlace* = 3;
            break;
        case 'E':
        case 'e':
            letterPlace* = 4;
            break;
        case 'F':
        case 'f':
            letterPlace* = 5;
            break;
        case 'G':
        case 'g':
            letterPlace* = 6;
            break;
        case 'H':
        case 'h':
            letterPlace* = 7;
            break;
        case 'I':
        case 'i':
            letterPlace* = 8;
            break;
        case 'J':
        case 'j':
            letterPlace* = 9;
            break;
        case 'K':
        case 'k':
            letterPlace* = 10;
            break;
        case 'L':
        case 'l':
            letterPlace* = 11;
            break;
        case 'M':
        case 'm':
            letterPlace* = 12;
            break;
        case 'N':
        case 'n':
            letterPlace* = 13;
            break;
        case 'O':
        case 'o':
            letterPlace* = 14;
            break;
        case 'P':
        case 'p':
            letterPlace* = 15;
            break;
        case 'Q':
        case 'q':
            letterPlace* = 16;
            break;
        case 'R':
        case 'r':
            letterPlace* = 17;
            break;
        case 'S':
        case 's':
            letterPlace* = 18;
            break;
        case 'T':
        case 't':
            letterPlace* = 19;
            break;
        case 'U':
        case 'u':
            letterPlace* = 20;
            break;
        case 'V':
        case 'v':
            letterPlace* = 21;
            break;
        case 'W':
        case 'w':
            letterPlace* = 22;
            break;
        case 'X':
        case 'x':
            letterPlace* = 23;
            break;
        case 'Y':
        case 'y':
            letterPlace* = 24;
            break;
        case 'Z':
        case 'z':
            letterPlace* = 25;
            break;
        default:
            cerr << "Invalid letter given!" << endl;
            break;
        }
    }
    StringTest ( letter, size );
    delete ] letterPlace;
    return 0;
}

void StringTest ( string letterPlace, int size )
{
    int tempNumber;
    cout << "Phonetic version is: ";
    const char* callsigns;
    callsigns = new char[size=];
    callsigns = ( "Alpha ", "Bravo ", "Charile ", "Delta ", "Echo ", "Foxtrot ", "Golf ", "Hotel ", "India ", "Juliet", "Kilo ", "Lima ", "Mike ", "November ", "Oscar ", "Papa ", "Quebec ", "Romeo ", "Sierra ", "Tango ", "Uniform ", "Victor ", "Whiskey ", "Xray ", "Yankee ", "Zulu ");
    for ( int i=0; i<=size; i++ ) {
        tempNumber = letterPlace*;
        cout << callsigns[tempNumber];
        i++;
    }
    cout << endl;
    delete ] callsigns;
    return;
}

When I run that I get this in the output:

vendion@Big-O:~/projects/ch11ex3/build> ./ch11ex3                                                                                                                                                                                                                      
Please enter a letter or a string of text: program                                                                                                                                                                                                                     
User input was program                                                                                                                                                                                                                                                 
8                                                                                                                                                                                                                                                                      
Invalid letter given!                                                                                                                                                                                                                                                  
Invalid letter given!                                                                                                                                                                                                                                                  
Phonetic version is: Z                                                                                                                                                                                                                                                 
*** glibc detected *** ./ch11ex3: munmap_chunk(): invalid pointer: 0x0000000000401a7e ***                                                                                                                                                                              
======= Backtrace: =========                                                                                                                                                                                                                                           
/lib64/libc.so.6[0x7f24ce879c76]                                                                                                                                                                                                                                       
./ch11ex3(_Z10StringTestSsi+0xb0)[0x4016af]                                                                                                                                                                                                                            
./ch11ex3(main+0x476)[0x40158a]                                                                                                                                                                                                                                        
/lib64/libc.so.6(__libc_start_main+0xfd)[0x7f24ce825a7d]                                                                                                                                                                                                               
./ch11ex3[0x401059]                                                                                                                                                                                                                                                    
======= Memory map: ========                                                                                                                                                                                                                                           
00400000-00402000 r-xp 00000000 08:07 889                                /home/vendion/projects/ch11ex3/build/ch11ex3                                                                                                                                                  
00601000-00602000 r--p 00001000 08:07 889                                /home/vendion/projects/ch11ex3/build/ch11ex3                                                                                                                                                  
00602000-00603000 rw-p 00002000 08:07 889                                /home/vendion/projects/ch11ex3/build/ch11ex3                                                                                                                                                  
00603000-00624000 rw-p 00000000 00:00 0                                  [heap]                                                                                                                                                                                        
7f24ce807000-7f24ce958000 r-xp 00000000 08:06 262687                     /lib64/libc-2.10.1.so                                                                                                                                                                         
7f24ce958000-7f24ceb58000 ---p 00151000 08:06 262687                     /lib64/libc-2.10.1.so                                                                                                                                                                         
7f24ceb58000-7f24ceb5c000 r--p 00151000 08:06 262687                     /lib64/libc-2.10.1.so                                                                                                                                                                         
7f24ceb5c000-7f24ceb5d000 rw-p 00155000 08:06 262687                     /lib64/libc-2.10.1.so                                                                                                                                                                         
7f24ceb5d000-7f24ceb62000 rw-p 00000000 00:00 0                                                                                                                                                                                                                        
7f24ceb62000-7f24ceb78000 r-xp 00000000 08:06 262679                     /lib64/libgcc_s.so.1                                                                                                                                                                          
7f24ceb78000-7f24ced77000 ---p 00016000 08:06 262679                     /lib64/libgcc_s.so.1                                                                                                                                                                          
7f24ced77000-7f24ced78000 r--p 00015000 08:06 262679                     /lib64/libgcc_s.so.1                                                                                                                                                                          
7f24ced78000-7f24ced79000 rw-p 00016000 08:06 262679                     /lib64/libgcc_s.so.1                                                                                                                                                                          
7f24ced79000-7f24cedcd000 r-xp 00000000 08:06 262732                     /lib64/libm-2.10.1.so
7f24cedcd000-7f24cefcc000 ---p 00054000 08:06 262732                     /lib64/libm-2.10.1.so
7f24cefcc000-7f24cefcd000 r--p 00053000 08:06 262732                     /lib64/libm-2.10.1.so
7f24cefcd000-7f24cefce000 rw-p 00054000 08:06 262732                     /lib64/libm-2.10.1.so
7f24cefce000-7f24cf0be000 r-xp 00000000 08:06 3908                       /usr/lib64/libstdc++.so.6.0.12
7f24cf0be000-7f24cf2be000 ---p 000f0000 08:06 3908                       /usr/lib64/libstdc++.so.6.0.12
7f24cf2be000-7f24cf2c5000 r--p 000f0000 08:06 3908                       /usr/lib64/libstdc++.so.6.0.12
7f24cf2c5000-7f24cf2c7000 rw-p 000f7000 08:06 3908                       /usr/lib64/libstdc++.so.6.0.12
7f24cf2c7000-7f24cf2dc000 rw-p 00000000 00:00 0
7f24cf2dc000-7f24cf2fa000 r-xp 00000000 08:06 262681                     /lib64/ld-2.10.1.so
7f24cf4cc000-7f24cf4cf000 rw-p 00000000 00:00 0
7f24cf4f5000-7f24cf4f9000 rw-p 00000000 00:00 0
7f24cf4f9000-7f24cf4fa000 r--p 0001d000 08:06 262681                     /lib64/ld-2.10.1.so
7f24cf4fa000-7f24cf4fb000 rw-p 0001e000 08:06 262681                     /lib64/ld-2.10.1.so
7fff66faa000-7fff66fbf000 rw-p 00000000 00:00 0                          [stack]
7fff66fff000-7fff67000000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted
vendion@Big-O:~/projects/ch11ex3/build>

I don’t know what I did to break it so bad, can someone please help me? :’( My guess is that the problem is in how I am deleting the arrays, but I’m not sure.[/size]************************[/size]

You said

 
int size = sizeof ( letter ); //Doesn't correctly get  the size

Try this instead


int size = strlen(letter);

Another tip. If you want to create an array of integers with dynamic size, you can initialize all values to 0 with the calloc function. If you don’t want to initialize you could use malloc.


int *numbers = (int *) calloc(size,sizeof(int));
int i;
for(i=0;i<size;i++)
{
        printf("%d",*(numbers + i));
}


int *numbers = (int *) malloc(size * sizeof(int));

However I think even if you use malloc, your ints will still be initialized to 0.
Hope this helps.

When I try that change it says strlen is not declared in the scope, I believe that I have the right headers included.

My bad, I thought you were using the cstring header instead of string. Since you are using the string class you would use this:


int size = letter.size();

imterpsfan2 wrote:

>
> My bad, I thought you were using the cstring header instead of
string.
> Since you are using the string class you would use this:
>
>
> Code:
> --------------------
>
> int size = letter.size();
>
> --------------------
>
>

Thanks for that, now it compiles and with the addition of a line to
subtract one from size, size() starts counting a 1 instead of 0, I get
a new and smaller error:

Code:

vendion@Big-O:~/projects/ch11ex3/build> ./ch11ex3
Please enter a letter or a string of text: program
User input was program
6
ch11ex3: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr)
(((char *) &((av)->bins((1) - 1) * 2])) - __builtin_offsetof (struct
malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size)
>= (unsigned long)((((__builtin_offsetof (struct malloc_chunk,
fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t)))

  • 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end &
    pagemask) == 0)’ failed.
    Phonetic version is: Aborted


“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

With a few changes fixing some problems that I found that would keep it from working I now get a conversion error with my int array on line 39, but I don’t know how to pass the letterPlace array to a function without it converting. Here is my code now:

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>

using namespace std;

int* ConverttoNub ( string, int );
void StringTest ( int, 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* ) {
        if ( ! isalpha ( letter* ) ) {
            cerr << "Error: " << letter* << " is not alphabetic, please enter something that is!" << endl;
            abort();
        }
        i++;
    }
    cout << "User input was " << letter << endl;
    int size = letter.size();
    size -= 1; //Account for the fact that counting starts a 0 instead of 1
    cout << size << endl; //Debugging use
    int* letterPlace;
    letterPlace = new int[size=];
    for ( i=0; i<=size; i++) {
        letterPlace* = 0;    // Initialize all elements to zero.
    }
    letterPlace = ConverttoNub( letter, size);
    StringTest ( letterPlace, size );
    delete ] letterPlace;
    return 0;
}

int* ConverttoNub ( string letter, int size ) {
    int* letterPlace;
    letterPlace = new int[size=];
    for ( int i=0; i<=size; i++) {
        letterPlace* = 0;    // Initialize all elements to zero.
    }
    for ( int i=0; i<=size; i++) {
        switch ( letter* ) {
        case 'A':
        case 'a':
            letterPlace* = 0;
            break;
        case 'B':
        case 'b':
            letterPlace* = 1;
            break;
        case 'C':
        case 'c':
            letterPlace* = 2;
            break;
        case 'D':
        case 'd':
            letterPlace* = 3;
            break;
        case 'E':
        case 'e':
            letterPlace* = 4;
            break;
        case 'F':
        case 'f':
            letterPlace* = 5;
            break;
        case 'G':
        case 'g':
            letterPlace* = 6;
            break;
        case 'H':
        case 'h':
            letterPlace* = 7;
            break;
        case 'I':
        case 'i':
            letterPlace* = 8;
            break;
        case 'J':
        case 'j':
            letterPlace* = 9;
            break;
        case 'K':
        case 'k':
            letterPlace* = 10;
            break;
        case 'L':
        case 'l':
            letterPlace* = 11;
            break;
        case 'M':
        case 'm':
            letterPlace* = 12;
            break;
        case 'N':
        case 'n':
            letterPlace* = 13;
            break;
        case 'O':
        case 'o':
            letterPlace* = 14;
            break;
        case 'P':
        case 'p':
            letterPlace* = 15;
            break;
        case 'Q':
        case 'q':
            letterPlace* = 16;
            break;
        case 'R':
        case 'r':
            letterPlace* = 17;
            break;
        case 'S':
        case 's':
            letterPlace* = 18;
            break;
        case 'T':
        case 't':
            letterPlace* = 19;
            break;
        case 'U':
        case 'u':
            letterPlace* = 20;
            break;
        case 'V':
        case 'v':
            letterPlace* = 21;
            break;
        case 'W':
        case 'w':
            letterPlace* = 22;
            break;
        case 'X':
        case 'x':
            letterPlace* = 23;
            break;
        case 'Y':
        case 'y':
            letterPlace* = 24;
            break;
        case 'Z':
        case 'z':
            letterPlace* = 25;
            break;
        default:
            cerr << "Invalid letter given!" << endl;
            break;
        }
    }
    return letterPlace;
}

void StringTest ( int* letterPlace, int size )
{
    int tempNumber;
    cout << "Phonetic version is: ";
    const char* callsigns;
    callsigns = new char[size=];
    callsigns = ( "Alpha ", "Bravo ", "Charile ", "Delta ", "Echo ", "Foxtrot ", "Golf ", "Hotel ", "India ", "Juliet", "Kilo ", "Lima ", "Mike ", "November ", "Oscar ", "Papa ", "Quebec ", "Romeo ", "Sierra ", "Tango ", "Uniform ", "Victor ", "Whiskey ", "Xray ", "Yankee ", "Zulu ");
    for ( int i=0; i<=size; i++ ) {
        tempNumber = letterPlace*;
        cout << callsigns[tempNumber];
    }
    cout << endl;
    delete ] callsigns;
    return;
}

[/size]***********************[/size][/size]

Something like this could work:


void StringTest ( int* letterPlace, int size )
{
   int tempNumber;
    cout << "Phonetic version is: ";
    char *callsigns] = { "Alpha ", "Bravo ", "Charile ", "Delta ", "Echo ", "Foxtrot ",
    "Golf ", "Hotel ", "India ", "Juliet", "Kilo ", "Lima ", "Mike ", "November ",
    "Oscar ", "Papa ", "Quebec ", "Romeo ",
    "Sierra ", "Tango ", "Uniform ", "Victor ", "Whiskey ", "Xray ", "Yankee ", "Zulu "};
    
    for ( int i=0; i<=size; i++ )
    {
        tempNumber = letterPlace*;
        cout << callsigns[tempNumber];
    }
       
}

That 27-case statement for letters is horrible. You should do something like this:

int c = letter*;
if (isalpha(c)) {
  c = tolower(c) - 'a';
  // c will be from 0 to 25
  // convert letter to word here
} else
  cout << "Invalid letter given" << endl;

This takes advantage of the contiguous nature of the letters in the ASCII set. I don’t think you’re likely to be working in an EBCDIC environment anymore.

You’ll also need to include ctype.h to get the prototypes for isalpha() and tolower().*