Saturday, 17 August 2013

Input string without using C++ string

Input string without using C++ string

I was asked to take a string (which can be of any size ) from input
without using C++ string. I thought of dynamically allocating space for a
char array and got following implementation from SO itself. But I am not
sure whether it is a good implementation. Is there a better implementation
for this which doesn't require you to input the number of elements in the
name?
#include<iostream>
using namespace std;
int main()
{
int size = 0;
cout << "Enter the size of the dynamic array in bytes : ";
cin >> size;
char *ptr = new char[size];
for(int i = 0; i < size;i++)
cin >> *(ptr+i);
}

No comments:

Post a Comment