strcmp problem

Pages: 12
i am trying to make a phonebook
i was able to add a few contacts using an add function.i also included a search function to search for contacts
here is the function
void phonebook::search()
char j[25];
cout<<" ";
gets(j);
for (int i=0;i<!='\0';i++)
if(strcmp(j,contact[i])==0)
cout<<contact[i]<<':'<<num[i];}
but when i enter a contact which was previously added using the add function ,nothing appears.
pls help me
There are errors in that code, it won't compile. Please post the actual code which you are using, otherwise we may simply be looking at typing errors.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
class phonebook
{char contact[100][25];
char number[100][12];
public:
phonebook(){cout<<"WELCOME TO SJ'S PHONEBOOK \n";}
void add();
void addmore();
void search();
void edit();
void display();
};
void phonebook::add()
{
int n;
cout<<"how many numbers do you want to add\n";
cin>>n;
for(int i=0;i<n;i++)
{cout<<"\nenter the name\t"<<i+1<<endl;
gets(contact[i]);
cout<<"\nenter the number\n";
gets(number[i]);}}
void phonebook::search()
{
char j[50];
cout<<"\nenter the contact\n";
gets(j);
for(int p=0;p!='\0';p++)
{if(strcmp(j,contact[p])==0)
{cout<<number[p];}
else
{cout<<"\nnot found\n";}
}
}
void phonebook::addmore()
{int l=strlen(contact[25]);
cout<<"how many more do you want to add\n";
int h;cin>>h;//need more stuff
for(l;l<h;l++)
{ cout<<"ENTER THE CONTACT NAME\n";
gets(contact[l]);
cout<<"ENTER THE NUMBER\n";
gets(number[l]);}
}
void phonebook::edit()
{cout<<"function under construction";}

void phonebook::display()
{for(int u=0;u!='\0';u++)
cout<<contact[u]<<':'<<number[u];}
void main()
{ clrscr ();

phonebook t;
cout<<"its empty over here. lets add some numbers\n";

t.add();
cout<<"cool. what do you want to do next\n (add[a],search[s],edit[e])\n";
char o;
cin>>o;
switch(o)
{
case 's' :t.search();break;
case 'a' :t.addmore();break;
case 'e' :t.edit();break;
} cout<<"displaying all contacts";
t.display();
}
You are using strcmp on arrays of type char which hold junk.
Don't ever use gets as it is unsafe.
main does not have a return type of void.
Your phonebook::display function will never display anything.
You access outside the bounds of your arrays.
PLS DO SUGGEST AN ALTERNATIVE CODE TO MEET MY PURPOSE.
Some of the loops don't work properly:
1
2
3
4
5
void phonebook::display()
{
    for(int u=0;u!='\0';u++)
    cout<<contact[u]<<':'<<number[u];
}


Here, u!='\0' is not suitable. '\0' is simply a character with a numeric value of zero. Hence that code is the same as u != 0; since the starting value is 0, the condition will fail the very first time and the loop will not execute.

It is probably simpler, and certainly safer, to keep a count of the number of elements of the phonebook which are currently in use. I'd like to suggest you used std::string and a std::vector instead of a array, but I've a feeling you may be using an older compiler, so I'll try to keep the suggestions with that in mind.

If you can get the add() and display() working correctly, the search and other functions can follow afterwards.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int capacity = 100;  // mximum number of entries
const int namewide = 25;   // number of characters in name
const int numwide  = 12;   // number of characters in number

class phonebook
{
    int size;              // how many enries are currently used
    char contact[capacity][namewide];
    char number[capacity][numwide];
    
public:
    phonebook() 
    {
        size = 0; 
        cout << "WELCOME TO SJ'S PHONEBOOK \n"; 
    }
    void add();
    void search();
    void edit();
    void display();
};

void phonebook::add()
{
    int n;
    cout << "how many numbers do you want to add\n";
    cin >> n;
    cin.ignore(1000, '\n'); // discard newline from input buffer
    
    for (int i=0; i<n && size<capacity; i++)
    {
        cout << "\nenter the name\t" << i+1 << endl;
        cin.getline(contact[size], namewide);
        cout << "\nenter the number\n";
        cin.getline(number[size], numwide);
        size++;
    }
}

