Looping problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int main(){//Declare an constant array of pointer to 6 Shipping Container Object
 	const int TOTAL_CONTAINERS = 6; 
 	ShippingContainer *sc[TOTAL_CONTAINERS]; 
	string theContent;
  
	int RFID_array; 
	cout << "Enter number of RFID container : ";
	cin  >> RFID_array;
	cin.ignore();

	RFIDShippingContainer *RFID_item = new RFIDShippingContainer[RFID_array];

	for( int i = 0 ; i < RFID_array ; i ++ ){
		cout << "Enter Content of item : ";
		getline( cin, theContent );
		RFID_item->add( theContent );
		sc[i] = RFID_item;
	}

	int ManualShipping_array;
	cout << "Enter number of Manual container : ";
	cin  >> ManualShipping_array;
	cin.ignore();

	ManualShippingContainer *Manual_item = new ManualShippingContainer[ManualShipping_array];
	for( int i = 0 ; i < ManualShipping_array ; i ++ ){
		cout << "Enter Content of item : ";
		getline( cin, theContent );
		Manual_item->setManifest( theContent );
	}
	for ( int i = 0; i < 2; i++ ) 
 	{ 
 		cout << sc[ i ]->getManifest() << endl; 
 	}

delete []RFID_item; //deallocate the memory for RFID containers
 	delete []Manual_item; //deallocate the memory for Manual Shipping containers
  
	system( "pause" );//System pause
 	return 0; //Exit the program
}


how should i declare in the loop ? according to print it .
Last edited on
1
2
3
	//Declare an constant array of pointer to 6 Shipping Container Object
	const int TOTAL_CONTAINERS = 6; 
	ShippingContainer *sc[TOTAL_CONTAINERS];
Sorry, but that comment doesn't match the code. You have an non-constant array of 6 pointer to Shipping Container, not an constant array of pointer to 6 Shipping Container.

As for your question, which loop?
here my question
write a main program that creates an array of pointers to 6 ShippingContainer objects. The array should be used to store both the ManualShippingContainer objects and RFIDShippingContainer objects. Construct the main program to allow the user to decide the number of each type of objects to be stored in the array. Instantiate all the objects accordingly and demonstrate how each objects behaves differently when the same instructions are given to them. For the ManualShippingContainer objects, you will have to invoke setManifest function to set the contents. For the RFIDShippingContainer objects, you will have to invoke add to set the contents (although, if this were real, the contents of the container would “add” themselves via the RFID chips instead of requiring a human to type them in).

