Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,392 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,251 people online right now. Registration is fast and FREE... Join Now!




character to terminate srting

 
Reply to this topicStart new topic

character to terminate srting

zerogee
post 21 Aug, 2008 - 06:29 PM
Post #1


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

I wrote a program to count lines (which is any sequence of character followed by the Enterkey). As i understand it a \n is the same as the Enter key, so i wrote the code
CODE
getline(cin, message, '\n');
but when i count with
CODE
LineCount = count(message.begin(), message.end(), '\n');
i get a return of "0". i also tried the \0 character but it also did not work. anyway i am stuck. here is the complete program:

CODE
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main ()
{
int LineCount = 0;


string message;
  {
      cout <<  "Week 2 Project, #1\n";
      cout << "#########################################\n\n\n\n\n";

   cout << "Enter string of several sentences: \n";
   cout << "======================================\n";
   getline(cin, message, '\n');
      
  }

     LineCount = count(message.begin(), message.end(), '\n');        // count new line
    
    
cout << "\n\n" << "The appears to be " << LineCount << " line in the string.\n\n";

return 0;
}
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 21 Aug, 2008 - 06:57 PM
Post #2


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


Well, if you take a look at the documentation to the getline function (http://www.cplusplus.com/reference/string/getline.html), you'll note that getline reads a line from the stream, up until the delimiter character, and then discards the delimiter. The string will never have a '\n' in it because of this. Plus, if you want to count lines, you'll need to read in more than one.
User is offlineProfile CardPM

Go to the top of the page

zerogee
post 21 Aug, 2008 - 08:19 PM
Post #3


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

QUOTE(perfectly.insane @ 21 Aug, 2008 - 07:57 PM) *

Well, if you take a look at the documentation to the getline function (http://www.cplusplus.com/reference/string/getline.html), you'll note that getline reads a line from the stream, up until the delimiter character, and then discards the delimiter. The string will never have a '\n' in it because of this. Plus, if you want to count lines, you'll need to read in more than one.



I understand what you are saying and yes I already knew about the last chacater (terminating character) not being counted/stored. Also, the only way i know to add lines is to terminate with a "." or "?" or "!" or some other chracter and when you want another line you press the Enter key and this takes you to the next line. With that being said, the assignment states that a line ends with a "Enter key. So how do you have more than one line and not have a special terminating character? I just may not be understanding the assignment right, but i am at a lost.

Ok, how about this. suppose i terminate the string with "%" and when you want a new line just press the Enter key. so when using the count code;
CODE
LineCount = count(message.begin(), message.end(), '????');

What character do i use to count the Enter key stroke???
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 22 Aug, 2008 - 02:40 PM
Post #4


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


You would use '\n'. Your getline would need to use a different delimiter (in this case, '%').
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 07:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month