Thread: PLEASE ! Need help fixing my program

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by zerob2st View Post
    How would that look like?
    that would look like 1 loop with 3 if inside
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zerob2st View Post
    How would that look like?
    Think about it for a while. Come up with a way that allows you to do all three checks in one iteration.
    Also, here's a neat trick:

    std::string s = "Hello World";
    std::transform(s.begin(), s.end(), s.begin(), &std::tolower);
    // s is now "hello world".

    std::transform is a function that takes a sequence denoted by two iterators, and an iterator to where to store the result and finally a functor which to call for every element in the range.
    So simply pit, it takes every element in the sequence, calls the functor and then stores back the result beginning at the iterator denoted by the third argument.
    It may seem a little advanced, but consider it as a challenge. You can use it to learn some new C++ concepts.
    An iterator can be seem as a position. A functor can be seen as a function (ie a function is a functor, but a functor may be more than that; you'll get to that eventually).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by SirPrattlepod View Post
    Since every character in the string must be checked, is there an advantage to using std::find? I think using find would result in requiring four loops (see my suggestion below) similar to how the code is now -- requiring iterating over the string four times when it could be done by iterating over it just once...

    I personally think it would be more beneficial to use the current approach with the following modification: combine the four loops into one.
    This is more or less what i had in mind:

    Code:
    unsigned int count = 0;
    std::size_t found = str.find("gc");
    while(found != std::string::npos)
    {
        found = str.find("gc", found + 2);
        ++count;
    }
    But no, i don't think there is any advantage to this approach. If anything i find the code that the OP ended up using to be simpler and therefore better.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help fixing bugs in program?
    By celticpride in forum C Programming
    Replies: 4
    Last Post: 05-04-2011, 04:19 PM
  2. need help fixing this program
    By truetrini20 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2010, 11:07 AM
  3. need help fixing excercise program...
    By SymDePro in forum C Programming
    Replies: 2
    Last Post: 05-07-2009, 03:33 PM
  4. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM