I am Stuck and have been for 3 weeks now.

Posted on 2009-04-29
Medium Priority
Last Modified: 2012-05-06
Write a function to count the number of lines entered using a string class object. Consider a line as any sequence of characters followed by the Enter key.

Here's what I have:
int ndex, count = 0; 
index = stng.find ((
if (index = stng.find((char)10, index++) 
string message;
cout << "Enter a string:\n";
getline(cin, count);
return count; 
cout << "The number of strings entered is:\n"
<< message << endl;

Open in new window

Question by:Foufyter
1 Comment
LVL 40

Accepted Solution

evilrix earned 120 total points
ID: 24267539
Hint: std::getline(stream,. string) captures a complete line. All you need to do is sit in a loop and count how many times this function returns.

Pseudo code:

someString== ""
lineCount= 0
  getline(inputStream, someString)
  if someString.length > 0 then lineCount+ lineCount+ 1
while someString.length > 0

Alternatively, if you have single string that contains multiple lines count the number of linefeeds

Pseudo code:

someString =  "this is a line\nthis is anotehrline\n and so is this\n"
lineCount = 0
stringIndex = 0

  if someString[stringIndex ] == '\n' lineCount = lineCount  + 1
  stringIndex = stringIndex  + 1
while stringIndex < someString.Length

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question