Goal: Your assignment is to write a C++ program to read in a list of phone call
ID: 3704763 • Letter: G
Question
Goal: Your assignment is to write a C++ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, L/O manipulators, and the string class. File format Here is an example of a file containing three phone call records: 1 20160101120000 20160101120015 +12062812000 2 20160508115512 20160508121513 +86698974534 3 20160229182110 20160229190005 +12025551234 4 20170131235500 20170131235532 +50499853746 5 20181231011515 20181231022432 +13257744661 6 20190505060000 20190505061559 +15067845191 Each line records one phone call. As you can see, each line contains 3 different fields: start, end, and a phone number Here is an explanation of these three fields: Start, end: A 14-digit field giving the year, month, day, hour, minute, and second when the call was started. The format is yyyymmddhhmmss. Military time is used for the hour. For instance, 20180101011020, means the date was January 1, 2018 and the time was 20 seconds past 1:10 am. Phone number E.164: A 12-character field starting with a +sign followed by 11 digits. This field specifies the phone number in E.164 format. The first 1-3 digits after the +sign specify the country code of the phone number, and the meaning of the remaining digits is specific to the country or region represented by the country code To limit the scope of this assignment we will only consider E.164 phone numbers from the following regions CC Region North America (US, Canada, and some Caribbean islands) 502 Guatemala 503 El Salvador 504 Honduras 505 Nicaragua 506 Costa Rica 886 Taiwan We've picked these regions because they have simple 12-digit E.164 formats.Explanation / Answer
I have created a function to print. Did not get time to validate.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class A
{
public:
int IsValidDate(string start,string yyyy,string dd,string hh,string mm,string ss,int MM)
{
if(start.length()!=14)
{
cout<<"invalid date";
}
for(int i=0;i<start.length();i++)
{
if(!(start[i]>='0' && start[i]<='9'))
{
cout<<"invalid date";
}
}
}
int PrintDate(string start)
{
string yyyy,dd,hh,mm,ss;
int MM;
yyyy = start.substr(0,4);
MM = stoi(start.substr(4,2));
dd = start.substr(6,2);
hh = start.substr(8,2);
mm = start.substr(10,2);
ss = start.substr(12,2);
IsValidDate(start,yyyy,dd,hh,mm,ss,MM);
switch(MM)
{
case '1':
cout<<"Jan"<<" ";
break;
case '2':
cout<<"Feb"<<" ";
break;
case 3:
cout<<"Mar"<<" ";
break;
case 4:
cout<<"Apr"<<" ";
break;
case 5:
cout<<"May"<<" ";
break;
case 6:
cout<<"june"<<" ";
break;
case 7:
cout<<"july"<<" ";
break;
case 8:
cout<<"Aug"<<" ";
break;
case 9:
cout<<"Sep"<<" ";
break;
case 10:
cout<<"Out"<<" ";
break;
case 11:
cout<<"Nov"<<" ";
break;
case 12:
cout<<"Dec"<<" ";
break;
}
cout<<"Jan"<<dd<<" "<<hh<<":"<<mm<<":"<<ss<<" "<<yyyy;
return 0;
}
int PrintPhone(string phone)
{
string country,phone1,phone2,phone3;
string na="1",Guatemala="502",el="503",honduras="504",nicar="505",costa="506",taiwan="886";
country = phone.substr(2,1);
if(country.compare("1")==0)
{
phone1 = phone.substr(2,3);
phone2 = phone.substr(5,3);
phone3 = phone.substr(8,4);
cout<<country<<" ("<<phone1<<") "<<phone2<<"-"<<phone3<<" y";
}
else
{
country = phone.substr(2,3);
phone1 = phone.substr(4,4);
phone2 = phone.substr(8,4);
if(country.compare(Guatemala)==0 || country.compare(el)==0 || country.compare("honduras")==0 || country.compare(nicar)==0 || country.compare(costa)==0 || country.compare(taiwan)==0)
{
cout<<country<<" "<<phone1<<"-"<<phone2<<" y";
}
else
{
cout<<country<<" "<<phone1<<"-"<<phone2<<" y";
}
}
}
};
int main()
{
cout<<"Start End Country Phone Local Duration(s)"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
ifstream input("data.txt");
if(input.is_open())
{
while(!input.eof())
{
string data;
getline(input,data);
string start = data.substr(0,14);
string end = data.substr(15,14);
string phone = data.substr(29,13);
A a;
a.PrintDate(start);
cout<<" ";
a.PrintDate(end);
cout<<" ";
a.PrintPhone(phone);
cout<<endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.