#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

/*
 * 
 */
#define NAME 5
#define EMAIL 2
int main() {



    char* address[NAME][EMAIL] = {{"Barrack", "president@whitehouse.gov"},
                         {"Roger", "rpowell@valleycollege.edu"},
                         {"Mark", "markz@facebook.com"},
                         {"Bill", "billg@microsoft.com"},
                         {"Gaga", "admin@lady-gaga.net"}};

    for(int i = 0; i < NAME; i++) {
        for(int j = 0; j < EMAIL; j++) {


           cout << address [i][j] << endl;

        }
    }




    return 0;
}

I don't seem to have a clue of how to search for the name and in return get the email for that name. I managed to figure out how to print out the array. Your help is much appreciated. Thank you.

You can compare strings with function strcmp().

For example

int i;
char* names[3] = { "john", "jessica", "james" };
char* findname = "jessica";

for (i=0; i<3; i++ ) {
    if (strcmp(names[i], findname) == 0) {
        printf("We found a match to %s at index %d", findname, i);
    }
}

Thank you, I got it to show only the one being searched for, but when i want to input it using cin >> it doesn't work.

char* findname = "Bill";




   for(int i = 0; i < NAME; i++) {
       if(strcasecmp(address[i][0], findname) == 0) {
        for(int j = 0; j < EMAIL; j++) {
            cout << address[i][j] << endl;
        }    
        }

    }

Edited 4 Years Ago by kruschev

This article has been dead for over six months. Start a new discussion instead.