Reputation: 3
I am making a gibberish translator where you input a word and the console outputs the word in gibberish. The parameters of the gibberish language are if the word has any vowels in it you put "ab" infront of that vowel. For example "Hello" in gibberish would be "H-ab-ell-ab-o". Although i'm having a problem (Please go easy on me if you find this code messy or not well written. Im just learning)
This is my code:
int quit = 2;
int i;
bool vowel;
string word;
string letter = &word[int (i)];
void translation() {
cout << "Enter a word: " << endl;
cin >> word;
for (int i = 0; i <= 20; ++i) {
if (letter == "a") {
cout << "ab" << letter;
}
if (letter == "e") {
cout << "ab" << letter;
}
if (letter == "i") {
cout << "ab" << letter;
}
if (letter == "o") {
cout << "ab" << letter;
}
if (letter == "u") {
cout << "ab" << letter;
} else {
cout << letter ;
}
}
}
void again() {
cout << "Enter 1 to translate another word, Enter 0 to quit" << endl;
cin >> quit;
}
int main() {
while (quit >= 1) {
translation();
again();
}
return 0;
}
Say, when it prompts you to put in a word like this:
Enter a word:
You put in the word Hello, it should output this:
Enter a word:
Hello
Habellabo
Enter 1 to translate another word, Enter 0 to quit
But my program outputs this:
Enter a word:
hello
Enter 1 to translate another word, Enter 0 to quit
Why isn't it outputting the translated word?
Upvotes: 0
Views: 85
Reputation: 10242
Try this instead :
string letter;
...
for (int i = 0; i <= word.length(); ++i) {
letter = word[i];
if (letter == "a") {
cout << "ab" << letter;
}
...
The letter variable has to be updated at each iteration, if not it remains empty.
Also, as mentioned in other answers, it would be a better choice, as you are iterating through characters one by one, to use the char
type for the letter
.
Upvotes: 1
Reputation: 52365
string letter = &word[int (i)];
is some strange code, and invokes UB because the char*
may not be null terminated. It is the equivalent of string letter = &word[0];
and you are taking the address of the reference to char
returned from the operator[]
. Also, using double quotes is wrong "a"
is a string literal. You want single quotes to compare with char
.
Change your code to the following:
for (int i = 0; i < word.size(); ++i) {
if (word[i] == 'a') {
cout << "ab" << word[i];
}
if (word[i] == 'e') {
cout << "ab" << word[i];
}
if (word[i] == 'i') {
cout << "ab" << word[i];
}
if (word[i] == 'o') {
cout << "ab" << word[i];
}
if (word[i] == 'u') {
cout << "ab" << word[i];
} else {
cout << word;
}
Upvotes: 1
Reputation: 1495
You define string letter = &word[int (i)];
. This doesn't do what you seem to think. It uses the value of i
at the time the statement executes. Changing i
later will not change the value of letter
. You need to explicitly evaluate word[i]
inside the loop. Also, comparing vs "a"
, "e"
, etc. compares strings, when you really want character comparison. Try single quotes instead of double quotes, i.e. 'a'
, 'e'
, etc.
Upvotes: 0
Reputation: 34625
string letter = &word[int (i)];
i
is global variable and is initialized to 0. But the problem is the with the string word
. It is an empty string. &word[0]
yields you the location of \0
character. So, letter
is initialized as an empty string. So, you are actually hitting the final else statement and nothing is printed as the letter
is empty as well.
Upvotes: 0