Design a PhoneCall class that holds a phone number to which a call is placed, the length of the call in minutes, and the rate charged per minute. Overload extraction and insertion operations for the class.

Overload the == operator to compare two PhoneCalls. Consider one PhoneCall to be equal to another if both calls are placed to the same number.

Create a main() function that allows you to enter 10 PhoneCalls into an array. If a PhoneCall is already been placed to a number, do not allow a second PhoneCall to the same number

I'm pretty good at logic so i think i can figure that part out, its the syntax and the declarations that i cant wrap my head around

please help!
thank you.

Recommended Answers

All 14 Replies

i guess the main thing for now is i need help starting this program... what should the declarations at the top look like?
what should the class look like?
stuff like that

#include "stdafx.h"


class PhoneCall
{
	friend ostream& operator<<(ostream&, const PhoneCall&);
private:
	double callRate;
	int callLength, phoneNumber;
public:
	PhoneCall(int, double, int);
	void setLength(int const *length);
    void setRate(double const *rate);
    void setPhoneNumber(int const *num);
	int operator==(const PhoneCall& aPhone);

};

void PhoneCall::setLength(int const *length)
    {
        delete length;
        //length = strdupnew(length);
    }

void PhoneCall::setRate(double const *rate)
{
	delete rate;
	//rate=strdupnew(rate);
}

void PhoneCall::setPhoneNumber(int const *num)
{
	delete num;
	//num=strdupnew(num);
}


PhoneCall::PhoneCall(int length, double rate, int num)
{
	callLength=length;
	callRate=rate;
	phoneNumber=num;
}



	ostream &operator<<(ostream& out, const PhoneCall& aPhone)
		{
        return
            out <<
                "Call Length:    " << aPhone.callLength() <<
                "Call Rate: " << aPhone.callRate() <<
                "Phone Number:   " << aPhone.phoneNumber();
    }

	int PhoneCall::operator==(const PhoneCall& aPhone)
	{
		int truth=0;
		if(phoneNumber=aPhone.phoneNumber)
			truth=1;
		return truth;
	}




	
	int main()
	{
		PhoneCall aPhone(10, 5.25, 1234567);
		cout<< aPhone;
		return 0;
		system("pause");
	
	}
#include "stdafx.h"


class PhoneCall
{
	friend ostream& operator<<(ostream&, const PhoneCall&);
private:
	double callRate;
	int callLength, phoneNumber;
public:
	PhoneCall(int, double, int);
	void setLength(int const *length);
    void setRate(double const *rate);
    void setPhoneNumber(int const *num);
	int operator==(const PhoneCall& aPhone);
	ostream& operator<<(const PhoneCall& aPhone);

};

void PhoneCall::setLength(int const *length)
    {
        delete length;
        //length = strdupnew(length);
    }

void PhoneCall::setRate(double const *rate)
{
	delete rate;
	//rate=strdupnew(rate);
}

void PhoneCall::setPhoneNumber(int const *num)
{
	delete num;
	//num=strdupnew(num);
}


PhoneCall::PhoneCall(int length, double rate, int num)
{
	callLength=length;
	callRate=rate;
	phoneNumber=num;
}



	ostream &operator<<(ostream& out, const PhoneCall& aPhone)
		{
        return
            out <<
                "Call Length:    " << aPhone.callLength() <<
                "Call Rate: " << aPhone.callRate() <<
                "Phone Number:   " << aPhone.phoneNumber();
    }

	int PhoneCall::operator==(const PhoneCall& aPhone)
	{
		int truth=0;
		if(phoneNumber=aPhone.phoneNumber)
			truth=1;
		return truth;
	}




	
	int main()
	{
		PhoneCall aPhone(10, 5.25, 1234567);
		cout<< aPhone;
		return 0;
		system("pause");
	
	}
#include "stdafx.h"
#include<conio.h>


class PhoneCall
{
	friend ostream& operator<<(ostream&, const PhoneCall&);
	friend istream& operator>>(istream&, PhoneCall&);
private:
	double callRate;
	int callLength, phoneNumber;
public:
	PhoneCall(int, double, int);
	void setLength(int);
	void setNumber(int);
	void setRate(double);
	//void setLength(int const *length);
   // void setRate(double const *rate);
    //void setPhoneNumber(int const *num);
	int operator==(const PhoneCall& aPhone);
	//ostream& operator<<(const PhoneCall& aPhone);

};


void PhoneCall::setLength(int l)
{
	
	cout<<"enter the length of the phone call";
	cin>>l;
	return;
}

