how to use array and for loop in this assignment

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1781 Views - Last Post: 01 January 2010 - 09:16 AM Rate Topic: -----

#1 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

how to use array and for loop in this assignment

Posted 31 December 2009 - 01:57 PM

#include<iostream>
#include<string>
using namespace std;

class Package{  //Base Class
   
   private:		 //data members
		   string name,city,state,sender,recipient;
		   int zip;
		   string address;
		   float weight;  //in ounce
		   double cost;  //per ounce
					
		   
   public:		 //member functions
		  void setName(string name);
		  void setCity(string city);
		  void setState(string state);
		  void setZip(int zip);
		  void setAddress(string address);
		  void setSender(string sender);
		  void setRecipient(string recipient);
		  string getName(){ return name;}
		  string getCity(){return city;}
		  string getState(){return state;}
		  int getZip(){return zip;}
		  string getAddress(){return address;}
		  string getSender(){return sender;}
		  string getRecipient(){return recipient;}
		  
		  
		  
		  
		  
		  
		  
double calculatecost(float weight,double costperounce) //function that calculate the cost z
				 { 
						   double z;  //total cost = weight*cost per ounce
				 z=weight*costperounce;  //the cost z
				 
				 cout<< "The Base Cost = " <<z << endl<<endl;
				 return z;
				 
						   
									
				 }
				 

};

void Package::setName(string n){
	 name=n;
	 
	 }
						   
void Package::setCity(string c){
	 city=c;
	 
	 }
void Package::setState(string s){
	 state=s;
	 }
void Package::setZip (int zp){
	 zip=zp;
	 }
void Package::setAddress(string adr){
	 address=adr;
	 }
void Package::setSender(string sen)
	 {
		sender=sen;  
	 } 
void Package::setRecipient(string rec)  
	 {
		recipient=rec;						  
	 }					  
	 
	 
	 
	 
class TwoDayPackage: public Package{	//inhereted class
	  public:
					  double calcshippingcost(float weight,double costperounce,double k) /* function that calculate shipping cost by adding the flat
					  fee to the weight-based cost calculated by base class Package’s calculateCost function*/
				  {
					  double z; //shippingcost of Two day Package class
					  z= calculatecost(weight,costperounce) +k;  //where k is the ???????
					  cout<< "The TwoDayPackage Cost = " <<z << endl;			   
				  }  
			 
			 
	  private:
			  double flatfee;	   
	 
};

class OvernightPackage: public Package{  //the inhereted function thatadds the additional fee per ounce to the standard cost per ounce  
	  public:
			 double calccostovernight(float weight,double costperounce,double w )
				  {
					  double z; //shippingcost of overnight class
					  z=calculatecost(weight,costperounce)+(w*weight);
					  cout<< "The OvernightPackage Cost = " <<z << endl;			   
				  }  
			 
			 
			 
	  private:
			  double overnightcost; //per ounce
};




