Not sure why do I always get a 0 from the isvalidReal function?

int isvalidReal(string signedNum)
{

    bool check;
    int i = 0, j = 1;
    string str = "+-.";

    for (i = 0; i <= signedNum.length(); i++)
    {
        if (isdigit(signedNum[i]))
        {
            check = 1;
        }
        else if (signedNum[0] == (str[0] || str[1]) && 
                 signedNum[1] == isdigit(signedNum[1]))
        {
            check = 1;
        }
        else if ((signedNum[i] == str[2]) && j == 1)
        {
            check = 1;
            j = 0;
        }
        else 
        {
            check = 0;
            break;
        }

    }

    return check;
}

int main()
{

    string signedNum;
    bool check;

    cout << "Type a real number with a +/- sign: \n";
    cin >> signedNum;

    check = isvalidReal(signedNum);
    if (check == 1)
        cout << "This is a real number" << endl;
    else
        cout << "This is not a real number" << endl;



    char aaaa;
    cin >> aaaa;
}

Well, this is wrong:

 else if (signedNum[0] == (str[0] || str[1]) &&
signedNum[1] == isdigit(signedNum[1]))

You should use

 else if (signedNum[0] == (str[0] || str[1]) && isdigit(signedNum[1]))

Here's something you might find interesting. A way to check if a string is a valid number using sscanf, which simplifies the code trmendously:

bool isvalidReal(string input)
{
    //Variable to hold the number
    float num = 0;
    //Variable to hold any left over characters
    char temp[20];
    //If all the characters are used parsing to a number then the return is true
    //If no characters are used or if some are read as a string then the return is false
    int result = sscanf(input.c_str(), "%f %s", &num, &temp);
    if (result != 1)
        return false;
    else
        return true;
}

If necessary the number can be returned by passing the address of a float variable to the function and using the address to that in sscanf.

On a side note, if you're return is a bool you should declare it as such and return true or false, instead of 0 or 1. This greatly reduces the risk of forgetting what the numbers represent and throwing other numbers in there.

Edited 2 Years Ago by tinstaafl

there are a few mistakes in your code.

1.

when you're defining a return type as bool then why are you returning an integer.so either make it int or change your return value to true/false.

2.

for (i = 0; i <= signedNum.length(); i++)

suppose signedNum=3.14 then its length is 4 but its last element is at 3rd index.therefore change this line to this

for (i = 0; i < signedNum.length(); i++)

3.

include only those lines in loop which are necessary(to be placed inside loop).your this line else if (signedNum[0] == (str[0] || str[1]) && isdigit(signedNum[1])) inside loop doesn't make sense. so you could write this just before loop.Add this just just before loop

    if ((signedNum[0] == str[0] || signedNum[0]== str[1]) && isdigit(signedNum[1]))
        {
            check = 1;
        }
    else
    {
        cout<<"number is either not signed or not numeric";
        return 0;
    }

Now start your loop with 1(i=1)

at last in loop, write this

    if(j==0) //if j==0 then number contain '.'(its real number) and return 1 otherwise return 1-1(i.e check-1)
        return check;
    return check-1;

Now , in main declare check as integer type.

you might also consider to return a value , in main(). therefore write this at the last line

return 0;

now your code would look like this :

int isvalidReal(string signedNum)
{
    int check;
    int i = 0, j = 1;
    string str = "+-.";
    if ((signedNum[0] == str[0] || signedNum[0]== str[1]) && isdigit(signedNum[1]))
        {
            check = 1;
        }
    else
    {
        cout<<"number is either not signed or not numeric";
        return 0;
    }
    for (i = 1; i < signedNum.length(); i++)
    {
        if (isdigit(signedNum[i]))
        {
            check = 1;
        }

        else if ((signedNum[i] == str[2]) && j == 1)
        {
            check = 1;
            j = 0;
        }
        else
        {
            check = 0;
            break;
        }
    }
    if(j==0)
        return check;
    return check-1;
}
int main()
{
    string signedNum;
    int check;
    cout << "Type a real number with a +/- sign: \n";
    cin >> signedNum;
    check = isvalidReal(signedNum);
    if (check == 1)
        cout << "This is a real number" << endl;
    else
        cout << "This is not a real number" << endl;
    return 0;
}

Edited 2 Years Ago by Learner010

This article has been dead for over six months. Start a new discussion instead.