void PhoneCall::setRate(double r)
{
	
	cout<<"enter rate per minute";
	cin>>r;
	return;
}

void PhoneCall::setNumber(int n)
{
	
	cout<<endl<<"enter phone number in format xxxxxxx";
	cin>>n;
	return;
}


/*void PhoneCall::setLength(int const *length)
    {
        delete length;
        //length = strdupnew(length);
    }

void PhoneCall::setRate(double const *rate)
{
	delete rate;
	//rate=strdupnew(rate);
}

void PhoneCall::setPhoneNumber(int const *num)
{
	delete num;
	//num=strdupnew(num);
}
*/

PhoneCall::PhoneCall(int length, double rate, int num)
{
	callLength=length;
	callRate=rate;
	phoneNumber=num;
}



	ostream &operator<<(ostream& out, const PhoneCall& aPhone)
		{
        return
            out <<
                "Call Length:    " << aPhone.callLength <<endl<<
                "Call Rate: " << aPhone.callRate <<endl<<
                "Phone Number:   " << aPhone.phoneNumber<<endl;
    }

	istream& operator>>(istream& in, PhoneCall& aPhone)
	{
		cout<<endl;
		cout<<"enter call length in minutes, round to the nearest minute";
		in>>aPhone.callLength;
		cout<<endl<<"enter rate per minute in dollars and cents";
		in>>aPhone.callRate;
		cout<<endl<<"enter phone number in form xxxxxxx";
		in>>aPhone.phoneNumber;
		return in;
	}



	int PhoneCall::operator==(const PhoneCall& aPhone)
	{
		int truth=0;
		if(phoneNumber=aPhone.phoneNumber)
			truth=1;
		return truth;
	}




	
	int main()
	{
		
		
		PhoneCall aPhone(0, 0, 0);
		cin>>aPhone;
		cout<< aPhone;

		//return 0;
		//system("pause");
		getche();
	
	}

i think i did part a and b
so now i need to somehow let a user enter 10 PhoneCalls into an array and compare them so the same phone number isnt entered twice....whats the syntax on that kind of array?
and i overloaded == to compare phone numbers so how do i use that in the main() to stop from repeating the same phone number twice?

please help!!! i am trying, just failing hardcor3

This is what I would do for your main() function.

int main()
{
	bool numberUsed;
	PhoneCall calls[10];
	for( int i = 0; i < 10; i++ )
	{
		do
		{
			numberUsed = false;
			cin >> calls[i];
			for( int c = 0; c < 10; c++ ) //go through all phone calls
				if( c != i ) //if is not the same phone call
					if( calls[i].phoneNumber == calls[c].phoneNumber ) //if the numbers match
						numberTaken = true; //number is taken
			
		}while(numberUsed); //do while numberTaken is true
	}
	
	for( int i = 0; i < 10; i++ )
	{
		cout << calls[i];
	}
	
	system("pause");
	return 0;

}

Change the constructor for your PhoneCall class to PhoneCall(int length = 0, double rate = 0, int num = 0); This way when you make the PhoneCalls and do not specifiy a value for each variable it just defaults them to zero.

OK my above post works but there are a few things that you have to do that I missed out.
-First change line 14 to numberUsed (not sure why I put numberTaken)
-Either create a getPhoneNumber() function that returns the phoneNumber or make it public (up to you)
-Add an error message saying that the phone number is taken and I would also output something showing what PhoneCall you are entering for (could use the i counter +1)

thank you so much, im very close now...however i am getting error c4430... did i set up what you suggested correctly?

#include "stdafx.h"
#include<conio.h>


class PhoneCall
{
	friend ostream& operator<<(ostream&, const PhoneCall&);
	friend istream& operator>>(istream&, PhoneCall&);
private:
	double callRate;
	int callLength;
public:
	PhoneCall(int, double, int);
	int phoneNumber;
	int getPhoneNumber(int);
	void setLength(int);
	void setNumber(int);
	void setRate(double);
	//void setLength(int const *length);
   // void setRate(double const *rate);
    //void setPhoneNumber(int const *num);
	int operator==(const PhoneCall& aPhone);
	//ostream& operator<<(const PhoneCall& aPhone);

};


void PhoneCall::setLength(int l)
{
	
	cout<<"enter the length of the phone call";
	cin>>l;
	return;
}

void PhoneCall::setRate(double r)
{
	
	cout<<"enter rate per minute";
	cin>>r;
	return;
}

void PhoneCall::setNumber(int n)
{
	
	cout<<endl<<"enter phone number in format xxxxxxx";
	cin>>n;
	return;
}