void phonebook::display()
{
    for (int i=0; i<size; i++)
        cout << contact[i] << ':' << number[i] << endl;
}

int main()
{ 
    phonebook book;
    
    cout <<"its empty over here. lets add some numbers\n";
    book.add();
     
    cout<<"\ndisplaying all contacts\n\n";
    book.display();    
      
    return 0;
} 


Note, addmore() is not needed, just use add() again.
thanks a bunch....can you please give an alternative for search function as well
Last edited on
Well, the search can be based upon the display() function. but instead of printing every line, first use strcmp() to compare contact[i] with the desired name.
the search function which i have used does not work .can you please modify that function.
pardon me if i am asking rubbish but is there any way by which the numbers entered during the execution could get saved permanently so that the next time i rerun the program i can acces the previously entered contacts
Last edited on
the search function which i have used does not work


Could you please show your latest code for that function, and we will see why it isn't working.
is there any way by which the numbers entered during the execution could get saved permanently so that the next time i rerun the program i can acces the previously entered contacts

That sounds a straightforward requirement. Save the data to a text file. Use something like the display() function. All that needs to change is the cout is replaced by the name of an ofstream. And possibly the delimiter (currently ':' ) might be changed to a tab character '\t'.

A corresponding function would be needed to read the data back from the file. That could be done by using an ifstream and getline with a matching '\t' delimiter to read the name and default '\n' delimiter to read the number.

Could I ask, are you using Turbo C++, and are you required to do so? ideally you should use a recent compiler, everything becomes simpler with less unsafe code.
here is the latest code
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
const int capacity = 100; // mximum number of entries
const int namewide = 25; // number of characters in name
const int numwide = 12; // number of characters in number

class phonebook
{
int size; // how many enries are currently used
char contact[capacity][namewide];
char number[capacity][numwide];

public:
phonebook()
{
size = 0;
cout << "WELCOME TO SJ'S PHONEBOOK \n";
}
void add();
void search();
void edit();
void display();
};
void phonebook::add()
{
int n;
cout << "how many numbers do you want to add\n";
cin >> n;
cin.ignore(1000, '\n'); // discard newline from input buffer

for (int i=0; i<n && size<capacity; i++)
{
cout << "\nenter the name\t" << i+1 << endl;
cin.getline(contact[size], namewide);
cout << "\nenter the number\n";
cin.getline(number[size], numwide);
size++;
}
}
void phonebook::search()
{
char j[namewide];
cout<<"\nenter the contact\n";
cin.getline(j,namewide);
for(int i=0; i<size; i++)
{do
{cout<<contact[i]<<':'<<number[i];}while(strcmp(j,contact[i])==0);}
}
void phonebook::edit()
{cout<<"function under construction";}

void phonebook::display()
{
for (int i=0; i<size; i++)
cout <<endl<<contact[i] << ':' << number[i] << endl;
}
int main()
{ clrscr ();

phonebook t;
cout<<"its empty over here. lets add some numbers\n";

t.add();
for(int i=0;i<=10;i++)
{ cout<<"cool. what do you want to do next\n (add[a],search[s],edit[e],display numbers[d],quit[q])\n";
char o;
cin>>o;
switch(o)
{
case 's' :t.search();break;
case 'a' :t.add();break;
case 'e' :t.edit();break;
case 'd' :t.display();break;
case 'q' :exit(0);
default :cout<<"wrong entry"; } }
return 0;
}
Last edited on
i am a beginner in c++ .I am doing this prgram as part of a project.i am to use only those functions which i have learned so far.i am yet to learn about fstream and stuffs.
:)
i need to produce the most basic code for the phonebook.
Thanks for posting the latest code.

One thing you need to understand is the difference in the behaviour of cin >> and cin.getline().

Example:
1
2
    char ch;
    cin >> ch;
here first of all any leading whitespace (including newline, space and tab characters) will be ignored. The first non-whitespace character found in the input buffer will be read into the variable ch. That has satisfied the cin request and the process stops, which means any other characters, including the newline character, will remain in the input buffer.

On the other hand, the getline() does not ignore leading whitespace. It reads each character it finds in the input buffer into the string, until it encounters the newline delimiter. That newline is extracted from the buffer and discarded.

When a sequence such as this occurs:
1
2
    cin >> n;
    cin.getline(contact[size], namewide);
there is a trailing newline '\n' remaining in the input buffer after the cin >> n and the getline() will stop when that is found. The result is an empty string will be read, and the user is not prompted for input. The solution is to empty the buffer after the first operation:
1
2
3
    cin >> n;
    cin.ignore(1000, '\n'); // discard newline from input buffer
    cin.getline(contact[size], namewide);


Your current code has a similar problem when choosing the search option as cin>>o; in main() is followed by cin.getline(j,namewide); in the function phonebook::search().

The solution is to change main() like this:
1
2
3
4
    cout<<"cool. what do you want to do next\n (add[a],search[s],edit[e],display numbers[d],quit[q])\n";
    char o;
    cin>>o;
    cin.ignore(1000, '\n'); // discard newline from input buffer     

This way, no matter which function will be executed next, you know that it will start with the input buffer empty.


Another error is in the function search(). There is a do-while loop meaning that if an entry is actually found, there is an infinite loop and the found result will be output repeatedly. Replace the do-while with a simple if statement.


I'm still not clear on whether or not you are obliged to use a particular compiler. I highly recommend that you upgrade to a modern compiler and use ISO standard C++.



Last edited on
thank you so much for your response.I made some changes in the program and it now works like a charm.
HERE IS THE CODE
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
const int capacity = 100; // mximum number of entries
const int namewide = 25; // number of characters in name
const int numwide = 12; // number of characters in number

class phonebook
{
int size; // how many enries are currently used
char contact[capacity][namewide];
char number[capacity][numwide];

public:
phonebook()
{
size = 0;
cout << "WELCOME TO SJ'S PHONEBOOK \n";
}
void add();
void search();
void edit();
void display();
};
void phonebook::add()
{
int n;
cout << "how many numbers do you want to add\n";
cin >> n;
cin.ignore(1000, '\n'); // discard newline from input buffer

for (int i=0; i<n && size<capacity; i++)
{
cout << "\nenter the name\t" << i+1 << endl;
cin.getline(contact[size], namewide);
cout << "\nenter the number\n";
cin.getline(number[size], numwide);
size++;
}
}
void phonebook::search()
{ int flag;
char j[25];
cout<<"\nenter the contact\n";
gets(j);
for(int i=0; i<=size; i++)
{if(strcmp(j,contact[i])==0)
{cout<<contact[i]<<':'<<number[i];flag=1;break;} else flag=0;}
if (flag==0) cout<<"\nno match found\n"; }
void phonebook::edit()
{cout<<"\nwhose number do you wantt to edit?\n";
char k[25];
gets(k);
for(int i=0;i<size;i++)
{if(strcmpi(k,contact[i])==0)
{cout<<"enter the edited number\n";
gets(number[i]);break;}}}


void phonebook::display()
{
for (int i=0; i<size; i++)
cout <<endl<<contact[i] << ':' << number[i] << endl;
}
int main()
{ clrscr ();

phonebook t;
cout<<"its empty over here. lets add some numbers\n";

t.add();
for(int i=0;i<=10;i++)
{ cout<<"\n what do you want to do next\n (add[a],search[s],edit[e],display numbers[d],quit[q])\n";
char o;
cin>>o;
switch(o)
{
case 's' :t.search();break;
case 'a' :t.add();break;
case 'e' :t.edit();break;
case 'd' :t.display();break;
case 'q' :exit(0);
default :cout<<"wrong entry "; } }
return 0;
}
I AM TO USE THE COMPILER WHICH I HAVE WITH ME NOW.
CAN I ASK YOU ONE LAST FAVOUR?
CAN U REMODEL THE ABOVE CODE SO THAT ALL THE NUMBERS CAN BE LOADED ON RE-RUN?
I AM OKAY IF YOU USE FSTREAM AND I GUESS THAT IS THE ONLY WAY TO DO SO.
CHEERS
Last edited on
Well, I did outline a way to approach that. Using fstream, once you get past the initial hurdle (adding two lines of code) things work in very much the same way as cin and cout so it's fairly easy to pick up.

Have a read through this introductory part of the tutorial, and then have a go at writing the code. If you run in to problems, show how far you got and we'll take it from there.
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<fstream.h>
const int capacity = 100; // mximum number of entries
const int namewide = 25; // number of characters in name
const int numwide = 12; // number of characters in number

