>Is it possible for a user to input a string without allocating memory space for the string?
It depends on how pedantic you are. If you mean without the programmer explicitly allocating memory then it is possible:
Code:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout<<"Name: ";
if ( std::getline ( std::cin, name ) )
std::cout<< name <<std::endl;
}
Or you could use an array and work with C-style strings:
Code:
#include <iostream>
#include <cstdio>
int main()
{
char name[BUFSIZ];
std::cout<<"Name: ";
if ( std::cin.getline ( name, sizeof name ) )
std::cout<< name <<std::endl;
}
In neither of these are you explicitly calling new to get anonymous memory. If you mean not allocate memory at all in any part of the program to hold the string, not without great difficulty. On the other hand, if you're willing to allow for a single character in memory:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char c;
cout<<"Name: ";
while ( cin.get(c) && c != '\n' )
cout<< c;
cout<<endl;
}