Hey; quick question if anyone can help me. I am entering First name and Last name and then searching for it to see if it is there. It finds the name before I save it to a disk file; but when I retrieve it back and search again it doesn't work. That's because I have to put carriage returns when saving to a disk file so that it knows which information is separated. Thus; when retrieving data I am trying to make it so that it gets rid of the carriage returns. But searching for name still doesn't work..
I know it is in my Retrievedata. You can do a CTRL+F for Retrievedata; I am sorry for posting the whole code here; hehe.
If anyone can hint me to the problem, would be great . Thanks!
Code:
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <graphics.h>
#include <dos.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
void inputdata();
void printdata();
void printdatatwo();
void retrievedata();
void searchfull();
void searchlast();
void searchzip();
void searchcitystate();
void sortabc();
void sortcity();
void sortzip();
void savedata();
void sortstate();
char choice;
char snum[25], sage[10],szip[10];
char string[25][25],name[50];
int num, namecmp, age[10], I, u;
struct info {
char namel[15];
char namef[15];
char string[50];
char city[15];
char state[15];
char phone[15];
char zipcode[15];
};
info MyArr[25];
int main()
{
int a;
do{
clrscr();
cout<<"1. Create Database \n";
cout<<"2. Save Database \n";
cout<<"3. Retrieve Database \n";
cout<<"4. Print Database Before Retrieve\n";
cout<<"5. Print Database After Retrieve\n";
cout<<"6. Quit\n\n";
cout<<"7. Search by Last Name Only \n";
cout<<"8. Search by Full Name \n";
cout<<"9. Search by Zipcode \n";
cout<<"10. Search by City/State \n";
cout<<"11. Sort Alphabetically \n";
cout<<"12. Sort by City\n";
cout<<"13. Sort by State\n";
cout<<"14. Sort by Zipcode \n\n";
cout<<"Answer: ";cin>>a;
if(a==1)
{inputdata();}
if(a==2)
{savedata();}
if(a==3)
{retrievedata();}
if(a==5)
{printdata();}
if(a==4)
{printdatatwo();}
if(a==7)
{searchlast();}
if(a==8)
{searchfull();}
if(a==9)
{searchzip();}
if(a==10)
{searchcitystate();}
if(a==11)
{sortabc();}
if(a==12)
{sortcity();}
if(a==13)
{sortstate();}
if(a==14)
{sortzip();}
}while(a!=6);
if(a==6)
{return 0;}
getch();
return 0;
}
void inputdata()
{ do{
num=num+1;
u++;
cout<<"Enter First Name: "; cin>>MyArr[num].namef;
cout<<"Enter Last Name: "; cin>>MyArr[num].namel;
printf("Enter Address: ");gets(MyArr[num].string);
cout<<"Enter City: "; cin>>MyArr[num].city;
cout<<"Enter State: "; cin>>MyArr[num].state;
cout<<"Enter Zipcode: "; cin>>MyArr[num].zipcode;
printf("Enter Phone #: ");gets (MyArr[num].phone);
cout<<"Anymore? Y/N: "; cin>> choice;
}while(choice=='y' || choice=='Y');
}
void savedata()
{ ofstream outfile;
outfile.open("U:\\EXAMPLE.DAT",ios::out);
if(outfile)
{ outfile<<num<<"\n";
for(I=1; I<=num; I++)
{ outfile<<MyArr[I].namef<<"\n";
outfile<<MyArr[I].namel<<"\n";
outfile<<MyArr[I].city<<"\n";
outfile<<MyArr[I].string<<"\n";
outfile<<MyArr[I].state<<"\n";
outfile<<MyArr[I].zipcode<<"\n";
outfile<<MyArr[I].phone<<"\n";
}
}
else
{cout<<"what did u do wrong ";}
outfile.close();
}
void retrievedata()
{ ifstream infile;
infile.open("U:\\EXAMPLE.DAT",ios::in);
char tempf[10], templ[10], tempc[10], tempa[10], temps[10], tempz[10], tempp[10];
int fl, ll, cl, al, sl, zl, pl;
if(infile)
{
infile.getline(snum,10);
num=atoi(snum);
for(I=1; I<=num; I++)
{ infile.getline(tempf,10);
infile.getline(templ,10);
infile.getline(tempc,10);
infile.getline(tempa,20);
infile.getline(temps,10);
infile.getline(tempz,10);
infile.getline(tempp,50);
fl=strlen(tempf);
strncpy(MyArr[I].namef,tempf,fl-1);
ll=strlen(templ);
strncpy(MyArr[I].namel,templ,fl-1);
cl=strlen(tempc);
strncpy(MyArr[I].city,tempc,fl-1);
}
}
else
{cout<<"There was something wrong with the file!";}
infile.close();
}
void printdata()
{}
void printdatatwo()
{}
void searchlast()
{
int I, found,f,ptr;
char Sname[25];
ptr=0;
clrscr();
do{
cout<<"Last Name You Are Searching For: ";
gets (Sname);
cout<<"\n";
found=0;
for(I=1; I<=u; I++)
{ ptr=strcmpi(Sname, MyArr[I].namel);
if (ptr==0)
{
cout<<"The Last Name " <<Sname<<" Was Found ";
cout<<"In: "; cout<<""<<MyArr[I].namef; cout<<" "<<MyArr[I].namel;cout<<"\n";
found++;
}
}
if (found==0)
{ cout<<"No one has this last name.\n\n";}
cout<<"Search a new #? 1=Yes | 2=No: ";
cin>>f;
clrscr();
}while(f!=2);
}
void searchfull()
{}
void searchzip()
{}
void searchcitystate()
{}
void sortabc()
{}
void sortcity()
{}
void sortstate()
{}
void sortzip()
{}