Help with writing/printing to and from an array in private data member

elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "elements.h"
using namespace std;
int main( )
{
	double v=0;
	double j=0;
	PlainBox bo;
	for(int i=0;i<10;i++){
		cout<<"Enter a variable: ";
		cin>>v;
		bo.setItem(v);
		cout<<endl;
	}
	for(j=0;j<10;j++){
		cout<<bo.getItem()<<endl;

	}
}


elements.h
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
// Set the type of data stored in the box
typedef double ItemType;

// Declaration for the class PlainBox
class PlainBox
{
  private:
// Data field
  ItemType vStore[10];
  int capacity;
  int num_used;
  int num;
  public:
	  
// Default constructor
  PlainBox ();

// Parameterized constructor
  PlainBox (const ItemType  theItem);

// Method to change the value of the data fi eld
  void setItem (const ItemType theIte);

// Method to get the value of the data fi eld
  ItemType getItem () const;

};  // end PlainBox
#endif 


lab1.cpp
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
/** @file PlainBox.cpp */
#include "elements.h"

PlainBox::PlainBox ()
{
	capacity=10;
	  num_used=0;
	  num=0;
}   // end default constructor

PlainBox::PlainBox (const ItemType theItem)
{
  
}   // end constructor

void
PlainBox::setItem (const ItemType theItem)
{
  vStore[num] = theItem;
}   // end setItem
ItemType
PlainBox::getItem () const
{
  return vStore[num];
}   // end getItem 


The console always prints the last number i type into the array 10 times as opposed to all the other numbers i put in.
For the actual assignment see:
http://www.cplusplus.com/forum/general/197376/

Where do you ever increase or decrease the index of your array?

A couple of problems:

lab1.cpp
--------
line 8: You initialize both num and num_used to 0. Not clear why you have two variables. You never change either of these variables.

Line 19: You always store the 0'th variable since you never incremented num.

Line 24: You always retrieve the 0'th variable since you never incremented num.

Suggestion: Get rid of num as a member variable. Pass num as an argument to get_item().
In setItem(), increment num_used.

edit: observations based on link in jlb's post:
1) Instructions do NOT say to create num as a member variable.
2) No where do you check if the array is full. The instructions say to provide a bool function to do this.
3) No function to determine if the array is empty.
4) No function to return the array capacity.
5) A getter with one argument. Your getter has no arguments.
6) A setter to overwrite a specific position. Not present.
7) A setter to add an element at the back. No correctly implemented.
8) A function to return the position of an element. Not present.
9) The driver should implement a menu system. Not present.


Last edited on
Part of the assignment requirement is to have a variable to hold the number of elements used.

Your class should include the following data members:
• An array (of size 10 by default)
• A variable to hold the array capacity (capacity).
• A variable to keep track of the number of elements currently being used (num_used).


Edit: As stated above you don't need num as a member variable.
Last edited on
Topic archived. No new replies allowed.