#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.

Recommended Answers

All 3 Replies

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

    }

When you input, is the ENTER you press added to the end of the string? Better check.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.