SO for this project im using binary trees and im supposed to make a phone book contact list. I have the set up sorrect but for some reason I cant get the contact list or even the names if the people I input to come up.
Code:
---------
#include
#include
#include
#include
#include
#include
using namespace std;
// define the class
class PhoneContacts {
// declare the variables.
char name[20], phoneno[15];
public:
// declare the member functions.
void FindContacts();
void DisplayContacts();
char *getContactname() { return name; }
char *getPhoneno() { return phoneno; }
void updateContact(char *nam, char *telno) {
strcpy_s(name, nam);
strcpy_s(phoneno, telno);
}
};
//function to find the contacts.
void PhoneContacts::FindContacts() {
cout << "\nEnter Name : ";
cin >> name;
cout << "Enter Contact No. : ";
cin >> phoneno;
}
// function to display the contacts.
void PhoneContacts::DisplayContacts() {
cout << "\n";
cout << setw(20) << name;
cout << setw(15) << phoneno;
}
// Main
int main()
{
// create object of the class.
PhoneContacts pc;
fstream fs;
int opt;
// open the file.
fs.open("input.txt");
char ch, nam[20], phono[10];
int choice, status = 0;
do {
cout << endl << "*****Contact Book*****" << endl << endl;
cout << "1) Add Contact\n" << endl << endl;
cout << "2) Display current contact" << endl << endl;
cout << "3) Find Contact Name." << endl << endl;
cout << "4) Find Contact Number" << endl << endl;
cout << "5) Update Contact No." << endl << endl;
cout << "6) Exit" << endl << endl;
cout << "Choose your choice : ";
cin >> choice;
switch (choice)
{
case 1:
pc.FindContacts();
// write the new record
fs.write((char *)&pc, sizeof(pc));
break;
case 2: //Display Contacts list
fs.seekg(0, ios::beg);
cout << "\n\nRecords in Contact Book\n";
while (fs)
{
fs.read((char *)&pc, sizeof(pc));
if (!fs.eof())
pc.DisplayContacts();
}
fs.clear();
break;
case 3:// finding the contact name.
cout << endl << endl << "Enter Contact Name: ";
cin >> nam;
fs.seekg(0, ios::beg);
status = 0;
while (fs.read((char *)&pc, sizeof(pc)))
{
if (strcmp(nam, pc.getContactname()) == 0)
{
status = 1;
pc.DisplayContacts();
}
}
fs.clear();
if (status == 0)
cout << endl << endl << "Contact is not found" << endl;
break;
case 4: //find contact name using the phone number.
cout << endl << endl << "Enter Telephone No : ";
cin >> phono;
fs.seekg(0, ios::beg);
status = 0;
while (fs.read((char *)&pc, sizeof(pc)))
{
if (strcmp(phono, pc.getPhoneno()) == 0)
{
status = 1;
pc.DisplayContacts();
}
}
fs.clear();
if (status == 0)
cout << endl << endl << "Contact is not found" << endl;
break;
case 5: //Updating the contact Telephone No.
cout << endl << endl << "Enter Name : " << endl;
cin >> nam;
fs.seekg(0, ios::beg);
status = 0;
int cnt = 0;
while (fs.read((char *)&pc, sizeof(pc)))
{
cnt++;
if (strcmp(nam, pc.getContactname()) == 0)
{
status = 1;
break;
}
}
fs.clear();
if (status == 0)
cout << endl << endl << "Contact is not found" << endl;
else
{
int location = (cnt - 1) * sizeof(pc);
cin.get(ch);
if (fs.eof())
fs.clear();
cout << "Enter New Telephone No : ";
cin >> phono;
fs.seekp(location);
pc.updateContact(nam, phono);
fs.write((char *)&pc, sizeof(pc));
fs.flush();
}
break;
}
cout << endl << endl << "Do you want to continue? Press 1 otherwise 0: " << endl << endl;
cin >> opt;
} while (opt == 1);
cout << endl << endl << "Thank you" << endl;
system("pause");
return 0;
}
---------
↧