My code is supposed to take in user input such as "1/24/1999 Coffee", which would be stored as an Appointment object in an UnsortedType dynamic array.
At runtime, one compiler gives a segmentation fault core dumped and when ran through another, I get 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x01092FEC). I've looked at other threads similar to mine, so I'm guessing my issue should be with accessing memory that doesn't belong to me. Another issue may be circular dependency as I was struggling with it earlier today, though now it compiles as compared to it failing previously.
Any help is appreciated.
In visual studio the exception is thrown, at the default constructor in unsorted.cpp(not sure if that will be helpful):
UnsortedType::UnsortedType()
{
//item_arr = new ItemType[10];
item_arr = new Appointment[10];
size = 10;
}
The full version of my code is below:
unsorted.h
#ifndef LAB3_H
#define LAB3_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
//#include "Appointment.h"
#include <vector>
using namespace std;
class Appointment; // forward declaration
class UnsortedType
{
protected:
int length = 0;
int size;
Appointment* item_arr;
public:
UnsortedType();
UnsortedType(int num);
UnsortedType(const UnsortedType& arg); // copy constructor
UnsortedType& operator = (const UnsortedType& arg); // assignment operator
~UnsortedType(); //destructor
int getLength() const { return length; };
Appointment* getitem_arr() const { return item_arr; };
};
#endif
unsorted.cpp
#include <string>
#include <cstdlib>
#include "unsorted.h"
#include "Appointment.h"
#include <iomanip>
#include <vector>
using namespace std;
UnsortedType::UnsortedType()
{
//item_arr = new ItemType[10];
item_arr = new Appointment[10];
size = 10;
}
UnsortedType::UnsortedType(int num)
{
//item_arr = new ItemType[num];
item_arr = new Appointment[num];
size = num;
}
UnsortedType::~UnsortedType()
{
delete[] item_arr;
}
UnsortedType::UnsortedType(const UnsortedType& arg)
{
length = arg.getLength();
//item_arr = new ItemType[length + 1];
item_arr = new Appointment[length + 1];
int i = 0;
for (i = 0; i < length; i++)
item_arr[i] = arg.item_arr[i];
}
UnsortedType& UnsortedType::operator = (const UnsortedType& arg)
{
length = arg.getLength();
//item_arr = new ItemType[length + 1];
item_arr = new Appointment[length + 1];
int i = 0;
for (i = 0; i < length; i++)
item_arr[i] = arg.item_arr[i];
return *this;
}
void UnsortedType::PutItem(string s)
{
Appointment* temp_arr;
bool x = isFull();
if (x == false)
{
item_arr[length + 1] = s;
length = length + 1;
}
else
{
temp_arr = new Appointment[size * 2];
for (int i = 0; i < size; i++)
{
temp_arr[i] = item_arr[i];
}
delete[] item_arr;
item_arr = temp_arr;
size = size * 2;
item_arr[length + 1] = s;
length = length + 1;
}
}
Appointment.h
#ifndef LAB3_H2
#define LAB3_H2
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include "unsorted.h"
#include <vector>
using namespace std;
//typedef Appointment ItemType;
class Appointment : public UnsortedType
{
int year;
int month;
int day;
string desc;
string checker;
public:
Appointment();
Appointment(string s);
friend istream& operator >> (istream& ins, Appointment& arg);
friend ostream& operator << (ostream& out, const Appointment& arg);
int getYear() const { return year; };
int getMonth() const { return month; };
int getDay() const { return day; };
string getDesc() const { return desc; };
string getChecker() const { return checker; };
};
#endif
Appointment.cpp
#include <string>
#include <cstdlib>
#include "unsorted.h"
#include "Appointment.h"
#include <iomanip>
#include <vector>
using namespace std;
Appointment::Appointment()
{
year = 0;
day = 0;
month = 0;
desc = "";
}
Appointment::Appointment(string s)
{
string x, y;
int index = s.find(" ");
x = s.substr(0, index);
y = s.substr(index, s.length());
desc = y;
if (x.length() == 9)
{
day = stoi(x.substr(2, 2).c_str());
month = stoi(x.substr(0, 1).c_str());
year = stoi(x.substr(5, 4).c_str());
}
else
{
day = stoi(x.substr(3, 2).c_str());
month = stoi(x.substr(0, 2).c_str());
year = stoi(x.substr(6, 4).c_str());
}
}
istream& operator >> (istream& ins, Appointment& arg)
{
//Appointment temp;
string s, x, y;
label1:
getline(cin, s);
//ins >> s;
if (arg.getChecker() == s)
{
cout << "Error! Appointment has already been entered.";
cout << "Add the date and description of your appointment:";
goto label1;
}
//arg.item_arr[arg.getLength() + 1] = s;
arg.item_arr[arg.getLength()] = s;
arg.length = arg.getLength() + 1;
arg.checker = s;
cin.clear();
return ins;
}
ostream& operator << (ostream& out, const Appointment& arg)
{
out << arg.getDay() << "/" << arg.getMonth() << "/" << arg.getYear() << " " << arg.getDesc();
return out;
}
main.cpp
#include <string>
#include <cstdlib>
#include "unsorted.h"
#include "Appointment.h"
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
string command;
Appointment a, b, c, x, temp;
vector<Appointment>ItemArray;
do
{
cout << "Choose a command (Display, Add, Search, Delete, Quit): ";
cin >> command;
if (command == "Display" || command == "display")
{
cout << "These are all the slated appoinments:";
for (int i = 0; i < a.getLength(); i++)
cout << a.getitem_arr()[i];
}
else if (command == "Add" || command == "add")
{
cout << "Add the date and description of your appointment:";
//cin >> a;
//ItemArray.push_back(a);
while (cin >> a)
ItemArray.push_back(a);
}
else if (command == "Quit" || command == "quit")
exit(0);
else
cout << "Invalid operation please try again" << endl;
} while (command != "Quit" || command != "quit");
return 0;
}
User contributions licensed under CC BY-SA 3.0