Verifying Input and passing Assert

My program needs to accept various user input. Some of those numbers are to be doubles (between 0 and 1). Even though I enter a conforming number (like 0.4 or just .4), the assert functions fail. This jumps around a bit, but here are the relevant lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
...
    Program_Constants my_const;

    cout << "Enter a decimal between 0.00 and 1.00: ";
    cin >> str;
    arrival_prob = verify_double(str);
    str.clear();
    cin.clear();
    my_const.set_arrival_prob(arrival_prob);

    ...
    run_sim(my_const)
}


1
2
3
4
5
double verify_double(string str)
{
   ...
   return atof(str.c_str());
}


1
2
3
4
void Program_Constants::set_arrival_prob(double arr_prob)
{
    arrival_prob = arr_prob;
}


1
2
3
4
5
6
7
8
void run_sim(&my_const)
{
   ...
   const double ARRIVAL_PROB = my_const.get_arrival_prob();

   Bool_source arrival(ARRIVAL_PROB);
   ...
}


1
2
3
4
5
6
Bool_source::Bool_source(double arr_prob)
{
   assert (arr_prob < 1);
   assert (arr_prob > 0);
   probability = arr_prob;
}


If I input say,
.4
, and add a cout << arr_prob immediately before the assert statements, it shows me
0.4
, so I don't get why the assert fails. If I take the asserts out, the whole program fails, so clearly something is wrong.

Thanks for looking at this. I appreciate it.
I asserted after the original input and it ran fine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <string>
using namespace std;

double verify_double(string str)
{
   return atof(str.c_str());
}

int main()
{
    string str;
    double arrival_prob;
    //Program_Constants my_const;

    cout << "Enter a decimal between 0.00 and 1.00: ";
    cin >> str;
    cin.clear();
    cout << str << endl;
    arrival_prob = verify_double(str);
    assert (arrival_prob < 1.0 && arrival_prob > 0.0);
    cout << arrival_prob << endl;
    str.clear();
    // my_const.set_arrival_prob(arrival_prob);

    // run_sim(my_const);
    return 0;
}

The problem has to be somewhere after those assertions. Somewhere along a data type or something could be different. Otherwise I see nothing wrong with the code.
Thanks for looking at this. You're right, this code was fine. I found the problem in my set functions. I need some work on my debugging skills. Sorry to waste your time on this, but confirming the code was very helpful.

Thanks again!
Topic archived. No new replies allowed.