dynamic arrays are known for letting you store a string or any data type without having to declare it size the problem that i'm facing with my c++ is the following :
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char*sentence=new char;
cout<<"enter a sentence (end with *) : ";
cin.getline(sentence,'*');
cout<<"sentence : "<<sentence<<endl;
system("pause");
return 0;
}
the cin.getline won't stop on the character '*' so the limit will be set when i press enter. but if i want to use only the return key it will read the first 9 characters of the string:
int main()
{
char*sentence=new char;
cout<<"enter a sentence : ";
cin.getline(sentence,'\n');
cout<<"sentence : "<<sentence<<endl;
system("pause");
return 0;
}
but it will work only if i limit the number of characters :
int main()
{
char*sentence=new char;
cout<<"enter a sentence : ";
cin.getline(sentence,100,'*');
system("pause");
return 0;
}
but i want to let the user input a sentence without limits how to do it without setting the number of characters in the cin.getline nor when declaring the dynamic array .