Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello guys. I hope you can help and give me some feedback to fix the size. My co

ID: 3828497 • Letter: H

Question

Hello guys. I hope you can help and give me some feedback to fix the size. My code is below. I dont know why some errors still get comments for me. [UPDATED]: I use the complier through Visual Studio 2010 program. Thus, I tried to run them but it still errors from below which is from VS 2010's comments. I dont know why it stills problem with size.

PLEASE HELP ME ASAP!

1>f:project11project11 ewmystring.h(33): error C2057: expected constant expression
1>f:project11project11 ewmystring.h(33): error C2466: cannot allocate an array of constant size 0
1>f:project11project11 ewmystring.h(33): error C2133: 'buffer' : unknown size
1>f:project11project11 ewmystring.h(115): error C2057: expected constant expression
1>f:project11project11 ewmystring.h(115): error C2466: cannot allocate an array of constant size 0
1>f:project11project11 ewmystring.h(115): error C2133: 'chars' : unknown size
1>f:project11project11exercises.cpp(308): warning C4018: '<' : signed/unsigned mismatch
1>

#ifndef MyNewString
#define MyNewString
#include
#include

class String
{
public:
   char *buffer;
   int len;


   String(const char ch, int size)
   {
       len = size;
       buffer [0] = ch;
       buffer[1] = '';
   }
   String(const char chars[], int size)
   {
       len = size;
       buffer = new char[len];
       for (int i = 0; i < len; i++)
       {
           buffer[i] = chars[i];
       }
       buffer[len] = '';
   }
   String append(const String &s)
   {
       int newLen = len + s.len;
       int j =0;
       char buffer[newLen]; //something is wrong!!!!!!!!!!!!!!!!!!!!!
       for (int i = len; i < newLen; i++)
       {
           buffer[i] = s.buffer[j];
           j++;
       }
       buffer[newLen] = '';
       len = newLen;
       return *this;
   }

   String append(const String &s, int index, int n)
   {
       int j = 0;
       for (int i = index; i        {
           buffer[i] = s.buffer[j];
           j++;
           len++;
       }
       buffer[len] = '';
       return *this;
   }

   String append(int n, char ch)
   {
       for (int i = 0; i < n; i++)
       {
           buffer [len++] = ch;
       }
       return *this;
   }

   String assign(const char *chars)
   {
       len = strlen(chars);
       buffer = new char[len];
       for (int i = 0; i < len; i++)
       {
           buffer[i] = chars[i];
       }
       return *this;
   }

   String assign(const String &s, int index, int n)
   {
       len = 0;
       buffer = new char[n];
       for ( int i = index; i < index + n; i++)
       {
           buffer [len++]= s.buffer[i];
       }
       buffer[len]= '';
       return *this;
   }

   String assign(const String &s, int n)
   {
       len = 0;
       buffer = new char[n];
       for ( int i = 0; i < n; i++)
       {
           buffer[len++] = s.buffer[i];
       }
       buffer[len] = '';
       return *this;
   }

   String assign(int n, char ch)
   {
       len = 0;
       buffer = new char[n];
       for (int i = 0;i < n; i++)
       {
           buffer[len++] = ch;
       }
       buffer[len] = '';
       return *this;
   }

   String substr(int index, int n ) const
   {
       char chars[n]; //////something is wrong!!!!!!!!!!!!!!!!
       int length =0;
       for (int i = index; i        {
           chars[length++] = buffer[index];
       }
       String str(chars, length);
       return str;
   }

   String substr(int index) const
   {
       return substr(index, len-index);
   }
   String erase(int index, int n)
   {
       if (index + n <= len)
       {
           buffer[index] = '';
       }
       else
       {
           int counter = 0;
           for (int i = index; i            {
               buffer[index] = buffer [n+counter];
               counter++;
           }
       }
       return *this;
   }
};
#endif

Explanation / Answer

Here are some suggested modifications along with comments for you:

#ifndef MyNewString
#define MyNewString
#include <iostream>
#include <cstring>
class String
{
public:
char *buffer;
int len;

String(const char ch, int size)   //Takes a character ch, and an int size as input.
{
len = size;       //Assigns size to len.
buffer = new char[size];   //This will actually allocate the memory. Note buffer is pointer and memory is not allocated to store string.
buffer [0] = ch;   //Now copies the passed character ch to first location in buffer.
buffer[1] = '';   //And terminates the string.
}
String(const char chars[], int size)   //Takes a character array chars, and an int size as input.
{
len = size;   //Assigns size to len.
buffer = new char[len];   //This will actually allocate the memory. Note buffer is pointer and memory is not allocated to store string.
for (int i = 0; i < len; i++) //Now copies the passed string chars, to buffer.  
{
buffer[i] = chars[i];
}
buffer[len] = '';   //terminates the string.
}
String append(const String &s)   //Takes a String as input.
{
int newLen = len + s.len;   //Increases the len to the sum of the current length + newly passed string length.
int j =0;                   //Assigns 0 to j.
//char buffer[newLen]; //something is wrong!!!!!!!!!!!!!!!!!!!!!
        //Its a static memory allocation. This should not be the case.
char *newBuffer = new char[newLen];   //Assigns a new buffer of newly required length.
int i;
for(i = 0; i < len; i++)   //Copies the old buffer value to new buffer.
    newBuffer[i] = buffer[i];          
for(; i < newLen; i++)   //Still proceeding further, appending the new String.
    newBuffer[i] = s.buffer[j++];
newBuffer[i] = '';   //Terminates the string.
buffer = newBuffer;   //Copies the newly created pointer address to buffer.      
return *this;
}
String append(const String &s, int index, int n)  
{
int j = 0;
for (int i = index; i < n; i++) {
buffer[i] = s.buffer[j];
j++;
len++;
}
buffer[len] = '';
return *this;
}
String append(int n, char ch)
{
for (int i = 0; i < n; i++)
{
buffer [len++] = ch;
}
return *this;
}
String assign(const char *chars)
{
len = strlen(chars);
buffer = new char[len];
for (int i = 0; i < len; i++)
{
buffer[i] = chars[i];
}
return *this;
}
String assign(const String &s, int index, int n)
{
len = 0;
buffer = new char[n];
for ( int i = index; i < index + n; i++)
{
buffer [len++]= s.buffer[i];
}
buffer[len]= '';
return *this;
}
String assign(const String &s, int n)
{
len = 0;
buffer = new char[n];
for ( int i = 0; i < n; i++)
{
buffer[len++] = s.buffer[i];
}
buffer[len] = '';
return *this;
}
String assign(int n, char ch)
{
len = 0;
buffer = new char[n];
for (int i = 0;i < n; i++)
{
buffer[len++] = ch;
}
buffer[len] = '';
return *this;
}
String substr(int index, int n ) const
{
//char chars[n]; //////something is wrong!!!!!!!!!!!!!!!!
//Again no static allocation
char *chars = new char[n];
int length =0;
for (int i = index; i < n; i++) {
chars[length++] = buffer[index];
}
String str(chars, length);
return str;
}
String substr(int index) const
{
return substr(index, len-index);
}
String erase(int index, int n)
{
if (index + n <= len)
{
buffer[index] = '';
}
else
{
int counter = 0;
for (int i = index; i < n; i++) {
buffer[index] = buffer [n+counter];
counter++;
}
}
return *this;
}
};
#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote