Thread: help with find_if

  1. #1
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20

    Question help with find_if

    In a previous post somebody, i can't remember their name, told me to use find_if. well i decided to use find_if for something else but i cant seem to get it to loop. i built it to validate numbers, at present it does what i want but it only does it once. here is the code.

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <cmath>
    #include <algorithm>
    #include <cctype>
    
    
    using std::cin;
    using std::cout;
    using std::string;
    using std::endl;
    using std::find_if;
    
    bool digi (char p)
    {
    	if (! isdigit(p))
    		return true;
    	else 
    		return false;
    }
    
    int main()
    {
    	string test;
    	do {
    	std::getline(cin, test);
    	if(find_if(test.begin(), test.end(), digi)==test.end())
    	
    		cout << "good";
    	
    	else 
    		cout<< "bad";
    	} while (!digi);
    	
    	return 0;
    }
    i have to validate integer inputs in more than one instance so i also want to make the entire validation code into a function but i will try that after i get it working here. i have a feeling that my problem is the
    Code:
     while (!digi);
    but i dont know what else to put, lol. please forgive the messy code, testing phase is always messy for me.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You dont need the do while loop at all. find_if() iterates over the string for you.
    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

  3. #3
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    Thanks but thats not the problem, sorry if i did not come out clearly. what i really want is for it to keep prompting for input once the current input is bad. currently if i input a letter for example it just outputs "bad" and it finishes. but what i want is for it to say "bad" and then prompt again for input until a good input is given.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In this case, it may be better to use std::string::find_first_not_of() instead of the more general std::find_if() algorithm.
    Code:
    do {
        std::cout << "Enter an integer: ";
        std::getline(std::cin, test);
    } while (test.find_first_not_of("0123456789", 0) != std::string::npos);
    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. #5
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20

    Smile

    thanks that works. since its so short i wont bother to make it into a function. i tried to functionify it once before with my own code and it worked for valid numbers but when i entered an invalid number it stuck in an infinite loop. my piece of code was much longer though. thats why i tried using the bool function instead. i will stick with what you gave me.

    moving along, could you explain in simpleton language what the last zero before the ) represents in the last line of code?
    Code:
     while (test.find_first_not_of("0123456789", 0) != std::string::npos);
    also i was wondering how to call a function from another funtion? like in the following simple attempt.
    Code:
     #include <iostream>
    using namespace std;
    
    int addition (int a, int b)
    {
      int r;
      r=multiply(a,b) + a;
      return (r);
    }
    
    int multiply (int c, int d)
    {
      int y = c+d;
    	return (y);
    }
    
    int main ()
    {
      int z;
      z = addition (5,3);
      cout << "The result is " << z;
      return 0;
    }
    i know it dont make sence mathematically, lol but i was trying something simple to help me undertsand it easier. now that gives me an error multiply identifier not found. what i am attempting to do is use the multiply function in the addition function.
    Last edited by cdkiller; 10-01-2006 at 09:13 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    moving along, could you explain in simpleton language what the last zero before the ) represents in the last line of code?
    Just read about find_first_not_of().

    As for your next problem, you can solve it by:
    a) defining multiply() before addition(), or
    b) place a function prototype for multiply() before addition()
    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

Popular pages Recent additions subscribe to a feed