Reputation: 29
Parden the kinda hacky code, but whenever I compile this it runs fine until it comes aross the letter 'u' then it spits out this,I'm sure it's something glaringly simple i'm overlooking but can't for the life of me see it.What am I doing wrong?
/*
* Converts letter into nato phonetic equivilant
*/
#include <iostream>
#include <vector>
int main()
{
std::vector<std::string> alphabet(26);
alphabet[97] = "ALPHA"; //0
alphabet[98] = "BRAVO";
alphabet[99] = "CHARLIE";
alphabet[100] = "DELTA";
alphabet[101] = "ECHO";
alphabet[102] = "FOXTROT";
alphabet[103] = "GOLF";
alphabet[104] = "HOTEL";
alphabet[105] = "INDIA";
alphabet[106] = "JULIET";
alphabet[107] = "KILO";
alphabet[108] = "LIMA";
alphabet[109] = "MIKE";
alphabet[110] = "NOVEMBER";
alphabet[111] = "OSCAR";
alphabet[112] = "PAPA";
alphabet[113] = "QUEBEC";
alphabet[114] = "ROMEO";
alphabet[115] = "SIERRA";
alphabet[116] = "TANGO";
alphabet[117] = "UNIFORM";
alphabet[118] = "VICTOR";
alphabet[119] = "WHISKEY";
alphabet[120] = "X-RAY";
alphabet[121] = "YANKEE";
alphabet[122] = "ZULU";
std::cout << "Enter a word:" << std::endl;
std::string word;
getline(std::cin, word);
std::system("clear");
for (int i = 0; i < word.length(); ++i) {
int num;
num = word.at(i);
std::cout << alphabet[num] << std::endl;
}
}
Upvotes: 1
Views: 248
Reputation: 385284
Your program is broken from the start, because you are writing to vector positions that do not exist!
Once you've started doing that, basically anything can happen, including the code appearing to work, or getting strange values, or spontaneously transporting the Mother of Dragons herself from her alternate reality into this reality and into my neighbourhood (yes, please!).
Your vector has 26 elements in it. The indices of those elements go from 0 to 25, not from 97 to 122.
You can apply some primary school arithmetic (i.e. subtracting then later re-adding 97) to offset the indices for storage, or you can use a std::map<int, std::string>
instead (which would be my recommendation).
Upvotes: 2
Reputation: 50026
You use vector like a map, by indexing it at places where no elements exists, maybe you want a map? If so then :
#include <map>
int main()
{
std::map<int, std::string> alphabet;
and it should work fine
Upvotes: 2
Reputation: 726849
The problem in your code is that it has undefined behavior. Your vector is 26-element long, but you set elements starting at index 97.
To fix this problem, make your vector 123-element long, or use num-97
for indexes, both in set up and printing.
std::vector<std::string> alphabet = {
"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"
};
...
std::cout << alphabet[num-'a'] << std::endl;
Note: Readability of your code would improve if you start using character literals in place of integer constants, i.e. alphabet['u'] = "UNIFORM";
instead of alphabet[117] = "UNIFORM";
. The end result is the same, but the readers wouldn't have to perform lookups in the UNICODE table to see the meaning of code point 117.
Upvotes: 3