Write a program to read from the text file phonebooktxt and

Write a program to read from the text file phonebook.txt and then do the following:
(1) Ask for inputting an area code from keyboard, and print on screen how many phone numbers in the file have this area code.
(2) Format the phone numbers from (1) by inserting a comma \",\" before the last four digits (for example, 814,8981329 is changed to 814,898,1329) and output all the formatted phone numbers to a new text file: formattedphonebook.txt

A sample run on screen and output file:

Enter Area Code to Extract: 814

Area Code 814 has 26 numbers.

All Phone Numbers = 75

phonebook.txt

543,2021000
543,2021006
543,2021025
543,2021061
543,2021063
543,2021066
543,2021080
543,2021116
543,2021117
543,2021132
543,2021158
543,2021159
543,2021164
543,2021167
543,2021175
543,2021214
543,2021240
543,2021271
543,2021273
543,2021329
543,2021354
543,2021357
543,2021360
543,2021361
543,2021397
618,5041000
618,5041006
618,5041025
618,5041061
618,5041063
618,5041066
618,5041079
618,5041080
618,5041116
618,5041117
618,5041132
618,5041158
618,5041159
618,5041164
618,5041167
618,5041175
618,5041214
618,5041240
618,5041271
618,5041273
618,5041329
618,5041360
618,5041361
618,5041397
814,8981000
814,8981006
814,8981025
814,8981061
814,8981063
814,8981066
814,8981079
814,8981080
814,8981116
814,8981117
814,8981132
814,8981158
814,8981159
814,8981164
814,8981167
814,8981175
814,8981214
814,8981240
814,8981271
814,8981273
814,8981329
814,8981354
814,8981357
814,8981360
814,8981361
814,8981397

Solution


#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{

   //declareation of variables
   string areaCode;
   string searchCode;
   string code1;
   string code2;
   string phoneNumber;
   int counter=0;  
   int totalPhoneNumbers=0;

   //set file names
   string filename=\"phonebook.txt\";
   string outFilename=\"formattedphonebook.txt\";
  
   //open input file stream
   ifstream fin;
   fin.open(filename);

   //open output file stream
   ofstream fout;
   fout.open(outFilename);


   //check file open not success
   if(!fin)
   {
       cout<<\"File doesnot exist...\"<<endl;
       system(\"pause\");
       return -1;
   }  
   cout<<\"Enter Area Code to Extract: \";
   //read search code
   getline(cin,searchCode);

   //read input file stream until end of file encounterd
   while(!fin.eof())
   {
      
       getline(fin,phoneNumber,\'\ \');
       //get area code
       areaCode=phoneNumber.substr(0,3);
       //get next three digits
       code1=phoneNumber.substr(4,3);
       //get next four digits
       code2=phoneNumber.substr(6,4);

       //write to output file
       fout<<areaCode<<\",\"<<code1<<\",\"<<code2<<endl;

       //chcking of search code is area code then
       //incrment counter by 1
       if(searchCode==areaCode)
           counter++;

       //increment total phone numbers by 1
       totalPhoneNumbers++;
   }

   //
   if(counter!=0)
       cout<<\"Area Code \"<<areaCode<<\" has \"<<counter<<\" numbers.\"<<endl;
   else
       cout<<\"No Area Code is matched with \"<<searchCode<<endl;

   cout<<\"All Phone Numbers = \"<<totalPhoneNumbers<<endl;


   //close input and output file streams
   fin.close();
   fout.close();

   //pause program output on console until user press akey
   system(\"pause\");
   return 0;
}

---------------------------------------------------------------------------------------------------------

Sample Output:

formattedphonebook.txt

543,202,2100
543,202,2100
543,202,2102
543,202,2106
543,202,2106
543,202,2106
543,202,2108
543,202,2111
543,202,2111
543,202,2113
543,202,2115
543,202,2115
543,202,2116
543,202,2116
543,202,2117
543,202,2121
543,202,2124
543,202,2127
543,202,2127
543,202,2132
543,202,2135
543,202,2135
543,202,2136
543,202,2136
543,202,2139
618,504,4100
618,504,4100
618,504,4102
618,504,4106
618,504,4106
618,504,4106
618,504,4107
618,504,4108
618,504,4111
618,504,4111
618,504,4113
618,504,4115
618,504,4115
618,504,4116
618,504,4116
618,504,4117
618,504,4121
618,504,4124
618,504,4127
618,504,4127
618,504,4132
618,504,4136
618,504,4136
618,504,4139
814,898,8100
814,898,8100
814,898,8102
814,898,8106
814,898,8106
814,898,8106
814,898,8107
814,898,8108
814,898,8111
814,898,8111
814,898,8113
814,898,8115
814,898,8115
814,898,8116
814,898,8116
814,898,8117
814,898,8121
814,898,8124
814,898,8127
814,898,8127
814,898,8132
814,898,8135
814,898,8135
814,898,8136
814,898,8136
814,898,8139

Write a program to read from the text file phonebook.txt and then do the following: (1) Ask for inputting an area code from keyboard, and print on screen how ma
Write a program to read from the text file phonebook.txt and then do the following: (1) Ask for inputting an area code from keyboard, and print on screen how ma
Write a program to read from the text file phonebook.txt and then do the following: (1) Ask for inputting an area code from keyboard, and print on screen how ma
Write a program to read from the text file phonebook.txt and then do the following: (1) Ask for inputting an area code from keyboard, and print on screen how ma
Write a program to read from the text file phonebook.txt and then do the following: (1) Ask for inputting an area code from keyboard, and print on screen how ma

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site