[英]Searching Multi-Dimensional Array
I've created a multi-dimensional array in C++, but it doesn't return anything when I search it. 我已经在C ++中创建了多维数组,但是在搜索时它不会返回任何内容。 Just wondering if I messed something up in the code? 只想知道我是否弄乱了代码? Full disclosure: This was originally a class assignment, but I turned it in 2 weeks ago. 全面披露:这本来是班级作业,但我在2周前上交了作业。 I'm asking for clarification for future programs. 我要求澄清将来的程序。
I've put the main body (minus some of the list since it's 1000 entries) below 我将主体(减去了一些列表,因为它有1000个条目)放在下面
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
std::string name;
std::string age;
//declare 1000*2 Array
std::string array[1000][2] =
{
{ "Karen Rose","25" },
{ "Cora Faison","11" },
{ "Donna Jones","49" },
{ "Robert Lowe","31" },
{ "Patricia Johnston","17" },
{ "Maryanne Hicks","40" },
{ "James Mcmurray","53" },
};
cout << "Enter a name to search for " << endl;
cin >> name;
for(int i = 0; i > 1000; i++)
{
if(name == array[i][0])
{
age = array[i][1];
cout << name << "'s age is" << age << endl;
}
else
{
cout << "we found nothing" << endl;
}
}
return 0;
}
Beyond having your loop test backward, using cin >> name
will only get the first name. 除了向后进行循环测试外,使用cin >> name
只会得到名字。 You test first and last. 您首先测试,最后一次。 Thus your tests will never succeed. 因此,您的测试将永远不会成功。
Use std::getline
. 使用std::getline
。
Doing a modicum of debugging work would have turned up both errors. 进行少量调试工作会同时出现两个错误。
for(int i = 0; i > 1000; i++)
should be for(int i = 0; i < 1000; i++)
. for(int i = 0; i > 1000; i++)
应该是for(int i = 0; i < 1000; i++)
。 The way you wrote it your for
loop will never be executed. 您编写它的方式for
循环将永远不会执行。
I > 1000 makes your loop empty. I> 1000使您的循环为空。 < is the ooerator you want. <是您想要的对象。
But anyway why 1000 and not 7? 但是无论如何为什么不是1000,而不是7?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.