# how to use array and for loop in this assignment

• (2 Pages)
• 1
• 2

## 16 Replies - 2436 Views - Last Post: 01 January 2010 - 09:16 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=147743&amp;s=2c3318830257068798799ba783ddb98e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mon mon

Reputation: -1
• 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;
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 setSender(string sender);
void setRecipient(string recipient);
string getName(){ return name;}
string getCity(){return city;}
string getState(){return state;}
int getZip(){return zip;}
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::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
float packageweight;
string customercity;
double costperounce;
double flatfee;
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<<endl;

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<<"   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 :
break;

default:
cout<<"INVALID CHOICE....Please Enter ur Choice Number From 1-->3 "<<endl;

}

cout<<endl;

cout<<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

• Engineer

Reputation: 1120
• Posts: 4,646
• 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?

### #3 Delta_Echo

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

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

Posted 31 December 2009 - 04:59 PM

Infact, I did it for you!
http://www.cprogramm...al/lesson3.html

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

### #4 mon mon

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

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

Posted 31 December 2009 - 05:04 PM

ImaSexy, 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

### #5 Delta_Echo

Reputation: 5
• 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

### #6 Delta_Echo

Reputation: 5
• 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

### #7 mon mon

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

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

Posted 31 December 2009 - 05:24 PM

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

ImaSexy, 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]->

```

### #8 Delta_Echo

Reputation: 5
• 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

### #9 mon mon

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

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

Posted 31 December 2009 - 05:40 PM

Delta_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.

### #10 Delta_Echo

Reputation: 5
• 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?

### #11 mon mon

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

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

Posted 31 December 2009 - 05:53 PM

Delta_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

Delta_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

### #12 Delta_Echo

Reputation: 5
• 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

### #13 Bench

• D.I.C Lover

Reputation: 859
• Posts: 2,343
• Joined: 20-August 07

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

Posted 31 December 2009 - 06:24 PM

mon 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.

### #14 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• 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

### #15 no2pencil

• Professor Snuggly Pants

Reputation: 5890
• Posts: 28,852
• 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