Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
lights,

Added whitespace trimming to front and back of input.
De-templatized 'getValue' which now expects 'value' refernece to be of type float.


Code:
#include <iostream>
#include <string>
#include <sstream>

#if 0

void getInput(float& value, const std::string& strPrompt, const std::string& strRepromptError)
{
    do
    {
        std::cout << strPrompt << std::flush;

        std::string     str;
        getline(std::cin, str);
        
        std::istringstream iss(str);
        if ( iss >> value )
        {
            break;
        }

        std::cout << strRepromptError << std::endl;
    } while ( true );
}

#else

void getInput(float& value, const std::string& strPrompt, const std::string& strRepromptError)
{
    std::string strTrimChars(" \t\r\n");

    int         decimal_points;
    bool        invalid;
    std::string str;
    do {
        std::cout << strPrompt << std::flush;
        getline(std::cin, str);

        if ( std::string::npos != str.find_last_not_of(strTrimChars) )
        {
            str.erase(str.find_last_not_of(strTrimChars) + 1) ; 
        }
    
        str.erase(0, str.find_first_not_of(strTrimChars)) ; 


        invalid         = false;
        decimal_points  = 0;

        std::string::iterator itr = str.begin();
        if ( ('-' == *itr) || ('+' == *itr) )
        {
            itr++;
        }

        for ( ; itr != str.end(); itr++ )
        {
            if ( !isdigit(*itr) )
            {

                if ( ('.' == *itr) && (++decimal_points <= 1) )
                {
                    continue;
                }

                invalid = true;
                std::cout << strRepromptError << std::flush;
                break;
            }
        }
    } while ( invalid );

    std::istringstream  iss(str);
    iss >> value;
}

#endif


int main(int argc, const char* argv[])
{
    std::string strError("Invalid entry, please try again!\n");

    float   value;
    
    while ( true )
    {
        getInput(value, "Give me a number: ", strError);
        if ( value >= 0.0f )
        {
            break;
        }

        std::cout << "Sorry, please provide a positive value!\n\n";
    }

    std::cout << "User provided value: " << value << std::endl;

    return EXIT_SUCCESS;
}
 
llyoddean! that code is so awesome, it works beautifully!

I wish I can use it since it checks for trailing errors and everything else.

Sadly, again, the code looks too advance for us students. But he never said we can't look at other header files and use some of those functions to implement an error check. Hmmm, I'm going to try to understand exactly how your code works and if I have time, maybe I can try to use it.

Thanks =)
 
Thanks for all the help guys, As I stated in my other thread, I believe I got the highest programming score and extra credit points.

While I did not use the code you guys helped me with, I learned a lot from you guys.

I ended up going with a do while statement for the error checks. And my professor didn't even check for errors lol, except for negative numbers, but he did see it and he liked that I took the steps to try to do some basic error checks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.