class phonebook
{
int size; // how many enries are currently used
char contact[capacity][namewide];
char number[capacity][numwide];

public:
phonebook()
{
size = 0;cout<<"\t\t\t\t";
textcolor(RED);
textbackground(GREEN);
cprintf("WELCOME TO SJ'S PHONEBOOK ");

}
void add();
void search();
void edit();
void display();
};
void phonebook::add()
{
int n;
textcolor(YELLOW);
cprintf( "\nhow many numbers do you want to add\n");
cout<<endl;
cin >> n;
cin.ignore(1000, '\n'); // discard newline from input buffer

for (int i=0; i<n && size<capacity; i++)
{ textcolor(BLUE);
cprintf( "\nenter the name");cout<<'\t'<< i+1 << endl;
cin.getline(contact[size], namewide);
cprintf("enter the number");
cout<<endl;
cin.getline(number[size], numwide);
size++;
}
}
void phonebook::search()
{ int flag;
char j[25];

cout<<"\nenter the contact\n";
gets(j);
for(int i=0; i<=size; i++)
{if(strcmp(j,contact[i])==0)
{cout<<"here is\t"<<contact[i]<<"'s number\n"<<number[i];flag=1;break;} else flag=0;}
if (flag==0) cout<<"\nno match found for\t"<<j; }
void phonebook::edit()
{cout<<"\nwhose number do you wantt to edit?\n";
char k[25];
gets(k);
for(int i=0;i<size;i++)
{if(strcmpi(k,contact[i])==0)
{cout<<"enter the edited number\n";
gets(number[i]);
cout<<"here is the edited contact\n"<<contact[i]<<':'<<number[i];break;}}}


void phonebook::display()
{ cout<<"\ndisplaying all numbers\n";
for (int i=0; i<size; i++)
{cout <<endl<<contact[i] << ':' << number[i] << endl;
ofstream(contact[i]);

ofstream(number[i]);
}}
int main()
{ clrscr ();
ofstream myfile;
myfile.open ("phone.txt", ios::out | ios::app | ios::in);

phonebook t;


cout<<endl<<endl;
textcolor( MAGENTA);
cprintf("its empty over here. lets add some numbers\n");
cout<<endl;

t.add();
for(int i=0;i<=10;i++)
{ cout<<"\n what do you want to do next\n (add[a],search[s],edit[e],display numbers[d],quit[q])\n";
char o;
cin>>o;

switch(o)
{
case 's' :t.search();break;
case 'a' :t.add();break;
case 'e' :t.edit();break;
case 'd' :t.display();break;
case 'q' :exit(0);
default :cout<<"\nwrong entry\n.Try once more\n"; } }
myfile.close();

return 0;
}
the code compiles but the next time I run the code,it wont display my previuos cotacts
For the time being I'll ignore the non-standard usage (such as conio.h) and focus on the problems with the fstream. Well done for having a go. Even though it isn't completely correct, you learn more by trying things for yourself than simply copying someone else's code.

Now, let's look at what you have right:
1
2
ofstream myfile;
myfile.open ("phone.txt", ios::out | ios::app | ios::in);

Some of that is right. Though it doesn't make much sense to use an output stream (ofstream) for input (ios::in). Also, having declared myfile, no more use is made of it, so naturally nothing happens with the file.

These lines simply don't compile for me
1
2
    ofstream(contact[i]);
    ofstream(number[i]);

it should have looked more like this:
 
    myfile << contact[i] << '\n' << number[i] << '\n';


I think in order to keep things simple, two new functions might be used, for example call them load() and save(). At the start of the program, after
 
phonebook t;
call the load function, like this:
 
t.load();

Similarly, just before exiting from the program, call the save function.

So, using the existing display() function as a guide, we can write the save() function like this:
1
2
3
4
5
6
7
void phonebook::save()
{
    ofstream fout("phone.txt");

    for (int i=0; i<size; i++)
         fout << contact[i] << '\n' << number[i] << '\n';
}

and add the function call in main() like this:
 
case 'q': t.save();    exit(0);

Try this, and then examine the contents of the text file to see whether it has worked.

The load() function will do something similar, using an ifstream as it will be used for input.
To read the name and number, I suggest to use the getline function.
http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
Pages: 12