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;
}
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.
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.