Do the following problem it is different than on the slides

Do the following problem (it is different than on the slides)

You will be making an inventory program.

Let the user decide a number between 1-10 items.

allocate (using either new, Malloc, or calloc) an array of item structures-- only allocate the number you need.

an item structure is an array of 15 characters for the name, and an int for the weight.

then, have a loop that has the user enter the information for each item.

next, have a new loop that displays all the items

free/delete the array

#include <iostream>

#include <iomanip>

using namespace std;

#include <stdlib.h>

#define MAX_REC 10

class Inventory1{

char Name[15];

double weight;

public:

void getdataitem();

void displaydata();

  

};

void Inventory1 :: getdataitem(){

  

cout<<\"\ Enter Inventory Name : \";

cin>>Name;

cout<<\"Enter weight : \";

cin>>weight;

  

}

void Inventory1 :: displaydata(){

  

cout<<endl;

  

cout.width(15);

  

cout<<Name;

  

cout.width(15);

  

cout.precision(2);

  

cout<<weight;

  

}

int main(){

Inventory1 record1[MAX_REC];

int i,n;

   cout<<\"\ How many Items : \";

cin>>n;

cout<<\"Enter \"<<n<<\" Records\ \";

for(i=0;i<n;i++)

record1[i].getdataitem();

cout<<\"\ \"<<setw(8)<<\"Name\"

<<setw(19)<<\"Cost\"<<endl;

cout<<\"------------------------\";

for(i=0;i<n;i++)

  

record1[i].displaydata();

  

  

  

}

use new or malloc in this code

Solution

What you have actually done is that you have created an array of Inventory1 object without using new or malloc. So you have to use constant MAX_REC because it will allocate the memory in the stack(where local variables reside) and stack size is usually not that high. So you might get stackoverflow error if you allocate large size object i.e.around 1 milllion array object . Apart from that you don\'t need to worry about memory leak because it will be cleared automatically after the program execution or execution of function where it was defined. But how to do that when User decide how many Inventory1 object he/she wants. You either need to use malloc or calloc or new operator. These will allocate the memory in the heap section of memory segment and it will not be free until you do free(objectname) or delete object. So basically you should always use new and delete.

Now how to do that

Inventory1 *record1 = new Inventory1[n]; // this will allocate n Inventory1 object in the memory.

And you can use it as normal(created without using new) object which you have used before. But after the whole operation you need to explicitly delete it so

delete []record1; // deleted n object of Inventory1

Without explicit deletion memory leak occurs and you might get exception in the program.

So your new main function is -

int main(){
int i,n;
cout<<\"\ How many Items : \";
cin>>n;
Inventory1 *record1 = new Inventory1[n]; // create the object
cout<<\"Enter \"<<n<<\" Records\ \";
for(i=0;i<n;i++)
record1[i].getdataitem();
cout<<\"\ \"<<setw(8)<<\"Name\"
<<setw(19)<<\"Cost\"<<endl;
cout<<\"------------------------\";
for(i=0;i<n;i++)
record1[i].displaydata();

delete []record1; // delete the object
}

Do the following problem (it is different than on the slides) You will be making an inventory program. Let the user decide a number between 1-10 items. allocate
Do the following problem (it is different than on the slides) You will be making an inventory program. Let the user decide a number between 1-10 items. allocate
Do the following problem (it is different than on the slides) You will be making an inventory program. Let the user decide a number between 1-10 items. allocate

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site