I am Stuck and have been for 3 weeks now.

Posted on 2009-04-29
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 39

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now