main.cpp _________________ #include<iostream> #include<string> #include\"BasketL
ID: 671246 • Letter: M
Question
main.cpp
_________________
#include<iostream>
#include<string>
#include"BasketList.h"
using namespace std;
int readEggsInBasket(string);
int main()
{
char choice;
int egg_in_a_basket;
BasketList BL_of_bunny;
cout << "This program demonstrates the collection of baskets for the Easter Bunny. " << endl;
cout << "Initially, you will be asked to create the basket list for the bunny." << endl;
cout << "You will be later prompted to manipulate the basket list." << endl << endl;
BL_of_bunny.makeBasketList();
cout << "The basket collection for the Easter Bunny are in order: " << endl;
BL_of_bunny.printBasketList();
do
{
cout << "*******************************************************" << endl;
cout << "i: Insert a new basket" << endl;
cout << "d: Delete (Delete a basket with the given number of eggs)" << endl;
cout << "p: Print (Display the number of eggs in each basket from the basket list)" << endl;
cout << "q: Quit (Quit the program)" << endl;
cout << "*******************************************************" << endl << endl;
cout << endl << "Please enter your choice here:";
cin >> choice;
cin.ignore(256, ' ');
switch (choice)
{
case 'i':
egg_in_a_basket = readEggsInBasket("to insert:");
BL_of_bunny.insertBasket(egg_in_a_basket);
break;
case 'd':
egg_in_a_basket = readEggsInBasket("to delete:");
BL_of_bunny.deleteBasket(egg_in_a_basket);
break;
case 'p':
cout << "The basket collection for the Easter Bunny are in order: " << endl;
BL_of_bunny.printBasketList();
break;
case 'q':
break;
default:
cout << " Invalid choice. Please try again. ";
break;
}
} while (choice != 'q');
cout << "Thank you for using this program" << endl;
return 0;
}
int readEggsInBasket(string descr)
{
int eggs;
cout << endl << "Please enter the number of eggs in the basket " << descr;
cin >> eggs;
cin.ignore(256, ' ');
while (cin.fail())
{
cin.clear();
cin.ignore(256, ' ');
cout << endl << "Please enter the number of eggs in the basket " << descr;
cin >> eggs;
cin.ignore(256, ' ');
}
return eggs;
}
BasketList.h
______________
/*
* File: BasketList.h
* Author: pike
*
* Created on September 26, 2015, 3:35 PM
*/
#ifndef BASKETLIST_H
#define BASKETLIST_H
#include <cstdlib>
#include <iostream>
using namespace std;
const int MAXEGGS = 500;
class BasketList; // needed for friend line below
class Basket
{
private:
int egg_num;
Basket* nextBasket;
public:
Basket(int, Basket*);
int getEggs() const;
Basket const* getNextBasket() const;
friend class BasketList; // gives BasketList access to egg_num and nextBasket
};
class BasketList
{
private:
Basket* head;
public:
BasketList();
void insertBasket(int);
void makeBasketList();
void deleteBasket(int);
void printBasketList();
};
#endif /* BASKETLIST_H */
BasketList.cpp
#include <cstdlib>
#include <iostream>
#include <ctime>
#include "BasketList.h"
using namespace std;
//---------------------------------------------------
//Basket Members
//---------------------------------------------------
Basket::Basket(int _datum, Basket * _next):
egg_num(_datum), nextBasket(_next)
{}
int Basket::getEggs() const
{
return egg_num;
}
Basket const* Basket::getNextBasket() const
{
return nextBasket;
}
//---------------------------------------------------
//BasketList Members
//---------------------------------------------------
BasketList::BasketList() :
head (NULL)
{}
void BasketList::insertBasket(int eggs)
{
Basket *currPtr = head;
Basket *prevPtr = NULL;
Basket *newBasketPtr; //points to a new basket
while(currPtr != NULL && eggs > currPtr->egg_num)
{
prevPtr = currPtr;
currPtr = currPtr->nextBasket;
}
newBasketPtr = new Basket(eggs, currPtr);
prevPtr->nextBasket = newBasketPtr;
}
void BasketList::makeBasketList()
{
const int INITIALBASKETS = 5;
int eggValue;
Basket *newBasketPtr;
srand(time(0));
eggValue = rand() % (MAXEGGS + 1);
newBasketPtr = new Basket(eggValue, NULL);
head = newBasketPtr;
for (int i=1; i < INITIALBASKETS; i++)
{
eggValue = rand() % (MAXEGGS + 1);
insertBasket(eggValue);
}
}
void BasketList::deleteBasket (int eggs)
{
Basket *delPtr = head;
Basket *prevPtr = NULL;
// If the list is empty, then do nothing;
if (delPtr == NULL)
return;
//Treat the first basket as a special case... head needs to be updated
if (head->egg_num == eggs)
{
head = head->nextBasket;
}
else
{
prevPtr = delPtr;
//Look for the basket to deleted
while (delPtr != NULL && delPtr->egg_num < eggs)
{
prevPtr = delPtr;
delPtr = delPtr->nextBasket;
}
prevPtr->nextBasket = delPtr->nextBasket;
}
//Delete the node
delete delPtr;
}
void BasketList::printBasketList ()
{
Basket *currPtr;
currPtr = head;
int count = 0;
if (currPtr == NULL)
{
cout << "Empty! Collect more..." << endl;
return;
}
while (currPtr != NULL)
{
count++;
cout << "Basket " << count << " contains " << currPtr->egg_num << " eggs." << endl;
currPtr = currPtr->nextBasket;
}
}
Find the errors in BasketList.cpp Functions deleteBasket and insertBasket. The errors are segment faults.
Explanation / Answer
Whenever we get the segmentation fault error we should understand that we are doing something wrong with the memory i.e we are accessing something (variable) which has been freed from the memory and we are just simply writing the read only memory portion of the memory.
Now in this code for the function insertBasket(int eggs)
We have the current pointer that is pointing to the head of the list which is perfectly fine,also the previous pointer pointing to null as there is no previous node and one more pointer to point to the new basket.
The constructor is being called [new Basket(eggs, currPtr);] but there is no definition for it.How the node will be created.We are passing the pointer to the constructor but there is no pointer variable to handle it i.e it is simply going to null.This is the error in the inserBasket() function.
Now for the code in the deletebasket function:-
Everything in the code is fine except the line [prevPtr->nextBasket = delPtr->nextBasket; ]
There will be ambiguity on which pointer for nextBasket will be updated.There will pe error in updating the next and previous pointer in this line.
Hence to resolve this error the pointer should be updated separately.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.