Sign in to follow this  

A Simple Word Wrap Function

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this