0

I created an array with the maximum dimension for text input. Now, I want to remove the remaining unnecessary part in order to free the memory. I am not allowed to use std::string, I need to use char[] arrays only.

Also, if you know better way how to use only char and allocate dynamic memory for text input, please help me. I do not know how many characters the user will enter

#include <iostream>

using namespace std;

int main()
{
    char *text = new char[256];
    cout << "Input text: ";
    cin.getline(text,256,'\n');
    while (cin.fail())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max, '\n');
        cout << "Input text: ";
        cin.getline(text,256,'\n'); //need to delete part after '\n';
    }
    return 0;
}
7
  • Why did you change the tag to C? Commented Nov 27, 2017 at 12:58
  • You can't do that – you need to copy to another, smaller, array. It's not very clear what you think you need to delete or why, though. Commented Nov 27, 2017 at 12:59
  • because i need to use only char. String type not allowed. Commented Nov 27, 2017 at 12:59
  • Use a temporary dynamic array. Commented Nov 27, 2017 at 12:59
  • 1
    @DenAndreychuk It is correct to tag this as C++, since that's what it is. However, you should have posted the artificial homework requirements as part of the question. Hang on, I'll edit it. Commented Nov 27, 2017 at 12:59

2 Answers 2

5

Just use std::string instead of a C array of char. It will be sized automatically according to the input length.

Use the free function std::getline() instead of istream::getline():

string line;
getline(cin, line);

http://en.cppreference.com/w/cpp/string/basic_string/getline

Sign up to request clarification or add additional context in comments.

3 Comments

Ye, I know. But in my homework i must use only char. String type not allowed.
If you write a question and tag it C++ you have to expect people with use C++ strings to answer it. You never said that wasn't allowed. I'm going to leave this answer here, as it addresses the question as written.
Corrected. Sorry, this my first topic.
0

You can use a temporary dynamic array then copy the part you wanted from the original array then delete the original array then assign the original pointer to the temporary pointer:

char* szTxt = new char[256];
std::cin.getline(szTxt, 256, '\n');
std::cout << szTxt << std::endl;

const int size = strlen(szTxt);
char* szTmp = new char[size + 1];
strncpy(szTmp, szTxt, size);

szTmp[size] = '\0';

delete[] szTxt;
szTxt = szTmp;

std::cout << szTxt << std::endl;
std::cout << "Size: " << size << std::endl;

delete[] szTxt;

2 Comments

Your code will fail at szTmp[5] = '\0'. Increase the size by 1 at the new statement.
@iBug: Thanx! I didn't notice that. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.