I figured this would be easy but ultimately I can't seem to get past the " " in a char array.

i.e.

const int ARRAY_SIZE = 80;
char charArray[ARRAY_SIZE];


cout << "input a sentence";
cin >> charArray;

"This is a test string" returns only "This" as the space becomes a null terminator in the array. I know I can use the string library but that isn't what I'm trying to accomplish.

Recommended Answers

All 10 Replies

You can use this instead..

cin.getline(charArray, 80);

You can use this instead..

cin.getline(charArray, 80);

So how is that stored? it stores the entire sentence then just fills the remaining empty spaces with?

A character array is terminated with a null('\0') character and the content after that doesn't matter. It will be some random data.

cout << "Please enter a sentence under 79 characters long: ";
	cin.getline(charArray, 80);

		for (int i = 0; i < 80; i++) {
			if (charArray[i] != '\0') {
				counter++;
			}
		}

		cout << counter;
	
}

Should then return the number of characters in the sentence entered then correct?

The ultimate purpose here is to create an empty 80 char array. The user then enters a sentence. This sentence is then put into a new dynamically allocated array just large enough to hold the sentence.

This will, with a litte modification. You should break the loop as soon as it finds a null. But remember it will also count spaces.

if (charArray[i] != '\0') {
     counter++;
} else
     break;

If you want to find the size of a char array and you dont want to use the strlen() function in the cstring header then you can use this.

//...
char sentence[80];
cout << "Enter a sentence to find the length: ";
cin.getline(sentence, 80);
int stringSize = 0;
while(sentence[stringSize] != '\0')
{
    stringSize++;
}
//...

You can make it shorter but that will give you the jist of it.

#include <iostream>

using namespace std;

char *getString();

int main()
{
	*getString();
}

char *getString()
{
	char charArray[80];
	char *newArray;
	int counter = 0;

	cout << "Please enter a sentence under 79 characters long: ";
	cin.getline(charArray, 80);

	for (int i = 0; i < 80; i++) {
		if (charArray[i] != '\0') {
			counter++;
		} else
		{
			break;
		}
	}
	newArray = new char[counter];

	for (int i = 0; i < (counter); i++)
	{
		newArray[i] = charArray[i];
	}
	cout << "Here is the sentence from within the function:\n";
	for (int i = 0; i < (counter); i++)
	{
		cout << *(newArray + i);
	}
	cout << "\n";
	return(newArray);
}

User enters a sentence, for loop counts the number of chars, create new array based on the counter, store old array into the new array and call by pointer.

I think I got this right?

Here were the reqs

/*5. getString Function 

Write a function named getString that has a local char array of 80 elements. 
The function should ask the user to enter a sentence, and store the sentence 
in the array. Then the function should dynamically allocate a char array 
just large enough to hold the sentence, plus the null terminator. It should 
copy the sentence to the dynamically allocated array, and then return a 
pointer to the array. Demonstrate the function in a complete program. 

Note: You need to write your own string copy function, not use the standard library version.
*/

For counting the number of characters, the better way is as NathanOliver suggested or

int length;
for(length=0;charArray[length]!='\0';length++);

And now about your code, it won't work.. because your are creating a new array at compile time when the value of counter is 0.

Write a function named getString that has a local char array of 80 elements. The function should ask the user to enter a sentence, and store the sentence in the array. Then the function should dynamically allocate a char array just large enough to hold the sentence, plus the null terminator.

You need to allocate memory using new.

/*5. getString Function 

Write a function named getString that has a local char array of 80 elements. 
The function should ask the user to enter a sentence, and store the sentence 
in the array. Then the function should dynamically allocate a char array 
just large enough to hold the sentence, plus the null terminator. It should 
copy the sentence to the dynamically allocated array, and then return a 
pointer to the array. Demonstrate the function in a complete program. 

Note: You need to write your own string copy function, not use the standard library version.
*/

#include <iostream>

using namespace std;

char *getString();

int main()
{
	char *mainArray = getString();
	int counter = 0;

	for (int i = 0; i < 80; i++) {
		if (mainArray[i] != '\0') {
			counter++;
		} else
		{
			break;
		}
	}
	cout << "Here is the sentence from within main:\n";
	for (int i = 0; i < counter; i++)
	{
		cout << *(mainArray + i);
	}
	cout << "\n";

}

char * getString()
{
	char charArray[80];
	char *newArray;
	int counter = 1;

	cout << "Please enter a sentence under 79 characters long: ";
	cin.getline(charArray, 80);

	for (int i = 0; i < 80; i++) {
		if (charArray[i] != '\0') {
			counter++;
		} else
		{
			break;
		}
	}
	newArray = new char[counter];

	for (int i = 0; i < counter; i++)
	{
		newArray[i] = charArray[i];
	}
	cout << "Here is the sentence from within the function:\n";
	for (int i = 0; i < counter; i++)
	{
		cout << *(newArray + i);
	}
	cout << "\n";
	return(newArray);
}

This compiles and seems to work; "allocate new" not sure I get your meaning?

Sorry, You already used new.. I overlooked your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.