Feb 3, 2015 at 4:56pm UTC
Can someone please help me? Im stuck at this and i dont know what to do.. :( Please edit the wrong ones.. or help me to change it.. Needed badly tomorrow.. Thanks guys!
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
#include <fstream>
using namespace std;
struct Person
{
char fname[100];
char lname[100];
char cnum[15];
string address;
};
void AddEntry(Person p);
void DisplayEntry();
void EditEntry(Person *);
void DeleteEntry();
void SearchEntry();
void line(char, int);
void newLine();
Person person[20];
Person p;
int index = 0;
int main()
{
ofstream fout;
fout.open("c:\\csprog\\personinfo.txt");
int option;
while (1) {
system("cls");
system("COLOR D");
cout << "-------------------Address Book";
line('-', 19);
cout << "\n What would you like to do?\n";
line('-', 50);
cout << " [1] Add Contact \n";
cout << " [2] Edit Contact \n";
cout << " [3] Delete Contact \n";
cout << " [4] View Contacts \n";
cout << " [5] Search Address Book \n";
cout << " [6] Exit \n";
line('-', 50);
cout << "Please enter your choice: ";
cin >> option;
newLine();
char buff[2];
switch (option)
{
case 1:
system("cls");
system("COLOR B");
cout << "Enter First Name: ";
cin.getline(p.fname, 100);
cout << "Enter Last Name: ";
cin.getline(p.lname, 100);
cout << "Enter Address: ";
cin >> p.address;
newLine();
cout << "Enter Contact Number: ";
cin >> p.cnum;
newLine();
AddEntry(p);
break;
case 2:
EditEntry(person);
break;
case 3:
DeleteEntry();
break;
case 4:
DisplayEntry();
break;
case 5:
SearchEntry();
break;
case 6:
exit(1);
default:
cout << "Invalid choice \n\n";
system("pause>0");
}
}
return 0;
}
void AddEntry(Person p)
{
ofstream fout;
fout.open("C:\\Users\\user\\Desktop\\personinfo.txt");
person[index] = p;
index++;
}
void DisplayEntry()
{
ofstream fout;
fout.open("C:\\Users\\user\\Desktop\\personinfo.txt");
system("cls");
system("COLOR E");
cout << setw(11) << "No." << setw(10)
<< " First Name" << setw(10)
<< " Last Name" << setw(18)
<< "Address" << setw(10)
<< " Contact No." << setw(10);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int i = 0;
for (; i < index; i++){
cout << endl;
fout << endl;
cout << i + 1 << setw(10);
fout << i + 1 << setw(2);
cout << person[i].fname << setw(10);
fout << person[i].fname << setw(2);
cout << person[i].lname << setw(18);
fout << person[i].lname << setw(2);
cout << person[i].address << setw(10);
fout << person[i].address << setw(2);
cout << person[i].cnum << setw(10);
fout << person[i].cnum << setw(2);
}
printf("\nRecords Total:%d\n", i);
system("pause>0");
}
void EditEntry(Person *p)
{
ofstream fout;
fout.open("C:\\Users\\user\\Desktop\\personinfo.txt");
system("cls");
system("COLOR A");
int a = 1;
cout << "Enter the entry number you would like to edit: ";
cin >> a;
newLine();
if (a <= index)
{
cout << endl;
cout << "Please enter the updated information: ";
cout << "\nEnter First Name: ";
cin.getline(p[a - 1].fname, 100);
cout << "Enter Last Name: ";
cin.getline(p[a - 1].lname, 100);
cout << "Enter Address: ";
cin >> p[a - 1].address;
newLine();
cout << "Enter Contact Number: ";
cin >> p[a - 1].cnum;
newLine();
}
else{
cout << "I'm sorry, that is an invalid selection.\n"
<< "The number of entry is ranging from 0-" << index << " \n"
<< "Please select this option again.\n\n";
system("pause>0");
}
void DeleteEntry()
{
bool is_ok = false;
while(! is_ok)
{
cout << "\nEnter the Entry Number that you want to Delete: ";
cin >> entrynum;
if (entrynum > index)
cout << "There are only " << index << " entries in the book";
else
break;
}
temp = entrynum - 1;
if (entrynum > index)
{
cout << "There are only " << index << " entries in the book";
DeleteEntry();
}
else
{
cin.clear();
cin.ignore();
cout << "\n[" << temp + 1 << "]";
cout << setw(15) << person[temp].fname;
cout << setw(19) << person[temp].lname;
cout << setw(19) << person[temp].address;
cout << setw(17) << person[temp].cnum;
cout << endl << endl;
cout << "Delete Selected Entry? [Y]/[N]";
cin >> choice;
}
if (choice == 'y')
{
for (int i = temp; i < (index - 1); i++)
{
person[i] = person[i + 1];
}
for (int i = temp; i < index - 1; i++)
{
person[i].fname = person[i - 1].fname;
person[i].lname = person[i - 1].lname;
person[i].address = person[i - 1].address;
person[i].cnum = person[i - 1].cnum;
}
--index;
ofstream fout;
fout.open("C:\\Users\\user\\Desktop\\personinfo.txt");
for (int i = 0; i < index; i++)
{
fout << person[i].fname << "," << person[i].lname << "," << person[i].address << "," << person[i].cnum << ",";
}
fout.close();
cout << "\nSuccessfully Deleted!";
cout << endl << endl;
}
else if (choice == 'n' || choice == 'N')
cout << "Deletion has been cancelled...";
{
cout << "Go back to Main menu? [y]/[N]";
cin >> choice;
if (choice == 'y')
{
for (int i = temp; i < (index - 1); i++)
{
person[i] = person[i + 1];
}
else if (choice == 'n' || choice == 'N')
{
DeleteEntry();
}
system("pause");
}