My code for address book is as follows:
#include <iostream>
#include <cstring>
using namespace std;
struct
{
char name[101][30];
char telephone[101][8];
char email[101][20];
char address[101][50];
char postal[101][6];
}employee;
void enter()
{
int n;
cout << "Number of entries?\n";
cin >> n;
for (int i=0; i<n; i++){
gets_s (employee.name[i]);
gets(
employee.email[i]);
gets( employee.address[i]);
gets( employee.postal[i]);
gets( employee.telephone[i]);
gets ( employee.telephone[i]);
}
}
void printall(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8],int n)
{
for (int i=0; i<n; i++){
puts (employee.name[i]);
puts (employee.email[i]);
puts (employee.address[i]);
puts (employee.postal[i]);
puts (employee.telephone[i]);
}
}
void deleteentry(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
{
gets(name [100]);
for (int i=0; i<100; i++)
{ if (name[i]-'0'== name[100]-'0')
{
employee.name[i][0]='\0';
employee.email[i][0]='\0';
employee.address[i][0]='\0';
employee.postal[i][0]='\0';
employee.telephone[i][0]='\0';
}
}
}
void update(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
{
gets(name[100]);
int i;
for (i=0; i<100; i++)
{ if (name[i]-'0'==name[100]-'0')
deleteentry(name,email,address,postal,telephone);
enter(i);
}
}
void find(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
{
gets(name[100]);
for (int i=0; i<30; i++)
{if (strcmp(name[i], name[100])==0)
{cout << i << "\n";
}
}
};
void sort(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
{
bool sorted;
char temp;
int limit=101, last;
while (!sorted)
{
sorted = true;
for
(int i = 0; i < limit; i++)
{
if(name[i][0]-'0' > name[i+1]-'0')
{
sorted = false;
temp = name[i][0];
name[i][0] = name[i+1][0];
name[i+1][0] = temp;
last = i;
}
}
limit = last;
}
}
void menu ()
{
int i,n;
while (cin >> i, i>0){
switch (i)
{
case 1: cin >> n; enter(); break;
case 2: deleteentry(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
case 3: update(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
case 4: find(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
case 5: printall(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
case 6: sort(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
default: cerr << "Please enter a valid option\n";
}
system ("pause");
system ("cls");
}
};
int main
()
{
menu();
system ("pause");
}
The problems I face are:
1> In input one of the entries is overlapped with another, as in if employee.postal==570306
employee. address== employee.postal
2> In outputting (printing) what happens is that one of the data is not printed. Same with gets(). I used it twice, otherwise it was inputting only 4 things. I think 1 and 2 might have a connection.
Can someone suggest me what to do, so that I can debug other numerous problems?
Thanks.