首页 > 解决方案 > C++错误[1]41387非法硬件指令

问题描述

我正在编写一个程序来获取用户值,返回更改后的版本。当它构建良好时,但是当我运行它并插入一个单词时,我收到一条错误消息“[1] 41387 非法硬件指令”。我是 C++ 新手,所以我不知道该怎么做,如果我需要解释更多,请告诉我

using namespace std;

string isFeminine(string countryName)
{
    if (countryName[countryName.length()] == 'e')
    {
        string country = "la" + countryName;
        cout << country;
        return country;
    }
    else
    {
        string country = "le" + countryName;
        cout << country;
        return country;
    }
}

string masculineExceptions(string countryName)
{
    string masculineExceptions[] = {"belize", "cambodge", "mexique", "mozambique", "zaire", "zimbabwe"};

    for (int i = 0; i < 5; i++)
    {
        if (countryName == masculineExceptions[i])
        {
            string country = "le" + masculineExceptions[i];
            cout << country;
            return country;
        }
    }

    isFeminine(countryName);
}

int main()
{
    cout << "Enter Country Name: ";

    string countryName;
    cin >> countryName;

    masculineExceptions(countryName);

    return 0;
}

标签: c++

解决方案


countryName.length()返回字符串中有多少个字符的值。如果字符串是abc,则字符串的长度将为 3。因为 C++ 数组从 0 开始,所以这将超出范围。您只需更改countryName.length()countryName.length() - 1.


推荐阅读