After I try to run the code, it could display the array anymore whenI do the cas
ID: 3889114 • Letter: A
Question
After I try to run the code, it could display the array anymore whenI do the case 9/ case 10. Coluld you chack what is my error?
//header File: PlainBox.h
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
#ifndef PLAIN_BOX
#define PLAIN_BOX
typedef double ItemType;
const int size = 10;
class PlainBox
{
private:
ItemType item[size];//
const int capacity = 10;//The maximum number of content
int num_used;//number of space currently used
public:
PlainBox();// Default constructor
bool IsFull();//Check is it full
bool IsEmpty();// Check is it empty
int arrayCapacity();//cout how capacity have
int numUsed();//cout how many space are used
void setItem(const ItemType& theItem, int a);// Method to set the value to the data field
int getItem(int a) const;// Method to get the value of the data field
void add(const ItemType& theItem);//Method to add the value ot the data field
int positionElement(const ItemType& theItem);// check where are the location of the element
void insert(int b, int n);//Method to insert an element at a given position
void deleteI(int b);//Method to delete an element at a given position
void display();
}; // end PlainBox
#endif
//Implementation File
#include"PlainBox.h"
PlainBox::PlainBox(){
for (int i = 0; i < 10; i++){
item[i] = 0;
}
num_used = 0;
} // end default constructor
bool PlainBox::IsFull(){
if (num_used == capacity)
return true;
else
return false;
}// end IsFull
bool PlainBox::IsEmpty(){
if (num_used == 0)
return true;
else
return false;
}// end IsEmpty
int PlainBox::arrayCapacity(){
return capacity;
}//endarrayCapacity
int PlainBox::numUsed(){
return num_used;
}//endnumUsed
void PlainBox::setItem(const ItemType& theItem, int a){
if (a >= 0 && a < capacity){
item[a] = theItem;
num_used++;
}
else
cout << "Error. Element entered was out of space." << endl;
} // end setItem
int PlainBox::getItem(int a) const{
return item[a];
} // end getItem
void PlainBox::add(const ItemType& theItem){
//bool Extraloca = (num_used < capacity);
int sit = capacity - num_used;
if (sit>0){
item[num_used] = theItem;
cout << "Element added successfully! "; //inf -- return sit;
num_used++;
}
display();
}//end add
int PlainBox::positionElement(const ItemType& theItem){
for (int i = 0; i < capacity; i++)
{
if (item[i] == theItem)
return i;
}
return -1;
}//end positionElement
void PlainBox::insert(int b, int N){//Method to insert an element at a given position
int space = capacity - num_used;
if (space >(num_used - 1)){
for (int i = capacity; i >= b; i--){
item[i] = item[i - 1];
}
}
item[b] = N;
num_used++;
cout << "Element insert successfully! ";
//display();
cout << endl;
}//end insert
void PlainBox::deleteI(int b){//Method to delete an element at a given position
item[b] = 0;
int capacity1 = 10;
if (num_used < capacity1)
for (int i = b; i < capacity1; i++)
item[i] = item[i + 1];
num_used--;
cout << "Element delete successfully! ";
display();
cout << endl;
}//end deleteI
void PlainBox::display()
{
for (int i = 0; i < capacity; i++){
cout << item[i] << " ";
}
cout << endl;
}//end display
//Main File
#include"PlainBox.h"
using namespace std;
int main()
{
PlainBox BoxA;
int c, n, s, p, ck, ins, num, de, element;
do //keep doing this menus until user type 11 to exit
{
cout << " 1. Check is it full";
cout << " 2. Check is it empty";
cout << " 3. Check it capacity";
cout << " 4. Check how many are used";
cout << " 5. Set Item";
cout << " 6. Get Item";
cout << " 7. Add Item";
cout << " 8. Position of element";
cout << " 9. Insert";
cout << " 10. Delete";
cout << " 11. Display";
cout << " 12. Exit" << endl;
cout << "Please enter your choice: ";
cin >> c;
switch (c)
{
case 1:
if (BoxA.IsFull() == true)
cout << "This box is full." << endl;
else
cout << "This box is not full." << endl;
break;
case 2:
if (BoxA.IsEmpty() == true)
cout << "This box is empty." << endl;
else
cout << "This box is not empty." << endl;
break;
case 3:
cout << BoxA.arrayCapacity() << endl;
break;
case 4:
cout << BoxA.numUsed() << endl;
cout << endl;
break;
case 5:
cout << " Enter an element: ";
cin >> n;
cout << " Put the element to which space(0-9): ";
cin >> s;
BoxA.setItem(n, s);
cout << endl;
break;
case 6:
cout << " Enter an any position you want(0-9): ";
cin >> p;
cout << BoxA.getItem(p) << endl;
break;
case 7:
cout << " Enter an element to add: ";
cin >> element;
BoxA.add(element);
cout << endl;
break;
case 8:
cout << " Enter an element in list you want to check: ";
cin >> ck;
if (BoxA.positionElement(ck) == -1)
cout << "Element could not find." << endl;
else
cout << BoxA.positionElement(ck) << endl;
cout << endl;
break;
case 9:
cout << "Enter a location where you want to insert:";
cin >> ins;
cout << "Enter a element you want to insert:";
cin >> num;
BoxA.insert(ins, num);
break;
case 10:///Delete
cout << "Enter a location where you want to delete:";
cin >> de;
BoxA.deleteI(de);
break;
case 11:
BoxA.display();
break;
case 12:
cout << " Goodbye. ";
break;
default:
cout << " Invalid choice:" << endl;
}
} while (c != 12);
return 0;
}//end Main
Explanation / Answer
the problem of that code was it return garbage value ( address of the value).
chaged the constant value to static constant and chage the case 9 and case 10 in condition so that the value entered should be proper way.
static const int capacity = 10;//The maximum number of content sould be define static integer
void PlainBox::insert(int b, int N){//Method to insert an element at a given position
int space = capacity - num_used;
if (space >(num_used - 1)){
for (int i = capacity; i >= b; i--){
item[i] = item[i - 1];
}
}
item[b] = N;
num_used++;
cout << "Element insert successfully! ";
//display();
cout << endl;
}//end insert
void PlainBox::deleteI(int b){//Method to delete an element at a given position
item[b] = 0;
int capacity1 = 10;
if (num_used < capacity1)
for (int i = b; i < capacity1; i++)
{
if(item[i]==0 && item[b]==0)
{
cout<<"Element not exist in this location ";
break;
}
else
{
item[i] = 0;
//num_used--;
cout << "Element delete successfully! ";
}
}
case 9:
cout << "Enter a location where you want to insert:";
cin >> ins;
if(ins>9)
cout<<"Entered location is wrong only 0-9 is valid location ";
else{
cout << "Enter a element you want to insert:";
cin >> num;
BoxA.insert(ins, num);
}
break;
case 10:///Delete
cout << "Enter a location where you want to delete:";
cin >> de;
if(de>9)
{
cout<<"Deletion location is wrong only 0-9 is valid location ";
}
else{
BoxA.deleteI(de);
}
break;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.