User Assigned Array/Function

Sep 21, 2010 at 3:39am
I am tweaking an assignment from school. I am not asking for someone to do my homework, just if what I want to do is possible. In the original assignment, the programmer has already entered a character array in the form of a sentence, pre-counted the characters, and assigned the number to size. A function or two take over, do a "sort/search/delete" couts back to the main the new sentence and the number of characters as newSize. Pretty nifty, but I'm not thrilled with the programmer using a predetermined sentence. I would like to incorporate a user's sentence in place of the pre-programmed sentence. This is what I have done to get the user's sentence:

1
2
3
4
5
char userSentence[100] = {'\0'};
    cout << "Enter a sentence of less than 99 characters and spaces." << endl;
    cin.getline(userSentence, 99, '\n');
    cout << "This is your sentence: " << endl;
    cout << userSentence << endl;


From here, I'd like a function (?) to pick up userSentence, count the characters and cout to the user/console how many characters their sentence had. That is more impressive. Is that possible? If so, could someone point me in the right direction?

To help you with my level of understanding, I am completing my first semester of C++ in one week. I guess that means I have a crude if not basic understanding of it.
Sep 21, 2010 at 3:42am
This would be very easy using a string.
1
2
3
4
5
6
7
string userSentence;
cout << "Enter a sentence." << endl;
getline(cout, userSentence);
getline(cin, userSentence);
cout << "This is your sentence: " << endl;
cout << userSentence << endl;
cout<<"Your sentence was "<<userSentence.size()<<" characters long."<<endl;


EDIT: Corrected typo.
Last edited on Sep 21, 2010 at 6:15am
Sep 21, 2010 at 3:56am
closed account (Lv0f92yv)
If you just want to get a sentence from the user and count how many chars the sentence contained, using the string class might be the easier way.

1
2
3
string userSentence;
getline( cin, userSentence ); //read a line from the cmdline including spaces
cout << userSentence.size() << "\n";


If you want to avoid using the string class,
1
2
3
4
5
6
int size = 90;
	char* sentence;
	sentence = new char[size]; //dynamically allocate memory for sentence on the heap
	cin.getline( sentence, size ); //read in a line of size 'size', delimitted by newline char by default
	cout << strlen( sentence );//use built in method strlen to calculate the number of chars
//in char array 


Slightly more low-level:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
	int size = 90;
	char* sentence;
	sentence = new char[size];
	cin.getline( sentence, size );

	int count = 0;
	for ( int i = 0; i < size; i++ )
	{
		//check for null terminator
		if ( sentence[i] != '\0' )
			count++;
	}
	cout << "size of string was " << count << "\n";

	return 0;
}


The final example is reliant on the fact that cin.getline() null-terminates the character array by placing a '\0' after the final character inserted into the array. This only works on null-terminated strings.

Edit: Spent about 20 min looking for youtube videos while making this post... Zhuge won.
Last edited on Sep 21, 2010 at 4:05am
Sep 21, 2010 at 4:06am
@ Zhuge and Desh - Either one of those would be better. I'll try your suggestions and leave some follow up. Thank You!
Last edited on Sep 21, 2010 at 4:08am
Sep 21, 2010 at 4:33am
OK. It did exactly what I wanted it to do. Except for one thing, I used Zhuge's suggestion (it was easiest)but, I changed line 3 to read

getline (cin, userSentence);

instead of

getline (cout, userSentence);.

Does that ruin it?

Thank You!
Sep 21, 2010 at 4:43am
closed account (Lv0f92yv)
The first parameter to getline is an input stream object. Cout is an output stream object, so getline( cout, userSentence ) will not compile.
 
getline( cin, destination ); 
says read from the standard input stream into the 'destination' string.
Last edited on Sep 21, 2010 at 4:44am
Sep 21, 2010 at 6:15am
Indeed, I made a typo. Desh is correct.
Topic archived. No new replies allowed.