0

I have a function which uses an std string parameter to test if there is an alpha character. It would be best to test for only numeric characters but I have not gotten that far just yet. I just am trying to get it to recognize if the input does not have a number in it. If not it loops the error message until there is only numbers. After this I am trying to convert the string to double by use of atof() so it can be returned in main(). I get a debug assertion failed! Message upon runtime which says, expression string subscript out of range if a number is put in. Other wise if a letter has input, it keeps looping its self with the error message. I got my code for the function below. Any one have any clues as to what I am doing wrong? I am out of ideas...

double Bet::betProb(std::string b)
{
    bool alphChar = false;
    double doubleBet;
    for(int i = 0; i < b.size(); i++){
        if(isalpha(b[i])){
            alphChar = true;
        }
    }
    while(alphChar){
        cout << "Error! Bet only with numbers." << endl;
        cin >> b;
        for(int i = 0; i < b.size(); i++){

            if(!isalpha(b[i])){
                alphChar = false;
            }
        }
    }

    string F=b;
    int T=F.size();
    char Change[100];
    for (int a=0;a<=T;a++)
    {
        Change[a]=F[a];
    }

    doubleBet = atof(Change);

    return doubleBet;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Road Rash
  • 131
  • 3
  • 14

1 Answers1

6

Since your problem has already been resolved, I thought I'd show you the way this would be done using standard C++ functionality:

#include <string>
#include <stdexcept>

double Bet::betProb(const std::string& str)
{
    double d;

    try {
        d = std::stod(str);
    } catch (const std::invalid_argument&) {
        std::cerr << "Argument is invalid\n";
        throw;
    } catch (const std::out_of_range&) {
        std::cerr << "Argument is out of range for a double\n";
        throw;
    }
    return d;
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • I see what you are saying. Use try catch. I have not studied how to do try catch much. Could you show me how I would implement this into what I am trying to accomplish. – Road Rash Nov 17 '13 at 04:36
  • @RoadRash `std::stod` has the ability to throw exceptions; if that happens, we *catch* them and either rethrow the exception, ignore the exception, or make a valid response. In this case, I made a valid response *and* rethrew the argument: I write to standard output messages regarding those possible outcomes (i.e invalid argument or an out or range value) and the `throw;` statement below them (recent edit) rethrows the caught exception. – David G Nov 17 '13 at 14:14
  • @RoadRash In your case, you might want to use [`std::strtod`](http://en.cppreference.com/w/cpp/string/byte/strtof) (A C library function) since they report error handling without using exceptions (so this might be more easy for you to handle). Read up on the different return values of the function. – David G Nov 17 '13 at 14:25