I want to declare a char array with not initial size. Later I will prompt the user to enter in a sentence. This input will be stored in the char array using getline.
But how do I make the number or chars entered by the user to become the size of the char array?
1 2 3 4
char text[] = {}; // empty char array
cout << "Please enter a sentence: << flush; // prompts the user
cin.getline(text, ???); // Stores the input in the array.
Line 4 is where the problem is. I dont want to put a specific number for the array size. I want the array size to be however many chars the user input was. The getline function, however, requires a specific number for the size of the array.
So, do as string does internally: dynamically allocate your array, then manually read string character by character. As soon you reach the end of the array, reallocate it to have more space, continue reading until you reach end of string.
Alternatively read line into std::string and use it internal buffer (create a pointers to it).
1 2 3 4
std::string line;
std::getline(std::cin, line);
char* text = &line[0];
int size = line.size();
Invalid. Always will be either 3 or 7. Use strlen here.
There we go! Its working ALMOST perfectly now. Only problem is, I sometimes get a garbage character at the end of the string. It usually happened when I enter a short string. A full sentence seems to be working fine. Any idea whats going on here?
#include <iostream>
//#include <string>
#include <cstring>
usingnamespace std;
int main()
{
// NOTE 100 is not really worth using the heap for; would be better to use
// a stack variable for the buffer (it's pretty routine to use char buffers
// of size 1024 for Windows applications, though this might be an issue when
// working with embedded devices and the like)
//
// and 100 is a bit tichy!
//
//char * ptr = new char [100]; // allocates an array of size 100
constint buffer_size = 100;
char * ptr = newchar [buffer_size]; // allocates an array of size 100
//cout << "Enter a string: " << flush;
cout << "Enter a string: "; // no need for flush
//string str;
//getline(cin, str);
cin.getline(ptr, buffer_size); // use getline method which works with char buffers
//for(int i = 0; i<str.length(); i++) // puts each character of string into pointer array
//{
// ptr[i] = str[i];
//}
//char * pStart, * pEnd; // delcare two char pointers
int nChars = strlen(ptr); // number of character in string entered
//pStart = ptr; // points to start of string
char * pStart = ptr; // points to start of string
//pEnd = ptr + nChars-1; // points to end of string
char * pEnd = ptr + nChars-1; // points to end of string
while(pStart < pEnd) // reverses string
{
char temp = *pStart;
*pStart = *pEnd;
*pEnd = temp;
pStart++;
pEnd--;
}
// print out string char by char...
//for (int i=0; i<str.length(); i++) // loops up to the last element before null terminator
for (int i=0; i<nChars; i++) // loops up to the last element before null terminator
{
//cout << ptr[i] << flush; // prints out the reversed string
cout << ptr[i]; // prints out the reversed string (no need for flush)
}
cout << "\n"; // plus add endline
// print out string again!!
cout << ptr << "\n";
delete [] ptr;
ptr = 0;
cin.get();
}