C++ : string object to character array

Objectives : string is a better choice of input, compared to character array.

  • new and delete operators: Managing string input with new[] and delete[] operators.
  • Creating c-string from array: Using the following syntaxes we can store the input sequence entered by the users into c-string.
    char cstr1[20]; // character array
    cin.getline(cstr1, sizecstr1+1); // gets a line from key board
    cin.getline(cstr1, sizecstr1+1, '\n');
    cin.getline(cstr1, sizecstr1+1, 'x');

    But, the above syntaxes can't process over sized data entered by the users.  We could have used another function like gets(cstr1), to fit oversized data, ignoring the size defined during declaring the array.  A string object fits better in the above scenario, and we don't have to ignore any rule.

  • File Name : char_new_delete1.cpp
  • The memory allocated from  heap by new operator must be de-allocated (freed)  with delete operator.
  • The function getline(cin,str1), with string object as a parameter, is a better choice of receiving users' input from key board, than saving user's input from keyboard as c-string using the function getline(cstr1,sizeofc-string+1).
  • Converting a string to character array.
  • Simple solution of quitting on an oversized data.
  • In this example, we also added a statement, ensuring  that if input data exceeded  the desired length, the application would terminate before allocating the memory.
  • Program Flow:
  • Code: char_new_delete1.txt

Step: 1 Create a source file.

Step: 2 Edit and save Source file

Step: 3 Runtime Views:

A) Checking oversize data input.

B) With correct data size.


Step: Brief discussion:

In this example we used a string object to get user's input from the keyboard, then serialized to initialized a character array. Users can add a string of any size, but the application will terminate if the length of the string contains more than 15 characters.

The c-string is an array of characters and can't be assigned to one another, these must be copied to one another. When we store a c-string into an array, a null character is added at the end of string. In contrast to c-string and arrays, a string object of C++ language can be assigned to each other.  By nature, the string objects are class type variables, and are designed to manipulate chunks of characters.

    c-string / Array                         C++ string object
    ---------------------------             ---------------------------
     char ch1[10];char ch2[10]               string str1; string str2; 
     strcpy(ch1, "Hello!");                  str1 = "Hello";
     strcpy(ch2, ch1);                       str2 = str1;
     ch2=ch1;//not allowed 	
     cin.getline(cstr1, sizecstr1+1);        getline(cin, strobj); 

The contents of the string objects are allocated with smaller segments, thus preventing defragmentation of the heap structure.

All the above facts, are in favor of using string object to receive users input from the keyboard, than using a character array.