Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,018 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,464 people online right now. Registration is fast and FREE... Join Now!




Array of Strings

 
Reply to this topicStart new topic

Array of Strings

eadams20
2 Dec, 2006 - 08:50 AM
Post #1

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions
CODE

#include <iostream>
#include <string>

using namespace std;

int main()
{
int input;
input = 0;
std::string ICAO[26] = { "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" };

cout << "Input a word to return in ICAO form: " << endl;

string icaoword;
cin >> icaoword;

for(int i=0; i < icaoword.length(); ++i)
{
    std::string strOutput;
    char cLetter;
    cLetter = icaoword[i];
    cLetter = toupper(cLetter); //convert to uppercase, so this works for both uppercase and lowercase letters
    strOutput = ICAO[cLetter-'A'];
    cout << strOutput << endl;
}
cin.get(); cin.get();
return 0;
}



Here is my assignment. "You were first asked ot write a program that inputs a string and then ouput the corresponding words in the international Civil Aviation Organization alphabet that would be used to spell it out pheontically. For that program you should have used a large switch statement. Rewrite that program using an array of strings to hold the words of the alphabet, and index the array by the positions of the letters of the alphabet. By using an index with semantic content, you can avoid the need for the case statement. Be sure that you don't try to index into the array using non-alphabetic characters, as that will result in an out-of-bounds access. "

thanks in advace smile.gif

My question is did I do all that I was asked.

Emily
User is offlineProfile CardPM
+Quote Post

cipherence
RE: Array Of Strings
2 Dec, 2006 - 08:53 AM
Post #2

D.I.C Regular
Group Icon

Joined: 1 Apr, 2006
Posts: 260



Thanked: 1 times
Dream Kudos: 650
My Contributions
you might want to declare icaoword next to all the other variables. but other then that, compile it, try it out, and see if it's primo.
User is offlineProfile CardPM
+Quote Post

eadams20
RE: Array Of Strings
2 Dec, 2006 - 08:54 AM
Post #3

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions
also i am getting one warning
c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Assignment11\assignment12.cpp(20): warning C4018: '<' : signed/unsigned mismatch


QUOTE(eadams20 @ 2 Dec, 2006 - 09:50 AM) *

CODE

#include <iostream>
#include <string>

using namespace std;

int main()
{
int input;
input = 0;
std::string ICAO[26] = { "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" };

cout << "Input a word to return in ICAO form: " << endl;

string icaoword;
cin >> icaoword;

for(int i=0; i < icaoword.length(); ++i)
{
    std::string strOutput;
    char cLetter;
    cLetter = icaoword[i];
    cLetter = toupper(cLetter); //convert to uppercase, so this works for both uppercase and lowercase letters
    strOutput = ICAO[cLetter-'A'];
    cout << strOutput << endl;
}
cin.get(); cin.get();
return 0;
}



Here is my assignment. "You were first asked ot write a program that inputs a string and then ouput the corresponding words in the international Civil Aviation Organization alphabet that would be used to spell it out pheontically. For that program you should have used a large switch statement. Rewrite that program using an array of strings to hold the words of the alphabet, and index the array by the positions of the letters of the alphabet. By using an index with semantic content, you can avoid the need for the case statement. Be sure that you don't try to index into the array using non-alphabetic characters, as that will result in an out-of-bounds access. "

thanks in advace smile.gif

My question is did I do all that I was asked.

Emily


User is offlineProfile CardPM
+Quote Post

BitByte
RE: Array Of Strings
2 Dec, 2006 - 09:33 AM
Post #4

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
Inside your for loop, change

CODE
for ( int i = 0; i < icoaword; I++ )


to

CODE
std::string::size_type i = 0; i < icoaword.length(); i++ )


That will get rid of the warning/error
string::size_type is the correct one to use as it is guaranteed to hold the length of any string.

This post has been edited by BitByte: 2 Dec, 2006 - 09:34 AM
User is offlineProfile CardPM
+Quote Post

eadams20
RE: Array Of Strings
2 Dec, 2006 - 11:11 AM
Post #5

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions
thank you both smile.gif

Emily
User is offlineProfile CardPM
+Quote Post

eadams20
RE: Array Of Strings
5 Dec, 2006 - 12:03 PM
Post #6

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions
CODE

#include <iostream>
#include <string>

using namespace std;

int main()
{
int input;
string icaoword;
input = 0;
std::string ICAO[26] = { "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" };

cout << "Input a word to return in ICAO form: " << endl;


cin >> icaoword;

for(int i = 0; i < icaoword.length(); i++ )
{
    std::string strOutput;
    char cLetter;
    cLetter = icaoword[i];
    cLetter = toupper(cLetter); //use this to convert to uppercase, so it deciphers between lower and upper
    strOutput = ICAO[cLetter-'A'];
    cout << strOutput << endl;
}
cin.get(); cin.get();
return 0;
}


Here is what I have left to fix, but I am stuck.. I can't get it in correctly
"""Your index on the array should be type int. Also you should check
to make sure that the index value was between 0 and 25. That is the index can not be
negative or greater than 25. """"

if I change it to int I get an error but it still works, so I may just leave it not sure... the error is posted a few post up I changed in this version of the code to make the warning go away, but I believe I am being told to leave the for (int i ....) as is

Thanks for any help smile.gif

EMily

User is offlineProfile CardPM
+Quote Post

BitByte
RE: Array Of Strings
5 Dec, 2006 - 12:36 PM
Post #7

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
If you are told to leave it at int then maybe you should leave it at int. It is only a warning because string::size_type is guaranteed to hold the length of any string. In your case i don't think it will matter as your strings are not very long. Just change it to unsigned int. It's still int, so i suppose that will not make a difference to your assignment.
User is offlineProfile CardPM
+Quote Post

eadams20
RE: Array Of Strings
5 Dec, 2006 - 01:28 PM
Post #8

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions
Thanks! I knew it didn't effect it one way or the other... thanks for the help on that one!

Emily
User is offlineProfile CardPM
+Quote Post

eadams20
RE: Array Of Strings
5 Dec, 2006 - 01:33 PM
Post #9

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions


Now my biggest issue is with the fact that I can't figure out how to check to make

sure that the index value was between 0 and 25. That is the index can not be
negative or greater than 25.

I have tried it so many was but I just get errors... I assume it must be a do while... but I have tried testing the data and it just goes haywire..

How do I test that

Emily



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month