// I want to dislpay my cout in an acsending order by ssn
// here is the program
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <math.h>
#include <ctype.h>
//Ebrahim Sharif
//ECE 370
//1-16-2008
using namespace std;
void getPersonalInfo();//function prototypes
int Str2Int(string InpStr);
long Str2Long(string);
void lookupPerson( char );
struct personalInfo
{
string name;
int Age;
long ssn;
string Address;
};
ifstream readfile;
string readfilename = "a1.txt";//read in file
personalInfo person[12];
int NumberOfPeople;
int main()
{
cout<<"*********Assinment number one******"<<endl<<endl;
int i,j,;
bool stoploop;
char lastInitial;
char letter;
//Step 1: Openning files
readfile.open(readfilename.c_str());
if (readfile.fail())//checking if file exists
{
cout<<"\n chech and make sure that there is an existing input file."<<endl;
system ("PAUSE");
return 0;
}
else
{
cout<<"\nfile is found and was opened"<<endl;
cout<<" "<<endl;
}
//Step 2: finction 1 to read files
getPersonalInfo();
//Step 3: cout all persons Info
for (j=0;j<NumberOfPeople ; j++)
{
cout << person[j].name <<endl;
cout << person[j].Age <<endl;
cout << person[j].ssn<<endl;
cout << person[j].Address << endl;
cout << endl;
}
//Step 4: looking up or searching persons
stoploop = false;
do
{
cout<<"********** search center ***********"<<endl<<endl;
cout << "Enter Last Name Initial to look up person : ";
cin>>lastInitial;
lookupPerson(lastInitial);
cout << "Do you want to search again press any key otherwise 'N' or 'n' to quit : ";
cin>>letter;
if(letter == 'N' || letter == 'n')
{
stoploop = true;//stop searching
}
else
{
stoploop = false; //keep asking for search
}
}while(!stoploop);
//Step : Close Files
readfile.close();
system ("PAUSE");
return 0;
}
void getPersonalInfo()
{
int i;
string temp; //temperary memory for convertion
string space;
i=0;//First Person (0th element)
while (readfile)
{
getline(readfile,person[i].name);//get person's name
getline(readfile,temp);//get age and convert to int
person[i].Age = Str2Int(temp);
getline(readfile,temp);//get ssn and conveert to long
person[i].ssn = Str2Long(temp);
getline(readfile,person[i].Address); //get persons sddress
getline(readfile,space); // skip line
i++;//loop for next person
}
NumberOfPeople = i;
}
int Str2Int(string InpStr)
{
char *str_ptr;
int Strlength;
int Outputage;
int i;
Strlength = InpStr.length();//get Length of the string
str_ptr = new char[Strlength]; // allocate memory needed for the input string
for (i = 0; i < Strlength; i++)//copy string to a poiunter for conversion need
str_ptr[i] = InpStr[i];
Outputage = atoi (str_ptr);//(alphabetic to integer)
return (Outputage);
}
long Str2Long(string InpStr)
{
char *str_ptr;
int Strlength;
long outputssn;
int i;
Strlength = InpStr.length();
str_ptr = new char[Strlength];
for (i = 0; i < Strlength; i++)
{
str_ptr[i] = InpStr[i];
}
outputssn = atol(str_ptr);
return (outputssn);
}
void lookupPerson(char lastInitial)
{
int j;
bool PersonFound ;
for (j=0;j<NumberOfPeople ; j++)
{
if(toupper(person[j].name[0]) == toupper(lastInitial))
{ //display on screen
cout << person[j].name <<endl;
cout << person[j].Age <<endl;
cout << person[j].ssn <<endl;
cout << person[j].Address << endl;
cout << endl;
PersonFound = true;
}
}
if(PersonFound == false)
{
cout<<"No one is found with a Last name initial '"<<lastInitial<<"'."<<endl<<endl;
}
}