Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 这个拉丁猪节目怎么了?_C++ - Fatal编程技术网

C++ 这个拉丁猪节目怎么了?

C++ 这个拉丁猪节目怎么了?,c++,C++,它应该做什么 我的piglatin程序应该从用户输入中提取一个短语,并将其输出为piglatin。基本上,它会把“你好”这样的词变成“你好” 我的问题 当我输入hello my man时,输出是ellohay y man-may,当我刚刚输入hello my时,输出是ellohay y-may。正如你所看到的,在它成功地翻译了第一个单词后,它在第二个单词上挣扎。它在y和may后面放了一个空格,我一辈子都搞不懂为什么会这样。当我输入两个以上的单词时,输出就更奇怪了,如上所示。我想让它在我输入时输

它应该做什么

我的piglatin程序应该从用户输入中提取一个短语,并将其输出为piglatin。基本上,它会把“你好”这样的词变成“你好”


我的问题

当我输入
hello my man
时,输出是
ellohay y man-may
,当我刚刚输入
hello my
时,输出是
ellohay y-may
。正如你所看到的,在它成功地翻译了第一个单词后,它在第二个单词上挣扎。它在
y
may
后面放了一个空格,我一辈子都搞不懂为什么会这样。当我输入两个以上的单词时,输出就更奇怪了,如上所示。我想让它在我输入时输出
hello my man
。代码如下。谢谢


#包括“stdafx.h”
#包括
#包括
使用名称空间std;
无效短语_解析器(字符串);//遍历短语并查找“”然后传递给pigLatin_Translator()
void pigu翻译程序(字符串);
int main()
{
字符串短语;//用于将用户单词或短语翻译为拉丁语

cout您的问题在
string word=phrase.substr(startCount,i);

您使用的
substr
不正确。
substr
的第二个参数是要提取的子字符串的长度。将
i
替换为
i-startCount
,您就可以开始了


或者,搜索一种更好的拆分字符串的方法。有许多选项比手动拆分容易得多。

如果您只使用
>
,您将读取以空格分隔的单词。您不需要手动拆分为单词。@RawN,这里有一点需要阅读::p您的分词器无法正确拆分“我的人”简而言之。我正在调查。解决此类问题的正确工具是调试器。在询问堆栈溢出问题之前,您应该逐行检查代码。有关更多帮助,请阅读。至少,您应该[编辑]您的问题包括一个重现您的问题的示例,以及您在调试器中所做的观察。+1对于调试器建议,这是一个非常宝贵的工具。这就是我所做的一切,我在代码中寻找错误的第一个迹象。非常感谢!我将研究其他拆分字符串的方法。再次感谢!
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void phrase_Parser(string); // Goes through phrase and looks for ' ' to then pass to pigLatin_Translator()
void pigLatin_Translator(string);
int main()
{
    string phrase; //used for user word or phrase to be translated to piglatin
    cout << "Enter any word: ";
    getline(cin, phrase); 

    phrase_Parser(phrase);
    return 0;
}

void phrase_Parser(string phrase) {
    int startCount = 0;

    for (int i = 0; i < phrase.length(); i++) {
        if (phrase[i] == ' ') {

            string word = phrase.substr(startCount, i);
            startCount = (i + 1); // decides where to start the word next time it is ran through

            pigLatin_Translator(word); // runs word through translator
        }
    }
}
void pigLatin_Translator(string word) {
    string partOne;
    string partTwo;

    for (int x = 0; x < word.length(); x++) {
        if (word[0] == 'q' && word[1] == 'u') {
            cout << word.substr(2, word.length()) << word.substr(0, 2) << "ay ";
            break;
        }
         else if ((word[x] == 'a') || (word[x] == 'e') || (word[x] == 'i') || (word[x] == 'o') || (word[x] == 'u') || (word[x] == 'y')) {
            partOne = word.substr(x, word.length()); //from first vowel to end of word
            partTwo = word.substr(0, x); // from first letter to first vowel, not including the vowel
            cout << partOne << partTwo << "ay "; // adding "ay" to the end of the word
            break;  
        }
    }
}