I am getting weird outputs from my string class when using char const * string

Hello, everyone, I am taking my third program in computer science this time in C++. I have been coding in Python for the last two semesters and as we recently transitioned to C++ I am having hard times to grasp complexities. I do understand the basics thanks to my Python background, however, when it comes to implementing a class dynamic memory class I am lost. I never felt so discouraged.
We are given an assignment, which is to implement a string class similar to the Pythonian one. I have borrowed codes from her dynamic array classhe compiler will give some very weird output when I passing (char const * string) in the constructor an error otherwise. Here I have my class but I think the problems is only the constructor.

//string
#include "My_string.h"

//----------------------------------------------------------------------------------------------------------
//Here is the constructor
My_string::My_string(size_t capacity, char const* input, size_t size)
{
string_list_ = new char [capacity];//setting address to pointer string_pile.
size_ = size;
capacity_ = capacity;
}

//----------------------------------------------------------------------------------------------------------
//Copy constructor
My_string::My_string(const My_string &string)
{
copy(string);
}

//----------------------------------------------------------------------------------------------------------
//Deconstructor
My_string::~My_string()
{
delete [] string_list_;
}
//----------------------------------------------------------------------------------------------------------
//Copy construction helper
void My_string::copy(const My_string &string)
{
size_t iteration;
size_ = string.size_;
capacity_ = string.capacity_;
string_list_ = new char[string.capacity_];
for (iteration = 0; iteration < string.capacity_; iteration ++)
{
string_list_[iteration] = string.string_list_[iteration];
}
}
//----------------------------------------------------------------------------------------------------------
//[] operator
char& My_string::operator[](size_t position)
{
if (0 < position < size_ - 1)
{
return string_list_[position];
}
}
//----------------------------------------------------------------------------------------------------------
//Equal operator
My_string& My_string::operator=(const My_string &string)
{
if (&string != this)
{
delete [] string_list_;
copy(string);
}
return *this;
}
//----------------------------------------------------------------------------------------------------------
//Assignment operator
My_string& My_string::operator+=(const My_string &string)
{
size_t iteration;
size_t position = size_;

if((size_ + string.size_) > capacity_)
{
resize(size_ + string.size_);
}

for (iteration = 0; iteration < string.size_; iteration ++)
{
string_list_[position ++] = string.string_list_[iteration];
}
size_ += string.size_;
return *this;
}
//----------------------------------------------------------------------------------------------------------
//resize method
void My_string::resize(size_t new_size)
{
char *temp;
size_t iteration;

capacity_ = new_size;
temp = new char[capacity_];

for (iteration = 0; iteration < size_; iteration ++)
{
temp[iteration] = string_list_[iteration];
}
delete [] string_list_;
string_list_ = temp;
}
//----------------------------------------------------------------------------------------------------------
//find method
size_t My_string::find(char character)
{
size_t index;

for (index = 0; index < size_; index ++)
{
if(string_list_[index] == character)
{
return index;
}
}
}
//----------------------------------------------------------------------------------------------------------
//ouput operator
std::ostream& operator<<(std::ostream &os, const My_string &list)
{
size_t iteration;

for(iteration = 0; iteration < list.size_; iteration++)
{
os << list.string_list_[iteration] << "\t";
}
os << std::endl;
return os;
}
//----------------------------------------------------------------------------------------------------------
//input operator
std::istream& operator>>(std::istream &is, My_string &string)
{
char in[100];

std::cout << "enter the string, hit Enter key:";
std::cin >> in;

size_t iteration;
is >> string.size_;
string.resize(10 * string.size_);
std::cout << "Now enter strings one by one. Hit the Enter key after each input";
for (iteration = 0; iteration < string.size_; iteration ++)
{
is >> string.string_list_[iteration];
}

std::cout << "All the elements are aquareed.\n";
return is;
}
The first obvious problem in your constructor is that you don't copy the string. The memory you correctly allocate has only garbage in it. Also you should validate the input parameters
Ok, thanks a lot Thomas1965. However, if I am going to add to my something like *string_list = input; It is going to report me an error saying "[Error] invalid conversion from 'const char*' to 'char' [-fpermissive]" What does that mean?
*string_list refers to a single char so you can't assign a pointer to it. Anyway to copy the string you either use strcpy or write your own copy function.

EDIT - just saw that your string has a copy function so you can use it.
Last edited on
ok working on it. In the mean time this is what I was able to develop:
My_string::My_string(size_t capacity, char * input, size_t size)
{
size_t iterations;
string_list_ = new char[capacity];//setting address to pointer string_input.
string_list_ = input;
size_ = size;
capacity_ = capacity;
/*
for (iterations = 0; iterations < size; iterations ++)
{
if (string_list_[iterations] == "\0")
{
copy(string_list_);
}
}*/
}

My main function is :

int main()
{
My_string b(10, "Hello, Emmanuel!",6);
cout<<b;

return 0;
}
'When the input is just hello the program will print it but it also display a warning on the compiler - [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings].
With Hell, Emmanuel! is it going to print Hello, only. I think is because the empty string after the comma is equals to Null?
I actually got it the problem was in updating the size and capacity from the main function. God bless you sir for your help you have no idea how useful you have been to me today tanks again and I wish you a nice weekend.
Last edited on
Topic archived. No new replies allowed.