简体   繁体   中英

Telephone directory program using 2D array in c++

I have been given following assignment

Write a simple telephone directory program; contain two dimensional arrays in which you have hard code names and telephone number. Then declare a simple character array. You have to prompt user to enter any name, which you want to search. This name should be store in this character array, then search this name from the two dimensional array. If number is found against entered name then program should display the number against this name, and if not found then program should display the message that name is not registered.

Here is my code but i could not get the number when i search for the name. I am new to coding so i am having trouble making this code work. Help is appreciated.

#include <iostream>
#include <conio.h>
using namespace std;

int getPhone(int p[5][10],int row, int col, char key[10],char n[5][10]);

int main() {
    int i,j;
    char search[10];
    const int r = 5;
    const int c = 10;
    int element;
    int phone[r][c] = 
    {
    42-5429874,
    42-5333156,
    42-9824617,
    42-9927562,
    42-6238175
    };

    char name[r][c] = {"shazia","zara","sana","ahmad","maha"};
    cout<<"\nEnter name to find in directory : ";
    cin>>search[r];

    element = getPhone(phone,r,c,search,name);
    cin.get();  
    return 0;
    }

int getPhone(int p[5][10],int row,int col,char key[10], char n[5][10]) {
    int i, j;
    for(i=0;i<row;i++)
     for(j=0;j<col;j++)
       p[5][10] = p[i][j];
    if(key[j] = n[5][10])
         cout<<"The desired number is: "<<p[i][j]<<endl;
         else if(key[j]!=n[5][10])
         cout<<"Sorry! This name is not registered."; 

     return p[i][j];
}

Your code contains several mistakes. Let's examine them.

for(i=0;i<row;i++)
    for(j=0;j<col;j++)
        p[5][10] = p[i][j];

Here, you make no change on your array, because just the value of p[5][10] is changed. Furthermore, you access an invalid memory zone, because array indexes go from 0 to size - 1 in C++. So last index is p[4][9] .

if(key[j] = n[5][10])

In C++, comparing two values needs two = , because only one is an affectation that results the if to be always true. A tip to remember: two values to compare need two = .

else if(key[j]!=n[5][10])

The same than before, you access invalid memory zone. And are you sure that j is valid, eg less than 10 ? If not, you do double invalid access.

cin>>search[r];

As search is an array of char , you do an input of only a single char there, which I think is not what you want and that can leads to segfault.

int phone[r][c] = 
    {
    42-5429874,
    42-5333156,
    42-9824617,
    42-9927562,
    42-6238175
    };

Your array is not good, a simple 1-dimension array is enough, not 2-dimensions. Furthermore, 42-54.. does a subtraction, and I think is not what you want.

There are others mistakes . But why not using C++ abstractions, like std::vector , or std::string ? Your life would get so much easier. But I guess you have an old teacher that never took time to learn C++ news, or that is not a good teacher.

As a beginner, I suggest you to read C++ Primer and Programming: Principles and Practice Using C++ to introduce you both programming and modern C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM