Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#ifndef PHONE_H #define PHONE_H #include <iostream> using namespace::std; const

ID: 641898 • Letter: #

Question

#ifndef PHONE_H
#define PHONE_H
#include <iostream>
using namespace::std;

const int COUNT_TOLL_FREE = 2; // CHANGE the value
class phone {
   public:
       phone(void); // Default constructor

       phone(int area, int exchange, int num); // 3 Argument constructor

       phone(const phone &); // Copy constructor

       phone(const char []); // pass a string as the parameter
                                                   // of the form "913-469-8500"
       //or of the form "9134698500"

       ~phone(void); // One and only destructor

       void setPhone( int area, int exchange, int num);

       void setPhone( const char []);

       void getPhone(char *) const; // form a string in caller's memory
       // of the form 913-469-8500

       int getNumber(void) const; // return the phone number as an integer
       // but without the area code

       void print(ostream & w) const; // will print ddd-ddd-dddd

       void input(void); // user types a string,with or without -'s

       void copyTo( phone & ) const; // copy invoking instance to parameter

       bool isSmaller(const phone & ) const; // return true if invoking instance
                                                           // is smaller than parameter
       bool sameAreaCode(const phone &) const; // return true if the invoking instance
       // and the parameter have
       // the same area code
       bool tollFree(void) const; // return true if a toll free number
       void SplitPhone(const char b []); //
   private:

       static const char SEPARATOR; // Class data, not member data

       static const int TOLL_FREE_EXCHANGES[COUNT_TOLL_FREE];

       int areaCode,
           theExchange,
           theLast4;
};
#endif

//phone.cpp

#include "phone.h"
#include <cmath>
#include <cstring>
#include <iostream>
using namespace::std;

const char phone::SEPARATOR = '-'; // give out the class data

const int phone::TOLL_FREE_EXCHANGES[COUNT_TOLL_FREE] = { 800, 888 };

bool phone::tollFree(void) const // return true if a toll free number
{
for(int i= 0; i < COUNT_TOLL_FREE; i++)
{
if(this->areaCode == TOLL_FREE_EXCHANGES[i])
   {
   cout<< " is a toll free exchange.";
   return true; // CHANGE
   }
}
cout << " is not a free exchange. " ;
return false;
}


phone::phone(void)
{
   setPhone(111,222,3333);
}

phone::phone(int area, int exchange, int num)
{
   phone(area, exchange, num);
}

phone::phone(const phone & other)
{
   setPhone(other.areaCode, other.theExchange, other.theExchange);
}

phone::phone(const char* s)// phone::phone(const char s[])
{
   setPhone(s);
}


phone::~phone(void)
{ }

void phone::setPhone( int area, int exchange, int num)
{
setPhone(area, exchange, num);
}


void phone::setPhone( const char sn[])
{
   /**
   * assume no error condition
   *how can I tell if the SEPARATOR is present?
   */
   char work[13];
   strcpy(work, sn);

   if(strlen(work) == 12)
   {
       // squeeze out the SEPARATORS
       strncpy(work + 3, work + 4, 3);
       strncpy(work +6, work +8, 4);
       work[10] = '';

       // take it apart
       theExchange = atoi( work + 6);
       work[6 ]= '';
       theExchange = atoi(work + 3);
       work[3] = '';
       areaCode = atoi(work);

   }  
}


void phone::getPhone(char * s) const
{ // null = 0
   s[0] =areaCode / 100 + '0' ;//NULL ;
       s[1] = (areaCode % 100) / 10 + '0';
       //s[2] = areaCode;
       //s[3] = areaCode;
}

void phone::print(ostream & w) const
{
   w << areaCode <<SEPARATOR << theExchange << SEPARATOR << theExchange << endl;
   // need to handl the case when a nubmer starts with zero

}

void phone::input(void)
{
char temp [15];
cout<<"Enter a phone number with or without dashes: ";
cin>>temp;
setPhone(temp);

}


void phone::copyTo( phone & r ) const
{
char temp[11];
getPhone(temp);
r.setPhone(temp);
  

}

bool phone::isSmaller(const phone & r) const
{
   if (areaCode < r.areaCode ){
       return true;}
   else if(areaCode > r.areaCode){
       return false;}
   else{
       return true;} // CHANGE - added to make the file compile
}

int phone::getNumber(void) const
{
//return 0; // CHANGE - added to make the file compile
   // return the phone number without the area code, with the area code
   // the phone number is 10 digits and may be larger than 2,147,xxx,xxx
   return ((theExchange * 10000) + theLast4);
}

bool phone::sameAreaCode(const phone &temp) const
{
return (areaCode ==temp.areaCode);
//true; // CHANGE - added to make the file compile
}

// write the code for the member function that you designed
void phone::SplitPhone(const char b [])
{
char temp[5];
int num, num2, num3;
strncpy(temp, b, 3);
temp[3] = '';
num = atoi(temp);

strncpy(temp, num+3, 3);
temp[3] = '';
num2 = atoi(temp);

strncpy(temp, num+6, 4);
temp[4] = '';
num3 = atoi(temp);

setPhone(num, num2, num3);


}

//driverphone.cpp

#include <iostream>
using namespace::std;
#include "phone.h"


// *********************************************************************************
// user written functions for main

void sort( phone info[], int count);
void print(ostream &, const phone info[], int, const char * message);

// Main program **************************************************************

const int ENTRIES = 5;

void main()
{

   {

       phone a;
       phone b(123, 456, 7890);
       phone c(b);
       phone d("913-469-8500");
       a.print(cout);
       b.print(cout);
       c.print(cout);
       phone e("913-469-8512");
       e.print(cout);
       phone f("1234567890");
       f.print(cout);
      
       return ;
   }
// test a few functions
   phone jccc("913-469-8500");
   int w1;
   char w2[20]; // larger than needed
   w1 = jccc.getNumber();
   jccc.getPhone(w2);
   cout << w1 << ' ' << w2 << endl;

   phone work("9138349312");
   w1 = work.getNumber();
   work.getPhone(w2);
   cout << w1 << ' ' << w2 << endl;

   if ( jccc.sameAreaCode(work))
       cout << "Same area code ";
   else
       cout << "Different area code ";

   phone duplicate(jccc);
   cout << "Test of copy constructor ";
   duplicate.print(cout);

   phone home(913,333,4444);
   cout << "Test of 3 argument constructor ";
   home.print(cout);

   // test your member function here *****************************
   cout<<" My member function splitPhone() works "
   "The splitPhone() is called each time "
   "setPhone (const char b[] is called. " ;
  

// bring in the data and sort the data
   phone friends[ENTRIES];

  

   cout << "Enter " << ENTRIES << " Phone numbers "
       << "of the form 913-469-8500 or 9134698500 ";
int i;
   for ( i = 0; i < ENTRIES ; i++)
       friends[i].input();

   print(cout,friends,ENTRIES,"First array before sorting");

   sort(friends,ENTRIES);

   print(cout,friends,ENTRIES,"Numbers after sorting");
}

// ********************** User written functions ***************

// THE FOLLOWING CODE WILL NOT BE CHANGED

void swapValues(phone &, phone &); // used in sort

int indexSmallest(const phone data[] , int start, int count); // used in sort

void sort(phone info[], int count)
{
   // Selection sort
   int smallIndex;
   for ( int i =0 ; i < count -1; i++)
   {
       smallIndex = indexSmallest(info,i,count);
       if ( i != smallIndex)
           swapValues(info[i],info[smallIndex]);
   }
}

void swapValues(phone & left, phone & right)
{
phone work;
left.copyTo(work);
right.copyTo(left);
work.copyTo(right);
}

int indexSmallest(const phone data[], int start, int numberUsed)
{
int indexMin = start;
phone min(data[start]);
for ( int i = start + 1; i < numberUsed; i++)
{
       if ( data[i].isSmaller(min) )
   {
       indexMin = i;
       data[i].copyTo(min);
   }
}
return indexMin;
}

void print(ostream & w, const phone info[], int count, const char * message)
{
   w << message << endl;
   for(int i = 0; i < count; i++)
   {
       info[i].print(w);
       w << endl;
   }
}

phone.cpp:155:3: error: no matching function for call to 'strncpy strncpy(temp, num+3, 3); /usr/include/string.h:85:7: note: candidate function not viable: no known char strncpy(char *, const char*, size.t); phone.cpp:159:3: error: no matching function for call to 'strncpy conversion from 'int' to 'const char * for 2nd argument strncpyCtemp, num+6, 4); /usr/include/string.h:85:7: note: candidate function not viable: no known char strncpy(char *, const char*, size.t); 2 errors generated. conversion from 'int' to 'const char * for 2nd argument make:[phone.o] Error 1

Explanation / Answer

The SpliPhone method should be

void phone::SplitPhone(const char b [])
{
char temp[5];
int num, num2, num3;
strncpy(temp, b, 3);
temp[3] = '';
num = atoi(temp);

strncpy(temp, b+3, 3);
temp[3] = '';
num2 = atoi(temp);

strncpy(temp, b+6, 4);
temp[4] = '';
num3 = atoi(temp);

setPhone(num, num2, num3);


}