/*void PhoneCall::setLength(int const *length)
    {
        delete length;
        //length = strdupnew(length);
    }

void PhoneCall::setRate(double const *rate)
{
	delete rate;
	//rate=strdupnew(rate);
}

void PhoneCall::setPhoneNumber(int const *num)
{
	delete num;
	//num=strdupnew(num);
}
*/

PhoneCall::PhoneCall(int length=0, double rate=0, int num=0)
{
	callLength=length;
	callRate=rate;
	phoneNumber=num;
}

PhoneCall::getPhoneNumber(int n=0)
{
	phoneNumber=n;

	
}



	ostream &operator<<(ostream& out, const PhoneCall& aPhone)
		{
        return
            out <<
                "Call Length:    " << aPhone.callLength <<endl<<
                "Call Rate: " << aPhone.callRate <<endl<<
                "Phone Number:   " << aPhone.phoneNumber<<endl;
    }

	istream& operator>>(istream& in, PhoneCall& aPhone)
	{
		cout<<endl;
		cout<<"enter call length in minutes, round to the nearest minute";
		in>>aPhone.callLength;
		cout<<endl<<"enter rate per minute in dollars and cents";
		in>>aPhone.callRate;
		cout<<endl<<"enter phone number in form xxxxxxx";
		in>>aPhone.phoneNumber;
		return in;
	}



	int PhoneCall::operator==(const PhoneCall& aPhone)
	{
		int truth=0;
		if(phoneNumber=aPhone.phoneNumber)
			truth=1;
		return truth;
	}




	
	int main()
	{
		
		
		PhoneCall aPhone(0, 0, 0);
		cin>>aPhone;
		cout<< aPhone;

		//return 0;
		//system("pause");
		//getche();
	
	
   
      

      bool numberUsed;

      PhoneCall calls[10];
   
      for( int i = 0; i < 10; i++ )
   
      {
 
      do

      {
   
      numberUsed = false;
  
      cin >> calls[i];

      for( int c = 0; c < 10; c++ ) //go through all phone calls

      if( c != i ) //if is not the same phone call
 
      if( calls[i].phoneNumber == calls[c].phoneNumber ) //if the numbers match
  
      numberUsed = true; //number is taken
  
       
  
      }
	  while(numberUsed); //do while numberTaken is true
  
      }

       

      for( int i = 0; i < 10; i++ )
  
      {
 
      cout << calls[i];
 
      }
 
      system("pause");

      return 0;
 
      }

Close but here are the changes that stuck out for me

class PhoneCall
{
	//private stuff
	public:
	PhoneCall(int length = 0, double rate = 0, int num = 0); //set values on prototype
	//rest of public stuff
	
};

PhoneCall::PhoneCall(int length, double rate, int num) //do not set the values on this one
{
	callLength = length;
	callRate = rate;
	phoneNumber = num;
}

Close but here are the changes that stuck out for me

class PhoneCall
{
	//private stuff
	public:
	PhoneCall(int length = 0, double rate = 0, int num = 0); //set values on prototype
	//rest of public stuff
	
};

PhoneCall::PhoneCall(int length, double rate, int num) //do not set the values on this one
{
	callLength = length;
	callRate = rate;
	phoneNumber = num;
}

ohhh, i see what you mean
ok, i changed that
i still have error c4430 on line 80 tho...not really sure why

Because you made the same mistake as your constructor you only assign values to parameters in prototypes or if there is no prototype.

So you have int n = 0 when you want that to just be int n and then inside your prototype (the function definition within the class) you put int n = 0

Because you made the same mistake as your constructor you only assign values to parameters in prototypes or if there is no prototype.

So you have int n = 0 when you want that to just be int n and then inside your prototype (the function definition within the class) you put >int n = 0

if im understanding you correctly and i hope i am, up in the class definition in public i put:

int getPhoneNumber(int n=0);

and then in the declaration i have:

PhoneCall::getPhoneNumber(int n)
{
    phoneNumber=n;
}

im still getting error c4430 in the same spot.

The problem is that you have no return type on this.

PhoneCall::getPhoneNumber(int n)
{
	phoneNumber=n;	
}

Your phone number is an int so you want

int PhoneCall::getPhoneNumber(int n)
{
	phoneNumber=n;	
}

The problem is that you have no return type on this.

PhoneCall::getPhoneNumber(int n)
{
	phoneNumber=n;	
}

Your phone number is an int so you want

int PhoneCall::getPhoneNumber(int n)
{
	phoneNumber=n;	
}

it works!!!!
EUREKA!
you are the man
thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.