i need help in c++ program I'm asked to make a phone book which can sort all the names in it alphabetically plllllz i need help
twomers 408 Posting Virtuoso
Why don't you show us what you have started and we'll go from there. If you have nothing started start and you'll be more likely to get help.
nouryn 0 Newbie Poster
i don't have a clue a bout the sort i've tried many functions from the internet but they didn't work but i've began in the other function in the phone book and that is what i get thnx
#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
using namespace std;
fstream phonebook;
struct phone
{
char word[50];
char fword[50];
fstream edit;
int id;
string fname;
string sname;
string lname;
int phonenumber;
int mobilenumber;
string email;
};
phone info;
char d,f,r;
string c,g;
int add()
{
phonebook.open("c:\\c\\phonebook.txt",ios::out|ios::app);
cout<<"enter the first name\n";
cin>>info.fname;
cout<<"enter the second name\n";
cin>>info.sname;
cout<<"enter the last name\n";
cin>>info.lname;
cout<<"enter the phone number\n";
cin>>info.phonenumber;
cout<<"enter the moile number\n";
cin>>info.mobilenumber;
cout<<"enter the e-mail\n";
cin>>info.email;
phonebook<<info.fname<<"\t "<<info.sname<<"\t "<<info.lname<<" \t "
<<info.phonenumber<<" \t "<<info.mobilenumber<<" \t "<<info.email<<endl;;
cout<<endl;
phonebook.close();
return 0;
}
int getdisplay()
{
cout<<"if u wanna display all contacts press 'a'\n"<<"if u wanna display contacts begining with specific letter press 'c'\n";
cin>>r;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
switch(r)
{
case 'a':
{
while(!phonebook.eof())
{
getline(phonebook,c);
cout<<c<<endl;
}
phonebook.close();
break;
}
case 'c':
{
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook>>c;
cout<<"enter the wanted letter";
cin>>g;
// getdisplay;
cout<<"dsucyeehr";
phonebook.close();
break;
}
}
return 0;
}
void getsearch()
{
cout << "Enter your contact:"<<endl;
cin >> info.word;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
int counter =0;
while (!phonebook.eof()){
phonebook >> info.fword;
int j=strcmp(info.fword,info.word);
if (j==0){
cout << "contact was found in the file :)"<<endl;
cout<< info.word << endl;
counter ++;
phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
cout<<info.fword<<" "<<info.fname<<" "<<info.sname<<" "<<info.lname
<<" "<<info.phonenumber<<" "<<info.mobilenumber<<" "<<info.email;
break;
}
}
if (counter == 0)
cout << "contact was not found in the file :(" << endl;
phonebook.close();
}
int main()
{
while(1)
{
cout<<endl<<"1-Adding new contacts"<<endl
<<"2-Displaying contacts information"<<endl
<<"3-Searching for a contact"<<endl
<<"4-Deleting a contact"<<endl
<<"5-Modifying a contact information"<<endl
<<"6-Sort the contacts by name"<<endl
<<"7-exit"<<endl<<endl;
cout<<"enter your choice"<<endl;
cin>>d;
switch(d)
{
case '1':
{
add();
cout<<"if u wann add another contact press 'y' if not press 'n'";
cin>>f;
if(f=='y')
{
add();
}
break;
}
case '2':
{
getdisplay();
break;
}
case '3':
{
getsearch();
break;
}
case '4':
{
break;
}
case '5':
{
break;
}
case '6':
{
break;
}
case '7':
{
return 0;
break;
}
}
}
system ("pause");
return 0;
}
ithelp 757 Posting Virtuoso Banned
If you reading the information from phonebook why are you asking user to input it ?
nouryn 0 Newbie Poster
that is one of the function that if the user want to add another contact to the original phone book which already contain names
nouryn 0 Newbie Poster
i've edited the code and made a function for sorting but now i need the modifiy function how can i modify a contact that's my code hope u can help
#include<iostream>
#include <cstring>
#include<string>
#include <iomanip>
#include <map>
#include<fstream>
#include<algorithm>
#include <vector>
//#include <stdlio>
using namespace std;
fstream phonebook;
struct phone
{
char word[50];
char fword[50];
fstream edit;
int id;
string fname;
string fname2;
string sname;
string lname;
int phonenumber;
int mobilenumber;
string email;
};
phone info;
char d,f,r;
string c,g;
void sortingtany2()
{
vector<string> sV;
phonebook.open("c:\\c\\phonebook.txt");
string word;
// separates words in file at a whitespace
// and loads a string vector
while(getline(phonebook,word))
sV.push_back(word);
cout << "Unsorted words:" << endl;
for(int i = 0; i < sV.size(); i++)
cout << sV[i] << endl;
cout << "---------------" << endl;
cout << "Sorted words:" << endl;
sort( sV.begin(), sV.end() );
for(int i = 0; i < sV.size(); i++)
{
cout << sV[i] << endl;
phonebook<<sV[i];
}
}
void edit()
{
string c;
char c1;
cout << "Please type some lines of text. Enter a period to finish:\n";
do {
c=cin.get();
phonebook>>info.fname;
if(c==info.fname)
{
info.fname.clear();
}
} while (c1!='.');
}
void add()
{
phonebook.open("c:\\c\\phonebook.txt",ios::out|ios::app);
cout<<"enter the first name\n";
cin>>info.fname;
cout<<"enter the second name\n";
cin>>info.sname;
cout<<"enter the last name\n";
cin>>info.lname;
cout<<"enter the phone number\n";
cin>>info.phonenumber;
cout<<"enter the moile number\n";
cin>>info.mobilenumber;
cout<<"enter the e-mail\n";
cin>>info.email;
phonebook<<info.fname<<"\t "<<info.sname<<"\t "<<info.lname<<" \t "
<<info.phonenumber<<" \t "<<info.mobilenumber<<" \t "<<info.email<<endl;;
cout<<endl;
phonebook.close();
}
void getdisplay()
{
cout<<"if u wanna display all contacts press 'a'\n"<<"if u wanna display contacts begining with specific letter press 'c'\n";
cin>>r;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
switch(r)
{
case 'a':
{
while(!phonebook.eof())
{
getline(phonebook,c);
cout<<c<<endl;
}
phonebook.close();
break;
}
case 'c':
{
char le;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
cout<<"enter the wanted letter";
cin>>le;
phonebook.seekg(0,ios::beg);
while(getline(phonebook ,c))
{
cout<<"zashgdas";
for(int i=0;i<c.length();i++)
{
cout<<"afa";
if(c[0]>=le)
{
cout<<c;
cout<<"hasgdajk";
break;
}
}
}
phonebook.close();
break;
}
}
}
void getsearch()
{
cout << "Enter your contact:"<<endl;
cin >> info.word;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
int counter =0;
while (!phonebook.eof())
{
phonebook >> info.fword;
int j=strcmpi(info.fword,info.word);
if (j==0)
{
cout << "contact was found in the file :)"<<endl;
cout<< info.word << endl;
counter ++;
break;
}
}
if (counter == 0)
cout << "contact was not found in the file :(" << endl;
phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
cout<<info.fword<<" "<<info.fname<<" "<<info.sname<<" "<<info.lname
<<" "<<info.phonenumber<<" "<<info.mobilenumber<<" "<<info.email<<endl;
phonebook.close();
}
void getmodify()
{
cout << "Enter your letter:"<<endl;
cin >> info.word;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
int counter =0;
while (!phonebook.eof()){
phonebook >> info.fword;
int j=strcmpi(info.fword,info.word);
if (j==0){
cout << "contact was found in the file :)"<<endl;
cout<< info.word << endl;
counter ++;
info.word==NULL;
break;
}
}
if (counter == 0)
cout << "contact was not found in the file :(" << endl;
phonebook.close();
}
int getdel()
{
char hot;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
ofstream myfile("c:\\c\\1.txt",ios::out);
cout<<"ur u sure u want to delete all contact(y/n)"<<endl;
cin>>hot;
if (hot=='y')
{
phonebook>>info.fname;
while( getline(phonebook,info.fname))
{
cout << info.fword<< "\n";
}
phonebook.close();
myfile.close();
// delete the original file
remove("c:\\c\\phonebook.txt");
// rename old to new
rename("c:\\c\\1.txt","c:\\c\\phonebook.txt");
}
else
{
cout<<"the contact still as it is"<<endl;
}
// all done!
return 0;
}
void del()
{
string del;
cout << "Enter your contact:"<<endl;
cin >> info.word;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
int counter =0;
while (!phonebook.eof())
{
phonebook >> info.fword;
int j=strcmpi(info.fword,info.word);
if (j==0)
{
cout << "contact was found in the file :)"<<endl;
cout<< info.word << endl;
counter ++;
break;
}
}
if (counter == 0)
cout << "contact was not found in the file :(" << endl;
phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
info.fname.clear();
info.fname=' ';
phonebook<<info.fname;
phonebook.close();
}
int main()
{
while(true)
{
cout<<endl<<"1-Adding new contacts"<<endl
<<"2-Displaying contacts information"<<endl
<<"3-Searching for a contact"<<endl
<<"4-Deleting a contact"<<endl
<<"5-Modifying a contact information"<<endl
<<"6-Sort the contacts by name"<<endl
<<"7-exit"<<endl<<endl;
cout<<"enter your choice"<<endl;
cin>>d;
switch(d)
{
case '1':
{
add();
cout<<"if u wann add another contact press 'y' if not press 'n'";
cin>>f;
if(f=='y')
{
add();
}
break;
}
case '2':
{
getdisplay();
break;
}
case '3':
{
getsearch();
break;
}
case '4':
{
int e;
cout<<"enter '1'if u want to delete all contact"<<endl
<<"enter '2' if u want to delete specific contact"<<endl;
cin>>e;
switch (e)
{
case 1:
{
getdel();
break;
}
case 2:
{
del();
break;
}
}
break;
}
case '5':
{
getmodify();
break;
}
case '6':
{
sortingtany2();
break;
}
case '7':
{
return 0;
break;
}
}
}
system ("pause");
return 0;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.