猪拉丁C++功能
PigLatin C++ function
这段代码给了我很多奇怪的错误。无论出于何种原因,"newstring"函数都没有运行。我认为这可能与它是 cout 语句的一部分这一事实有关,因为如果我没记错的话,如果我独立于 cout 语句调用函数,它不会给出相同的错误。该程序需要一个字符串函数,但由于某种原因,新函数未运行。任何人都可以看看代码吗?
#include <iostream>
#include <string>
using namespace std;
void newstring(string);
bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);
int main()
{
string str;
cout << "Enter a sentence to be translated to Pig Latin: ";
getline(cin, str);
cout << endl;
cout << "The pig Latin form of " << str << " is: " << newstring(str);
system("PAUSE");
return 0;
}
bool isVowel(char ch)
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();
string rStr;
rStr = pStr.substr(1, len - 1) + pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string :: size_type len;
bool foundVowel;
if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;
for ( int counter = 1; counter < len - 1; counter++)
{
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else
pStr = rotate(pStr);
if (!foundVowel)
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}
return pStr;
}
}
string newstring(string sentence)
{
string newsentence, currentword;
for (int i = 0; i < sentence.length(); i++)
{
if (sentence[i]==' ')
{
pigLatinString(currentword)+" ";
currentword.clear();
}
else
{
currentword+=sentence[i];
}
}
return newsentence;
}
您的newstring
原型是错误的。
void newstring(string);
应该是
string newstring(string);
函数 newstring 被声明为具有 void 类型
void newstring(string);
您不能创建 void 类型的对象并在输出流中发送它们
cout << "The pig Latin form of " << str << " is: " << newstring(str);
此外,该函数没有定义,因为您定义了另一个具有相同名称但返回 std::string 的函数
string newstring(string sentence)
^^^^^^^^^^^^^^^^^^
pigLatinString(currentword)+" ";
pigLatinString 返回一个字符串,但你不会对该结果执行任何操作。
newstring 返回 newSentence,但 is 为空。
也许你应该用从pigLatinString返回的内容填充新句子?
哦,现在我注意到你有两个newstring
......一个空和一个字符串...
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?