【发布时间】:2017-10-01 15:51:18
【问题描述】:
我正在尝试在数组 Bob 中找到“空”,然后在找到索引中的空位置后,我想用“LEEROY JENKINS”替换单词“空”请原谅我缺乏知识,我仍然很对此不熟悉,并尽我所能学习。
问题:
- int spindex() 没有找到正确的索引
- empty 没有被替换
#include <string>
#include <iostream>
using namespace std;
int spindex(string x[], int n)
{
int i = 0;
int tindex;
while (x[i] != "empty" && i < n)
{
tindex = i;
i++;
}
return tindex;
}
int main()
{
string Bob[] = { "shaun", "empty", "tom", "empty", "chris", "sharon", "empty"};
int pim = 7;
int Q = spindex(Bob, pim);
Bob[Q] = "LEEROY JENKINS";
cout << Q << endl;
cout << Bob[Q] << endl;
for (int i = 0; i < 7; i++) {
cout << Bob[i] << endl;
}
}
【问题讨论】:
-
您的
pim值错误。为什么? -
让它更短忘记改变它,但不会改变结果只是一个小的错过类型
-
如果您的输入中没有“空”,它会极大地改变结果。
标签: c++ arrays string while-loop