A Simple Word Wrap Function

Started by
3 comments, last by Enigma 17 years, 10 months ago
Hey guys, C++ is giving me troubles. Well.. Not really, the logic all seems to be right, just having a problem with the 'getline(cin, words);' line. the variable 'words' is a char[80]; Does the getline command need words to be a 'string'? Anyway, what I'm trying to do is write a function that formats the lines on teh screen. Right now, I have it set to check at 15 spaces, if the chararray is longer than 15 spaces, it checks words[15] to see if its a space. If so, it changes that space into a '/n' char. If it is not, it checks words[14] all the way back until it finds a space. Then I have it cout << words; I'd have the source code in here, but its on my laptop. If any of you guys can shed some light on the getline(cin, words) command, that'd be great. The logic in the loop to check for a space is right... No errors on compile there. getline returns an error saying its a type mismatch with the words variable. Thanks guys! MMcNeil
Advertisement
Quote:Original post by MMcNeil
Does the getline command need words to be a 'string'?

Yes.
That sucks? Can I still look at individual characters in a 'string' like you can with a char array?

string words;

words[15] = 'a'?

MMcNeil
Quote:Original post by MMcNeil
That sucks? Can I still look at individual characters in a 'string' like you can with a char array?

string words;

words[15] = 'a'?

MMcNeil


yes.
The getline member function of istream can be used with char *s, but using it is often a bad idea for the same reason using raw char *s are generally a bad idea in C++.

C++ std::string objects can be used with a (very little) bit of work just about anywhere that raw char *s can be, with the added bonuses of exception safety, automatic memory management and, often, increased efficiency. In particular, operator[] is fully supported.

Σnigma

This topic is closed to new replies.

Advertisement