gpt4 book ai didi

c++ - Piglatin 输出仍无法正常工作

转载 作者:行者123 更新时间:2023-11-28 05:58:46 24 4
gpt4 key购买 nike

#include <iostream>
using namespace std;
char firstLetter;
int pigLatin();
string word;
int wordFinder();
int firstVowel;
int x;
char vowel = ('a' && 'e' && 'i' && 'o' && 'u' && 'y' && 'A' && 'E' && 'I' && 'O' && 'U' && 'Y');
string engSentence;
char* letter = &engSentence[0];
bool vowelChecker (char c) // will check to see if there is a vowel
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'Y')
{
return true;
}
else
{
return false;
}
}

int pigLatin()
{
int lastLetter = word.length();
firstLetter = word[0];


if (vowelChecker(firstLetter)) //if the first letter of a word is a vowel...
{
cout << word << "way "; // print the word then way
}
else //if the first letter is not a vowel...
{
for (x = 1; x < lastLetter; x++) //in the loop of starting at the second letter and going to the last letter...
{
if (vowelChecker(x)) // check each letter to see if there is a vowel...
{
int firstVowel = x; //says that the first vowel is at point x
break;
}

}
string firstPortion = word.substr(0, firstVowel);
string secondPortion = word.substr(firstVowel, lastLetter);
cout << secondPortion << firstPortion << "ay";
//above is stating that it will first print the part of the word from the first vowel to the last letter,
//then it will print from the first letter to the first vowel and add 'ay' to the end

}
return 0;
}
int wordFinder() //will find words within a string
{
char* letter = &engSentence[0];
while ( *letter != '\0') //while the string isn't done...
{
if ( *letter == ' ' || *letter == '.' || *letter == ',' || *letter == '-' || *letter == '!' || *letter == '?') //if there is a space, comma or period...
{
pigLatin(); //run piglatin func
cout << " "; // add a space before the next word
word = ""; //sets the word back to empty

}
else
{
word += *letter; //adds letters to the word if no space comma or period is found.
}
letter++;
}
return 0;
}
int main()
{
cout << "Please enter a sentence for me to translate: " << endl;
cout << "**REMEMBER TO END SENTENCES WITH A PERIOD OR QUESTION MARK OR EXPLANATION MARK!**" << endl;

getline(cin, engSentence); //will get the whole line entered
wordFinder(); //runs wordfinder function
}

到目前为止,它只适用于元音是第一个字母的情况。 ( if (vowelChecker(firstLetter)) { cout << word << "way ";} ) 文本的这一部分......另一部分似乎没有在 Piglatin 函数中运行。此外,我收到一条警告,指出未使用 firstVowel,这可以解释为什么我的程序无法正常运行。不知道为什么...

最佳答案

if (vowelChecker(x)) // check each letter to see if there is a vowel...

这是传递循环变量 x,而不是那个位置的字符。你想要的是:

if (vowelChecker(word[x])) // check each letter to see if there is a vowel...

关于c++ - Piglatin 输出仍无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33676993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com