c++ - 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;
}
解决方案
countryName.length()
返回字符串中有多少个字符的值。如果字符串是abc
,则字符串的长度将为 3。因为 C++ 数组从 0 开始,所以这将超出范围。您只需更改countryName.length()
为countryName.length() - 1
.
推荐阅读
- python - 无法安装 Pygame Mac
- javascript - axios:未处理的拒绝(TypeError):无法读取未定义的属性“错误”
- excel - 如何在 Google 表格或 Excel 中正确使用两个 if 语句?
- graphql - GraphQL-tag 始终使用默认值
- android - 在android studio中查询图像大小
- google-apps-script - 提取文件名(不包括文件扩展名)
- javascript - 如何在反应中将表单(不同用户)中的数据存储到数据库中
- java - 如何使用 Java CORBA 实现使用多个端口的分布式系统?
- python - ValueError:形状(10,36)和(35,)未对齐:36(dim 1)!= 35(dim 0)
- sql - 如何在 Postgres 中将 varchar 日期转换为日期类型?