-2
  • The array should be dynamic, but it is fixed.
  • Printing the array gives nothing.

I'm trying to create a dynamic char array, get a string line from user, save line string char by char into the array.

#include <iostream>
#include <string>
using namespace std;
int main()
{
char* lenptr = NULL;        // declare a pointer initialized with null
int leng, arrsz;
string mystr;
cout << "enter a string: ";
getline(cin, mystr);        // input a line of string
leng = mystr.length();      // get length of string
cout << "string length: " << leng << endl;
lenptr = new char[leng+1];      // declare a dynamic char array with length+1
for (int i = 0; i < leng; i++)
{
    lenptr[i]=mystr[i];     // fill array with saved string
}
lenptr[leng] = '\0';        // save '\0' in last array cell
arrsz = sizeof(lenptr);     // get size of array
cout << "size of array after saving " << leng << " chars: " << arrsz << endl;
cout << "string in array: ";
for (int j = 0; j < leng; j++)      // print array
{
    lenptr[j];
}
cout << endl;
// delete pointer
delete[] lenptr;
lenptr = NULL;  
system("pause");        // pause system
return 0;
}

enter a string: helloworld string length: 10 size of array after saving 10 chars: 8 string in array: Press any key to continue . . .

GKE
  • 960
  • 8
  • 21
  • 2
    "The array should be dynamic, but is fixed." Try read [this](https://stackoverflow.com/questions/2672085/static-array-vs-dynamic-array-in-c). – GKE Jan 26 '19 at 03:56
  • 4
    Why are you not simply using `std::string`? Is this what is being taught in a C++ course? You even included ``, which is the header file that declares `std::string`. – PaulMcKenzie Jan 26 '19 at 03:57
  • `arrsz = sizeof(lenptr);` -- This does not get you the number of characters. As a matter of fact, the number of characters is totally lost when you do things this way with pointers. That's another reason why `std::string` exists -- all of that information (such as length) is encapsulated within the class. – PaulMcKenzie Jan 26 '19 at 04:04
  • First, std:: is wasting time, rather using namespace std; is quite useful,I believe so! Second, thanks for GKE, your little help fixed my code issue! Third, THANK YOU guys for NEGATIVE MARK, it's very HELPFUL! Maybe someday I'll give it back to you, also these little negative marks make me think to leave this website for good and looking for a help at useful places rather! – e.b_al-issa Jan 26 '19 at 04:44

1 Answers1

0

You can't get the size of an array allocated with new[]. Although std::string is the recommended way to go about this, a vector would've been a more viable choice if the string absolutely had to be stored in a data structure.

At any rate, your program wasn't printing the characters of the array because you forgot a std::cout in the for loop:

for (int j = 0; j < leng; j++)      // print array
{
    cout<<lenptr[j];
}

and to actually print out the size of the array just use the leng variable you defined earlier:

cout << "size of array after saving " << leng << " chars: " << leng + 1 << endl;
GKE
  • 960
  • 8
  • 21