so which part that I wrong at?
Assignment wrote:
write a main program that creates an array of pointers to 6 ShippingContainer objects.
Since this asks for an array of pointers, which is unusual, it probably means that you will have a maximum of 6 to deal with but you may also have less than 6.[quote=AssignmentThe array should be used to store both the ManualShippingContainer objects and RFIDShippingContainer objects.[/quote]This means you need one array for both kinds. I assume that ManualShippingContainer and RFIDShippingContainer are classes that inherit from ShippingContainer, using polymorphism.[quote=Assignment]Construct the main program to allow the user to decide the number of each type of objects to be stored in the array.[/quote]Aha, just as I thought. You have a cap of 6 total, but it can also be less. Make sure that the combined total of the two kinds is <= 6. If they enter 6 for the first kind you shouldn't need to ask them how many for the second kind, since it must be 0.
Assignment wrote:
Instantiate all the objects accordingly and demonstrate how each objects behaves differently when the same instructions are given to them.
Yep, polymorphism. You have to call methods from the context of a generic ShippingContainer, and C++ will automatically figure out what kind of conainer it actually is and call the correct methods.
Assignment wrote:
For the ManualShippingContainer objects, you will have to invoke setManifest function to set the contents. For the RFIDShippingContainer objects, you will have to invoke add to set the contents
This is something that should be done before you place the instance into the array, when you still can be sure that it is of the type you think it is.

Basic layout of this program:
-create the array of 6 pointers to ShippingContainer
-ask the user how many ManualShippingContainers they want over and over until they give an answer less than or equal to six
-if their answer was less than 6, ask them how many RFIDShippingContainers they want over and over until the combined total is less than or equal to six
-for each ManualShippingContainer ask them for the information required to create one and then set the pointer in the array
-for each RFIDShippingContainer ask them for the information required to create one and then set the pointer in the correct place in the array
-for each ShippingContainer in the array, call some function to show that the same code has different behavior due to polymorphism
-deallocate the memory you created with new by using delete

Your current program is close but you should only be using the array of pointers to ShippingContainer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int main() { 

	string theContent;
	int RFID_array = 0;
	int ManualShipping_array = 0;
	int totalContainerArray = 0;

	//that creates an array of pointers to 6 ShippingContainer objects
 	const int TOTAL_CONTAINERS = 6; 
 	ShippingContainer *sc[TOTAL_CONTAINERS];
	
	
	do{ 
		cout << "Enter number of RFID container : ";
		cin  >> RFID_array;

		cout << "Enter number of Manual container : ";
		cin  >> ManualShipping_array;
		cin.ignore();

		totalContainerArray = RFID_array + ManualShipping_array;

		if( totalContainerArray > 6 || totalContainerArray < 1 )
			cout << "Invalid Input ! " << endl << endl;
	}while( totalContainerArray > 6 || totalContainerArray < 1 );

	RFIDShippingContainer *RFID_item = new RFIDShippingContainer[RFID_array];

	cout << endl;

	for( int i = 0 ; i < RFID_array ; i ++ ){
		cout << "Enter item for RFID container : ";
		getline( cin, theContent );
		RFID_item->add( theContent );
		//sc[i] = RFID_item;
	}

	cin.ignore();

	ManualShippingContainer *Manual_item = new ManualShippingContainer[ManualShipping_array];
	for( int i = 0 ; i < ManualShipping_array ; i ++ ){
		cout << "\nEnter item for manual shipping container : ";
		getline( cin, theContent );
		Manual_item->setManifest( theContent );
	}

	for ( int i = 0; i < 6; i++ ) 
 	{ 
 		cout << sc[ i ]->getManifest() << endl; 
 	}


i having problem for the last loop . how to cout the getManifest()?
and for line 35. how should i declare it?
Last edited on
As I said before, you should only keep one array. I basically outlined the whole program for you. Also, I don't know what the ShippingContainer classes look like or what methods they have, so I don't know how to answer your specific question.
i get it after i wake up

isnt u mean this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
RFID_item->add( "crate of pears" ); 
 	RFID_item->add( "crate of apples" ); 
 	RFID_item->add( "crate of pears" );
	RFID_item->add( "crate of pears" );
  
 	sc[0] = RFID_item;

	RFID_item->add( "crate of mangoes" );
	RFID_item->add( "crate of bananas" );
	RFID_item->add( "crate of bananas" );

	sc[1] = RFID_item;

	RFID_item->add( "crate of bananas" );
	RFID_item->add( "crate of bananas" );
	RFID_item->add( "crate of pears" );
	RFID_item->add( "crate of pears" );

	sc[2] = RFID_item;
No, that would be manually adding data without bothering to ask the user what they want to add.
err

my concept correct already?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
int main() { 

	int counter = 0;
	//creates an array of pointers to 6 ShippingContainer objects
 	const int TOTAL_CONTAINERS = 6; 
	int numContainerRemain = TOTAL_CONTAINERS;
 	ShippingContainer *sc[TOTAL_CONTAINERS]; 
  
	//create an pointer that point to RFID Shipping Container
 	RFIDShippingContainer *RFID_item = new RFIDShippingContainer(); 
	//Create an pointer that point to ManualShippingContainer
 	ManualShippingContainer *Manual_item = new ManualShippingContainer(); 

		//cout << "How many RFID containers you want to have : ";
		int numOfRFID;
		//cin  >> numOfRFID;
		//cin.ignore();//Clear buffer
		//numContainerRemain -= numOfRFID;//Counting number remaining for containers
		
		if(numContainerRemain >= 0){
			do{
				if( numContainerRemain != 0 || numContainerRemain < 6 ){
					cout << "How many RFID containers you want to have : ";
					cin  >> numOfRFID;
					cin.ignore();//Clear buffer
					numContainerRemain -= numOfRFID;//Counting number remaining for containers

					cout << "Number Remaining for total containers : " << numContainerRemain << endl << endl;
					for( int i = 0 ; i < numOfRFID ; i++ ){
						cout << "Enter item for RFID containers : ";
						string theItem;
						getline( cin , theItem );
						RFID_item->add(theItem);
					}
					counter++;
					sc[counter] = RFID_item;//Assign each element of array assigned to a new object
					cout << sc[ counter ]->getManifest() << endl; //display the getManifest() list for Shipping Container
				}
				if( numContainerRemain != 0 ){
					cout << "\nHow many Manual containers you want to have : ";
					int numOfManual;
					cin  >> numOfManual;//Input for manual Shipping Container
					cin.ignore();//Clear buffer memory
					numContainerRemain -= numOfManual;//Counting remaining number for container

					cout << "Number Remaining for total containers : " << numContainerRemain << endl << endl;

					for( int i = 0 ; i < numOfManual ; i++ ){
						cout << "Enter item for Manual containers : ";
						string theManifest;
						getline( cin , theManifest );
						Manual_item->setManifest( theManifest );//Set manifest to the Manual ShippingContainer
					}
					counter++; 
					sc[counter] = Manual_item;//Assign each element of array assigned to a new object
					cout << sc[ counter ]->getManifest() << endl << endl;//display the getManifest() list for Shipping Container
				}
			}while( numContainerRemain != 0 || numContainerRemain > 0 );
		}
		else
			cout << "Error ! Invalid Input" << endl;
  
	//Free all the memory
 	delete []RFID_item; //deallocate the memory for RFID containers
 	delete []Manual_item; //deallocate the memory for Manual Shipping containers

  
	system( "pause" );//System pause
 	return 0; //Exit the program
 }
my concept correct already?
Nope, your second attempt comes closer to the problem.

As L B said it's a matter of polymorphism. That involves inheritance and overriding virtual functions. This:

http://en.wikipedia.org/wiki/Virtual_function

shows how it is done.

So show how you defined ShippingContainer/RFIDShippingContainer/ManualShippingContainer.

if RFIDShippingContainer and ManualShippingContainer are subclasses from ShippingContainer you can store pointers to them in your sc array!
Topic archived. No new replies allowed.