int main(){
	
	// weight and costperounce sabteen fe kol el classes y3ne hane7teghom fel kol el mataleb
	int i;  //i represent the user`s choice number
	string customername,customeraddress,city,state,senderaddress,recipientaddress;
	float packageweight;
	string customercity;
	double costperounce;
	double flatfee;
	double additionalcost;
	string customerstate;
	int customerzipcode;
	
	Package base;   //the object base of the package class
	TwoDayPackage twoday;  //the object twoday of the first inhereted calss
	OvernightPackage overnight;   //the object overnight of the second inhereted calss
	/*int  package[3];
	for()
	  
	int packag={"TwoDaypackage","overnightpackage"};
	int *packag;
	
*/  

	cout<<"	   *****Welcome To The American Package Delievery Services*****"<<endl<<endl;
	cout<<"Please Fill In The Requested Data Follow:	"<<endl<<"-----------------------------------------"<<endl<<endl;;
	
	
	cout<<"Enter Customer Name "<<endl<<endl;
	cin>>customername;
	cout<<endl;
	base.setName(customername);  //call function setName
	
	
	cout<<"Enter Customer Address"<<endl<<endl;
	cin>>customeraddress;
	cout<<endl;
	base.setAddress(customeraddress);

	cout<<"Enter Customer City"<<endl<<endl;
	cin>>customercity;
	cout<<endl;
	base.setCity(customercity);
	
	cout<<"Enter Customer State"<<endl<<endl;
	cin>>customerstate;
	cout<<endl;
	base.setState(customerstate);
	
	cout<<"Enter Customer ZIP code"<<endl<<endl;
	cin>>customerzipcode;
	cout<<endl;
	base.setZip(customerzipcode);
	
	
	cout<<"Enter Weight"<<endl;
	cin>>packageweight;
	cout<<endl;
	cout<<"Enter Cost Per Ounce"<<endl;
	cin>>costperounce;
	cout<<endl;
	
	
	
	cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl;
	cout<<"   1- Calculate Base Cost  "<<endl<<endl;
	cout<<"   2- Calculate Two Day Cost "<<endl<<endl;
	cout<<"   3- Calculate Over Night Cost"<<endl<<endl;
	cin>>i;
	cout<<endl;  //i represent customer choice
	
	
	switch (i)
	{
		   case 1 :
		   base.calculatecost(packageweight,costperounce);
		   break;
		   
		   case 2 :
		   cout<<"Enter Flat Cost"<<endl<<endl;  //additonal(to weight and cost) needed information 
		   cin>>flatfee;
		   twoday.calcshippingcost(packageweight,costperounce,flatfee);
			break;
		   
		   
		   case 3 :
		   cout<<"Enter The Additional Cost"<<endl<<endl;
		   cin>>additionalcost;
		   overnight.calccostovernight(packageweight,costperounce,additionalcost);
			break;
		   
		   default:
			cout<<"INVALID CHOICE....Please Enter ur Choice Number From 1-->3 "<<endl;
		   
		   
	  }
	  
   
	cout<<"Enter sender address "<<endl<<endl;
	cin>>senderaddress;
	cout<<endl;
	base.setSender( senderaddress);
	
	cout<<"Enter ricipent address"<<endl<<endl;
	cin>>recipientaddress;
	cout<<endl;
	base.setRecipient(recipientaddress);   
	
	cout<<"address from:"<< senderaddress<<endl;
	cout<<"To:"<<recipientaddress<<endl;

[b]// [color=#FF0000] int  package[3];
	for(int i=0;i<3;i++)
	{
	   if(i=0)
	   {
}			 
	  
	int packag={"TwoDaypackage","overnightpackage"};
	int *packag;[/[/color]b]
		
system ("pause");
return 0;






problem is(Use the Package inheritance hierarchy created in the previous problem to create a program
that displays the address information and calculates the shipping costs for several Packages. The
program should contain an array of Package pointers to objects of classes TwoDayPackage and
OvernightPackage. Loop through the array to process the Packages polymorphically. For each
Package, invoke get functions to obtain the address information of the sender and the recipient,
then print the two addresses as they would appear on mailing labels. Also, call each Package’s
calculateCost member function and print the result. Keep track of the total shipping cost for all
Packages in the array, and display this total when the loop terminates.)



i want to replay very fast

Is This A Good Question/Topic? 0
  • +

Replies To: how to use array and for loop in this assignment

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 03:05 PM

do you have a specific question or problem that your having, or should we just do your homework assignment for you?
Was This Post Helpful? 0
  • +
  • -

#3 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 04:59 PM

Does Nobody read the "READ THIS FIRST" statement?

Google search: C++ Loops
Infact, I did it for you!
http://www.cprogramm...al/lesson3.html

^^ I suggest you add that site to your favorites. ^^

Also, We here at DIC have many talented coders/guru. And thay work tirelessly to design this: http://www.dreaminco...showforum78.htm

Here is a DIC tut, thanks to rahulbatra(please rate this post, this person put allot of time into it):http://www.dreamincode.net/forums/showtopic13919.htm
Was This Post Helpful? 0
  • +
  • -

#4 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:04 PM

View PostImaSexy, on 31 Dec, 2009 - 02:05 PM, said:

do you have a specific question or problem that your having, or should we just do your homework assignment for you?


no i want to know how to use array in my assignment and give example
what ido in this array use array or pointers
 string package [3];
	package[0]={"base"}
	package[1]={"twoday"}
	package[2]={"overnight"}
	
		

This post has been edited by mon mon: 31 December 2009 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:15 PM

umm...
string Package[3];
for(int a=0;a < 2;a++)
{
	cout << "Enter a line: ";
	getline(cin, Package[a]);
}



Not sure what you are doing. If its going to be unique input from the user that would work, but if your going to define them inside your code then a loop is poinless.

This post has been edited by Delta_Echo: 31 December 2009 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#6 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:20 PM

here are a few tuts on pointers and Arrays:
http://www.cprogramm...al/lesson8.html << arrays
http://www.cprogramm...al/lesson6.html << pointers
Was This Post Helpful? 0
  • +
  • -

#7 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:24 PM

View Postmon mon, on 31 Dec, 2009 - 04:04 PM, said:

View PostImaSexy, on 31 Dec, 2009 - 02:05 PM, said:

do you have a specific question or problem that your having, or should we just do your homework assignment for you?


no i want to know how to use array in my assignment and give example
what ido in this array use array or pointers
 string package [3];
	package[0]->
	package[1]->				  //write function after arow that i want to call
	package[2]->
	
		

Was This Post Helpful? 0
  • +
  • -

#8 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:30 PM

What??? What function do you want to call? What are you trying to do?

This post has been edited by Delta_Echo: 31 December 2009 - 05:33 PM

Was This Post Helpful? 0
  • +
  • -

#9 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:40 PM

View PostDelta_Echo, on 31 Dec, 2009 - 04:30 PM, said:

What??? What function do you want to call? What are you trying to do?

i'm so sorry for distribuance you and thanks for help me
but i want to read to understand the problem
this problem
The
program should contain an array of Package pointers to objects of classes TwoDayPackage and
OvernightPackage. Loop through the array to process the Packages polymorphically. For each
Package, invoke get functions to obtain the address information of the sender and the recipient,
then print the two addresses as they would appear on mailing labels. Also, call each Package’s
calculateCost member function and print the result. Keep track of the total shipping cost for all
Packages in the array, and display this total when the loop terminates.
Was This Post Helpful? 0
  • +
  • -

#10 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:47 PM

If all this information is supose to be contained in Package[], whay are you using the string type? Why don't you use a class or struct that would contain all the information as member variables?
Was This Post Helpful? 0
  • +
  • -

#11 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:53 PM

View PostDelta_Echo, on 31 Dec, 2009 - 04:30 PM, said:

What??? What function do you want to call? What are you trying to do?

itry to find solution for my problem after that i remember every idea

View PostDelta_Echo, on 31 Dec, 2009 - 04:47 PM, said:

If all this information is supose to be contained in Package[], whay are you using the string type? Why don't you use a class or struct that would contain all the information as member variables?

how use it but i study oop and c++ not data structured
give me example for it
Was This Post Helpful? 0
  • +
  • -

#12 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 05:54 PM

A class is really just a collection of data and functions that manipulate said data.
Here is a great tutorial on classes: http://www.cprogramm...l/lesson12.html

This post has been edited by Delta_Echo: 31 December 2009 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#13 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 06:24 PM

View Postmon mon, on 1 Jan, 2010 - 12:04 AM, said:

no i want to know how to use array in my assignment and give example
what ido in this array use array or pointers
 string package [3];
	package[0]={"base"}
	package[1]={"twoday"}
	package[2]={"overnight"}

If you need to ask questions like this then you need to go and spend time working with small toy programs in order to understand exactly how arrays work. Don't think that you'll try to be clever and just work them into a larger program - you'll end up spending more time guessing and getting it wrong than if you'd spent a few hours studying the basics.
Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 06:57 PM


struct package
{
   string something;
};

int main()
{
	//this is an array of 10 strucutres(package)
   package *overnight = new package[10]; 



its hard to understand what your trying to do because you do not explain what your assignment is very well
Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,433
  • Joined: 10-May 07

Re: how to use array and for loop in this assignment

Posted 31 December 2009 - 10:19 PM

I used an array of structures in my store program.

Though I have to agree with the others in that you are not really getting to the point very well :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2