Segmentation fault (core dumped)

Help - Segmentation fault (core dumped). I am going to try to comment out line by line to fix. I wish they gave you a line number. Anybody see anything obvious?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 18 float calc_rect_area(float, float);
 19 
 20 int main ()
 21 {
 22    float check, low, high;
 23    float width, height;
 24    string message;
 25 
 26    get_float(message);
 27    check_valid_input(check, low, high);
 28    calc_rect_area(width, height);
 29 
 30    const float MIN_VALUE = 1;
 31    const float MAX_VALUE = 10;
 32 
 33    float my_input = get_float("Enter a number from 1 to 10 ");
 34    float sample_width = get_float("Enter the width for some imaginary rectangle");
 35    float sample_height = get_float("Enter the height for some imaginary rectangle");
 36 
 37    cout << my_input << endl;
 38 
 39    cout << check_valid_input(my_input, MIN_VALUE, MAX_VALUE) << endl;
 40 
 41    cout << calc_rect_area(sample_width, sample_height) << endl;
 42 
 43    return 0;
 44 }
 45 
 46 float get_float(string message)
 47 {
 48    float check = atof(message.c_str());
 49    return check;
 50 }
 51 
 52 float check_valid_input(float check, float low, float high)
 53 {
 54    float neg = -1.0;
 55 
 56    if(check >= low && check <= high)
 57    {
 58       return check;
 59    }
 60    else
 61    {
 62       return neg;
 63    }
 64 }
 65 
 66 float calc_rect_area(float width, float height)
 67 {
 68    if (height == 0)
 69       return 0;
 70    else
 71       return width + calc_rect_area(width, --height);
 72 }
well, the lines in your code seem to be numbered... in the code!
I mean the segmentation fault error, why not give me a line where the error is?
Why are you calling check_valid_input() and calc_rect_area() on lines 10 and 11, when their arguments have not been initialized?
> I mean the segmentation fault error, why not give me a line where the error is?
¿never heard of a debugger?

http://www.cplusplus.com/forum/general/112111/
How should i initialize check_valid_input() and calc_rect_area() on lines 10 and 11?
Last edited on
Why do you even need lines 9, 10 11(or 26, 27, 28)? Shouldn't those lines simply be removed?
core no longer dumped. new problems, new post
Well, there's no place where the questions could be asked. For example the text "Enter a number from 1 to 10 " is passed to the function get_float() which in turn passes it to the function atof() which attempts to parse the string in the hope that it starts with a valid floating-point number. Since it doesn't, the returned value will be zero.

Because 0 is not in the range 1 to 10, the check_valid_input() function will return -1.0.
What everyone is saying is that you have declared variables but not given any value to them. Then, without value, you try to work with them. You must initialize your variables first. That means give them some starting value.
Topic archived. No new replies allowed.