Program crash?

Hello, so basically this program is to measure distance mile, yards, feet and inches. When I try to build and run program below it crashes. After checking debugger it says that problem is in Distance cpp line 111 and "SIGSEGV, Segmentation fault". I can't find solution for this problem.

Header:

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
#include <iostream>
#include <iomanip>
#include <cstdlib>

#ifndef DISTANCE_H_INCLUDED
#define DISTANCE_H_INCLUDED


class Distance{
private:
    int judze;
    int jardi;
    int peda;
    int colla;
public:
    Distance();
    Distance(int, int, int, int);
    Distance(int);

    int get_judze() const;
    int get_jardi() const;
    int get_peda() const;
    int get_colla() const;

    void set_judze(int);
    void set_jardi(int);
    void set_peda(int);
    void set_colla(int);

    void izvadit() const;

    friend std::ostream& operator<< ( std::ostream &os, const Distance& d);

    friend std::istream& operator>> ( std::istream &is, Distance& d);
};

#endif // DISTANCE_H_INCLUDED 



Distance cpp:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <iomanip>
#include <cstdlib>

#include "distance.h"

Distance::Distance() : judze(0), jardi(0), peda(0), colla(0){}


Distance::Distance(int c){
    if(jardi>0){
        jardi=0;
    }
    if(c<0){
        peda=0;
        jardi=0;
        judze=0;
        colla=0;
    }
    else{
        colla=0;
        peda=0;
        jardi=0;
        judze=0;
    while(colla>12){
        peda++;
        colla-=12;
    }
    while (peda>3){
        jardi++;
        peda-=3;
    }
    while (jardi>1760)
        judze++;
        jardi-=1760;
    }

}

Distance::Distance(int ju, int ja, int p, int co){
    judze = ju;
    jardi = ja;
    peda = p;
    colla = co;
    }



int Distance::get_colla() const{
    return colla ;
}

int Distance::get_jardi()const{
    return jardi;
}

int Distance::get_judze()const{
    return judze;
}

int Distance::get_peda()const{
    return peda;
}

void Distance::set_judze(int a){
    judze = a;
}

void Distance::set_jardi(int b){
    jardi = b;
}

void Distance::set_peda(int e){
    peda = e;
}

void Distance::set_colla(int f){
    colla = f;
}

void Distance::izvadit() const{

    std::cout << std::setw(2) << colla << std::endl;
    std::cout << std::setw(2) << jardi << std::endl;
    std::cout << std::setw(2) << judze << std::endl;
    std::cout << std::setw(2) << peda  << std::endl;
}

std::ostream& operator<<( std::ostream &os, const Distance& d){
        if(d.get_judze() == 0){
            if(d.get_jardi() == 0){
                if(d.get_peda() == 0){
                os << d.get_colla();
                }
    else{
        os << d.get_peda() << "f"<< d.get_colla() << "inch" << std::endl;
    }
            }
    else{
       os << d.get_jardi() << "y" << d.get_peda() << "f" << d.get_colla() <<   "inch" << std::endl;
    }
        }

    else{
        os << d.get_judze() << "m" << d.get_jardi() << "y" << d.get_peda() << "f" << d.get_colla() << "inch" << std::endl;
    }
    return os;
    }

    std::istream& operator>>(std::istream & is, Distance& d){
    is >> d.get_judze() >> d.get_jardi() >> d.get_peda() >> d.get_colla();

    return is;
    }



And what should I write in so for example it would convert "1234" to "0 34 0 10" .

If I build and run this it would show "-1760 0 0 0".
Last edited on
Maybe try this:
111
112
//  is >> d.get_judze() >> d.get_jardi() >> d.get_peda() >> d.get_colla();
    is >> d.judze >> d.jardi >> d.peda >> d.colla;
Look at line 111 and tell me what you are trying to do. Then explain exactly how the first three symbols ("is >> d.get_judze()") accomplishes this.
@Chervil Thanks it fixed it


@PanGalactic I was trying to make cin operator overload to simply put, where I write with spaces for example "1 2 3 4" and it would show "1 2 3 4" but I have came across problem where I have to make it work if I write 12 inches then it would increase feet by 1, if I write 3 feet it would increase yards by 1 if I have 1760 yards it would increase miles by 1. Simply put it would look something like this - when I write "1 2 3 12" it would convert to "1 3 1 0" something like this.

So, I don't know how do I do that part.
Last edited on
Topic archived. No new replies allowed.