Narno22 Narno22 - 1 month ago 19
C++ Question

Searching Multi-Dimensional Array

I've created a multi-dimensional array in C++, but it doesn't return anything when I search it. 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. 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

#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;
}

Answer

for(int i = 0; i > 1000; i++) should be for(int i = 0; i < 1000; i++). The way you wrote it your for loop will never be executed.