0

我正在写一个“猪拉丁”程序;读取用户的输入(名字和姓氏)使输入小写并根据名称中的内容更改名称。如果第一个字母(名字和姓氏)是元音,我们应该在它的末尾添加“方式”。

如果第一个字母是辅音,我们将取第一个字母,将它移到字符串的末尾并在其末尾添加“ay”。

尝试将文本添加到字符串末尾时,我的代码一直给我错误。它说它无法将字符串转换为字符,我不确定这意味着什么。它还说我不能将输出操作数 << 用于字符串,即使我以前使用过它。

错误发生在“strcpy”和我输出名称的最终代码中。

37:错误:无法转换'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >''char*'参数'1''char* strcpy(char*, const char*)'

47:错误:无法转换'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >''char*'参数'1''char* strcpy(char*, const char*)'

'operator<<'54:错误: in不匹配'std::cout << first'

我只需要一些帮助来修复错误并查看我哪里出错了。包含完整的代码。

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
int main()
{
    int q, s;
    char shea[] = "way";
    char gavin_stop_looking_at_ponies[] = "ay";
    vector <string> first;
    vector <string> last;
    cout << "Please enter your first name." << endl;
    for (int i = 0; i < first.size(); i++)
    {
        getline (cin, first[i]);
        string nfirst = first[i];
        while (nfirst[q])
        {
            nfirst[q] = tolower(nfirst[q]);
        }
        first[i] = nfirst;
    }
    cout << "Please enter your last name." << endl;
    for (int j = 0; j < last.size(); j++)
    {
        getline (cin, last[j]);
        string nlast = last[j];
        while (nlast[s])
        {
            nlast[s] = tolower(nlast[s]);
        }
        last[j] = nlast;
        }
    if ( (first[0] == "a") ||( first [0] == "e") || (first [0] == "i") || (first [0] == "o")     || (first [0] == "u"))
    {
        strcpy (first, "way");
    }
    else
    {
        first[first.size()] = first[0] + "ay";
    }
    
    if ( (last[0] == "a") ||( last [0] == "e") || (last [0] == "i") || (last [0] == "o") || (last [0] == "u"))
    {
        strcpy (last, "way");
    }
    else
    {
        last[last.size()] = last[0] + "ay";
    }
    cout << first << last << endl;
    return 0;
}
4

4 回答 4

5

我已经用一些问题的解释和解决方案的建议来注释你的代码。如果有什么不明白的,请评论,我会尽力澄清。

#include <iostream>

// You don't need 'vector' for this.
#include <vector>

// You won’t often need the C string header in C++.
#include <cstring>

#include <string>
using namespace std;
int main()
{
    // These variables are unused.
    int q, s;
    char shea[] = "way";
    char gavin_stop_looking_at_ponies[] = "ay";

    // 'first' and 'last' are both names, not collections
    // of names.
    string first;
    string last;
    vector <string> first;
    vector <string> last;

    // 'endl' is unnecessary here; it outputs a newline and
    // flushes the stream, but standard output is usually
    // line-buffered, meaning that newline flushes the
    // stream regardless.
    cout << "Please enter your first name.\n"
    cout << "Please enter your first name." << endl;

    // If you just want to get one name, 'getline' is perfect.
    getline(cin, first);

    // This loop would run 0 times because 'first' is an
    // empty vector.
    for (int i = 0; i < first.size(); i++)
    {
        getline (cin, first[i]);
        string nfirst = first[i];
        while (nfirst[q])
        {
            nfirst[q] = tolower(nfirst[q]);
        }
        first[i] = nfirst;
    }

    // To make a string lowercase, use 'tolower' on each character.
    // Here's one way to do it:
    for (string::size_type i = 0; i < first.size(); ++i)
        first[i] = tolower(first[i]);

    // Here's another, with C++11 enabled:
    for (auto& c : first)
        c = tolower(c);

    cout << "Please enter your last name.\n";
    cout << "Please enter your last name." << endl;

    // Same thing.
    getline(cin, last);
    for (int j = 0; j < last.size(); j++)
    {
        getline (cin, last[j]);
        string nlast = last[j];
        while (nlast[s])
        {
            nlast[s] = tolower(nlast[s]);
        }
        last[j] = nlast;
    }

    // Now 'first' is a string, and 'first[0]' is a 'char'.
    // "a" is a string literal; 'a' is a character literal.
    // You can compare each character individually:
    if (first[0] == 'a' || first[0] == 'e' || first[0] == 'i' || first[0] == 'o' || first[0] == 'u')

    // Or you can say "if the character was found in this
    // set of vowels".
    if (string("aeiou").find(first[0]) != string::npos)

    if ( (first[0] == "a") ||( first [0] == "e") || (first [0] == "i") || (first [0] == "o")     || (first [0] == "u"))
    {
        // This would try to copy "way" into 'first':
        // formerly a vector of string objects, now just a
        // string object. 'strcpy' wants a character buffer,
        // and will overwrite characters in that buffer—
        // probably not what you want:
        //
        // "aaron" => "wayon"
        // 
        strcpy (first, "way");

        // Instead, just append "way":
        first += "way";
    }
    else
    {
        // This says "take the first first character of the
        // string, add the value of that character to a
        // pointer to a buffer containing "ay", then try to
        // copy the resulting pointer past the end of the
        // string. Again, not quite what you intended!
        first[first.size()] = first[0] + "ay";

        // Think of it instead like this: take everything
        // after the first character, add a string consisting
        // of the first character back onto the end, then add
        // "ay" after that.
        first = first.substr(1) + string(1, first[0]) + "ay";
    }

    // Duplicated code! You could move the above logic into
    // a function to avoid this duplication. Then you only
    // have to work on it in one place. :)
    if ( (last[0] == "a") ||( last [0] == "e") || (last [0] == "i") || (last [0] == "o") || (last [0] == "u"))
    {
        strcpy (last, "way");
    }
    else
    {
        last[last.size()] = last[0] + "ay";
    }

    // I need a space between my first and last names!
    cout << first << ' ' << last << '\n';
    cout << first << last << endl;
    return 0;
}
于 2013-04-04T19:51:47.597 回答
1

