Please help with a c++ string

This is a discussion on Please help with a c++ string within the C++ Programming forums, part of the General Programming Boards category; Thanks for your help guys. Got it all figured out....

  1. #16
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks for your help guys. Got it all figured out.

  2. #17
    Registered User Dawnson's Avatar
    Join Date
    Oct 2009
    Location
    In front of my computer
    Posts
    6
    Quote Originally Posted by King Mir View Post
    If your using stl functions, you may as well use count:

    Code:
    mycount = count (message.begin(), message.end(), letter);
    But jeffcobb's first example is what the book was describing.
    I know how to cope with the problem that the first floor has asked, but you give me another method to handle it. So, I must show my thanks.

  3. #18
    Registered User Dawnson's Avatar
    Join Date
    Oct 2009
    Location
    In front of my computer
    Posts
    6
    Quote Originally Posted by King Mir View Post
    If your using stl functions, you may as well use count:

    Code:
    mycount = count (message.begin(), message.end(), letter);
    But jeffcobb's first example is what the book was describing.
    But this code seems to be wrong so that I can't compile it.Why?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Quote Originally Posted by Dawnson
    But this code seems to be wrong so that I can't compile it.Why?
    Dunno, it works for me:
    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        string message("Dawnson");
        char letter = 'n';
        string::size_type mycount = count(message.begin(), message.end(), letter);
        cout << mycount << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,054
    The type of mycount should be the string::difference_type, not string::size_type apparently.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Quote Originally Posted by King Mir
    The type of mycount should be the string::difference_type, not string::size_type apparently.
    Ah. Luckily, the result of std::count will be non-negative, the conversion from signed to unsigned is well defined, and if it is possible for the count to be greater than the maximum value of size_type, then we're probably doomed anyway since size() might return an incorrect value
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21