c++ code to read following data below in array and search the column city for Wilmington and output the entire row with city Wilmington.
------------------------------data-----------------
Number,Gender,Title,GivenName,MiddleInitial,Surname,StreetAddress,City,Sta
te,ZipCode,EmailAddress,Birthday
1,male,Mr.,Hugo,L,Casey,"3867 Hillcrest
Circle",Rockford,MN,55373,[email protected],6/5/1990
2,female,Ms.,Tabitha,K,Cross,"1198 Carson
Street",Lexington,KY,40517,[email protected],1/28/1956
3,male,Mr.,Tyrone,L,Crowley,"3176 Fincham
Road",Riverside,CA,92507,[email protected],1/21/1982
4,male,Mr.,Lindsey,B,Reyes,"1251 Columbia
Road",Wilmington,DE,19801,[email protected],7/31/1954
5,female,Mrs.,Jay,A,Mier,"1443 Heavens Way","Los
Angeles",CA,90017,[email protected],12/25/1994
// C++ program to read a file and search the column city for Wilmington and output the entire row with city Wilmington.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string info[100][12]; // creating array of 100 records, assume max records= 100
int num_records=0, index;
string line;
ifstream fin("input.txt"); // open input file, provide full path to file
if(fin.is_open())
{
// read till end of file
while(!fin.eof())
{
getline(fin,line); // read the line
// break the line data using "," as delimiter and set the individual information in array
info[num_records][0] = line.substr(0, line.find(","));
index = line.find(",");
info[num_records][1] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][2] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][3] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][4] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][5] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][6] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][7] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][8] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][9] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
info[num_records][10] = line.substr(index+1,line.find(",",index+1)-index-1);
index = line.find(",",index+1);
if(fin.eof())
info[num_records][11] = line.substr(index+1,line.find(",",index+1)-index);
else
info[num_records][11] = line.substr(index+1,line.find(",",index+1)-index-1);
num_records++;
}
// loop over all records
for(int i=0;i<num_records;i++)
{
if(info[i][7] == "Wilmington") // check if city is Wilmington then print the entire row
cout<<" Number : "<<info[i][0]<<" Gender : "<<info[i][1]<<" Title : "<<info[i][2]<<" GivenName :"<<info[i][3]<<" MiddleInitial : "<<
info[i][4]<<" Surname : "<<info[i][5]<<" StreetAddress : "<<info[i][6]<<" City : "<<info[i][7]<<endl<<" State : "<<info[i][8]<<
" ZipCode :"<<info[i][9]<<" EmailAddress : "<<info[i][10]<<" Birthday : "<<info[i][11]<<endl;
}
fin.close(); //close the file
}else
cout<<" Unable to open file : input.txt";
return 0;
}
//end of program
Output:
Input file: input.txt
The data in a line are separated by comma
Output:
c++ code to read following data below in array and search the column city for Wilmington...