Форум » C/C++ для начинающих (C/C++ for beginners) » Just ask some question » Ответить

Just ask some question

BasicNewbie1: @vlad for the last time RFID container add() 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. I'm not really get it what is allow user to decide the number of each type of object to be stored in array . what is this mean?

Ответов - 16

BasicNewbie1: [pre2]int RFID_array; cout << "Enter number of RFID container : "; cin >> RFID_array; sc[RFID_array] = RFID_item; int ManualShipping_array; cout << "Enter number of Manual container : "; cin >> ManualShipping_array; sc[ManualShipping_array] = Manual_item;[/pre2] am i correct?

BasicNewbie1: [pre2]const int TOTAL_CONTAINERS = 6; ShippingContainer *sc[TOTAL_CONTAINERS]; int RFID_array; cout << "Enter number of RFID container : "; cin >> RFID_array; RFIDShippingContainer *RFID_item = new RFIDShippingContainer[RFID_array]; int ManualShipping_array; cout << "Enter number of Manual container : "; cin >> ManualShipping_array; ManualShippingContainer *Manual_item = new ManualShippingContainer[ManualShipping_array];[/pre2] do some changes. isnt correct?

BasicNewbie1: [pre2]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; 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 ); } int ManualShipping_array; cout << "Enter number of Manual container : "; cin >> ManualShipping_array; 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 ); }[/pre2] so how i display all the getManifest for ShippingContainer ? sc[0] = RFID_item; sc[1] = Manual_item; << not really know how to declare since my array is been insert by user so how should i declare them?


BasicNewbie1: [pre2]//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 = 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; }[/pre2] how should i declare in the loop ? according to print it .

Сыроежка: I see two possibilities. The first one is that you declare an array of type ShippingContainer * with fixed size and then you use function std::rand to determine how many containers will be of type RFIDShippingContainer or of type ManualShipping. Or you can ask a user to enter the number of for example RFIDShippingContainer containers. Another approach is that you at first ask a user how many containers should be of type RFIDShippingContainer and of type ManualShipping and then you dynamically allocate an array with the size equal to the sum of the two numbers. There is another (the third) idea. You have an array of fixed size. And before assigning an object of ShippingContainer to an array element you ask a user what kind of container he wants to have. Also you can define a constant array of strings which will contain manifests and then you will be pick randomly a manifest to add it to a given RFIDShippingContainer container.

BasicNewbie1: but maximum container is 6 only? then how i called the ShippingContainer* out with fixed size? because just now i tried to use looping to call it out, the result of output was repeating , and i'm not really know how to display it. for third idea . u mean that i should do a switch( choice ) menu? if choice = 1 mean go the RFID container that i want to have if choice = 2 mean go the manual container that i want to have? isn't this u mean ?

BasicNewbie1: for the second choice . even thought i did it like this way [pre2]int RFID_array; cout << "Enter number of RFID container : "; cin >> RFID_array; cin.ignore(); int ManualShipping_array; cout << "Enter number of Manual container : "; cin >> ManualShipping_array; cin.ignore(); RFIDShippingContainer *RFID_item = new RFIDShippingContainer[RFID_array]; ManualShippingContainer *Manual_item = new ManualShippingContainer[ManualShipping_array]; [/pre2] then how should call the shippingcontainer* out? confuse with it

Сыроежка: You have two choices. Either you declare an array of fixed size as for example ShippingContainer *sc[N]; where N - any constant integral expression. Or you can allocate memory in the heap ShippingContainer **sc = new ShippingContainer *[N];

BasicNewbie1: i get it just an example : const int N = 6; ShippingContainer *sc[N]; i just having problem at here [pre2]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 = 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; }[/pre2] because i not really know how to cout the getManifest() out and maximum of container RFID_array and ManualShipping_array when total up must less than or equal to 6

Сыроежка: One more either you declare an array of type ShippingContainer * of fixed size as for example ShippingContainer * sc[N];, or you allocate a memory in heap as ShippingContainer ** sc = new ShippingContainer *[N]; In the both cases method getManifest is caleed as sc[ i ]->getManifest(); where i - any acceptable index. There is already an example in the previous thread how to fill the array. Investigate it.

BasicNewbie1: i try to . but u said i - any acceptable index [pre2]string theContent; int RFID_array = 0; int ManualShipping_array = 0; int totalContainerArray = 0; int counter = 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 ! Maximum 6 numbers of container ! " << 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[counter++] = 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 ); sc[counter++] = Manual_item; } for ( int i = 0; i < counter; i++ ) { cout << sc[ i ]->getManifest() << endl; }[/pre2] because the output is keep repeating the output when i can getManifest it will be: 1 crate of apples , 2 crate of pears 1 crate of apples , 2 crate of pears 1 crate of apples , 2 crate of pears 5 crate of pears. something like this. it's correct?

Сыроежка: You shall ot allocate a whole array of ManualShippingContainer or RFIDShippingContainer containers. You already defined an array. Fill it. What is the problem? Write for example in a loop sc[ i ] = new ManualShippingContainer(); I do not understand why are you asking the questions when there is an example how to do that in the previous thread?

BasicNewbie1: i already tired of it , i keep try and keep try already . i know u already provided the example. u mean i have to set 1 by 1? using loop? i have to using sc = new RFIDShippingContainer(); or sc = new RFIDShippingContainer(); where i is any index isn't like this? then how i see their different behaviour for the ShippingContainer ?

Сыроежка: I do not understand what you mean. There is an example how to do that. use the example. You should allocate 6 (as you pointed out) objects of types RFIDShippingContainer and ManualShippingContainer. Each element of the array should be assigned a new object.

BasicNewbie1: i get it after i wake up isnt u mean this? [pre2]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;[/pre2]

Сыроежка: I would do the following way [pre2]ShippingContainer *sc[TOTAL_CONTAINERS]; const char * manifets[] = { "crate of pears", "crate of apples", "crate of mangoes", "crate of bananas" }; const MANIFEST_NUM = sizeof( manifests ) / sizeof( *manifests ); const int MAX_MANIFEST = 10; // Let assume that among TOTAL_CONTAINERS there are RFID_TOTAL RFIDShippingContainer containers int i = 0; for ( ; i < RFID_TOTAL; i++ ) { sc[ i ] = new RFIDShippingContainer(); int total_manifests = std::rand() % MAX_MANIFEST; for ( int j = 0; j < total_manifests; j++ ) { sc[ i ]->add( manifests[ std::rand() % MANIFEST_NUM ] ); } } // something similar should be done for ManualShippingContainer for ( ; i < TOTAL_CONTAINERS; i++ ) { sc[ i ] = ManualShippingContainer(); // other stuff }[/pre2] I did not test the code. It can contain errors. But purpose of the code to demonstrate the idea.



полная версия страницы