first是一个vector<string>,不是一个stringvector<<支持cout. 如果要输出std::string中的每个字符std::vector,请尝试遍历std::vector并一次输出一个字符。

同样,firstis avector<string>不能隐式转换为 a char*。 对远离 strcpy的原始char数据块进行操作。用于字符串缓冲区上的 C 级操作(即使这样,使用起来也很危险)。firststrcpy

first[first.size()] = first[0] + "ay";是未定义的行为,因为您正在访问 的最后一个元素first,这是无效的内存。如果你想把东西推到后面first,试试看first.push_back( first[0] + "ay" );

std::string很可能您对, achar和 a之间的区别感到困惑char*。这些是完全不同的事情。 std::string是 的托管缓冲区charchar是一个 8 位的值,通常用于存储诸如'a'(类型为char)之类的文字。 char*是指向单个 的指针char,通常用作指向char紧密打包的非托管缓冲区的开始的指针。

std::vector<std::string>是托管缓冲区的托管char缓冲区。中的每个元素vector都是一个完整的缓冲区,其中包含一些未知数量的chars,而不是单个char

于 2013-04-04T19:30:24.967 回答
0

我同意之前的一些响应者,但我不想进行详细的代码审查,因为它会很大并且可能会错过重点。我认为您遇到的问题是对正确使用 C++ 标准库的根本性误解的症状。大多数情况下,您将标准 C 库字符串实现与 C++“字符串”对象混合。在 C 中,有一个函数集合接受字符指针并将它们作为字符串进行操作,而在 C++ 中有一个字符串对象,它封装了一个字符串并提供了一个对其进行操作的集合。在某些情况下,可以在 C char * 通常存在的地方使用 C++ 字符串,但在大多数情况下不能。

即使在 C++ STL(标准模板库)中,你也会把事情搞混了。根据您的代码,您似乎想要检索用户的名字和姓氏,即 2 个单独的字符串。但是,您要声明 2个字符串集合(在本例中为向量),而不仅仅是两个单独的字符串本身。程序中的许多其他问题也随之而来。

我想你可能会喜欢这里的信息:http: //en.cppreference.com/w/

于 2013-04-08T15:07:45.710 回答
0

我快速尝试编写代码只是为了稍微玩一下。这是我想出的——我认为它非常简单、干净且易于理解:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* simple pig-latin program ... converts a given "first name" and "last name"*/
/* from the user to pig latin translations.  Obviously, the so-called names  */
/* are really just any 2 random words, but that's the theme anyway.          */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


string xlat_to_piglatin(const string source)
{
    string result;

    //
    // translation: if word begins with a vowel, simply append "way" to
    //  it.  Otherwise, move the initial consonant from the beginning to
    //  the end, and append "ay" to it.
    //
    if (source.find_first_of("aeiou") == 0) {
        result = source;
        result.append("way");
    } else {
        result = source.substr(1, source.size());
        result.append(source.substr(0, 1));
        result.append("ay");
    }
    return result;
}

int main()
{
    string  first;
    string  last;

    //
    // get the first and last names from the user on standard in ...
    //
    cout << "Please enter your first name:" << endl;
    cin >> first;
    cout << "Please enter your last name:" << endl;
    cin >> last;

    //
    // convert strings to lower case ...
    //
    transform(first.begin(), first.end(), first.begin(), ptr_fun<int, int>(tolower));
    transform(last.begin(), last.end(), last.begin(), ptr_fun<int, int>(tolower));

    cout << xlat_to_piglatin(first) << " " << xlat_to_piglatin(last) << endl;
    return 0;
}
于 2013-04-09T12:29:29.370 回答