Class Question

When I compile this program it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 4294967295) > this->size() (which is 77)

Process returned 3 (0x3)   execution time : 2.339 s
Press any key to continue. 

INPUT Data:
12:15 + 06:55
00:59 + 10:01
19:10 - 10:45
20:20 + 07:50
20:20 - 20:30
09:00 + 15:45
03:30 - 07:20


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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

ofstream out("Time.out");

class Time
{
    private:

        int minute;
        int hour;

    public:
        //Default Constructor Function
        Time(){minute = 0; hour = 0;}

        //Setter Functions
        void SetMinute(int M) {minute = M;}
        void SetHour(int H) {hour = H;}

        //Getter Functions
        int GetMinute(){return minute;}
        int GetHour(){return hour;}

        //Modifier Functions
        Time Add(Time T)
        {
            Time temp;
            //IF minutes are greater than 59
            if(minute + T.GetMinute() > 59)
            {
                //IF hours are greater/equal to 24
                if(hour + T.GetHour() + 1 >= 24)
                    temp.SetHour(hour + T.GetHour() + 1 - 24);
                else
                    temp.SetHour(hour + T.GetHour() + 1);
                temp.SetMinute((minute + T.GetMinute()) % 60);
            }
            else
            {
                if(hour + T.GetHour() >= 24)
                    temp.SetHour(hour + T.GetHour() - 24);
                else
                    temp.SetHour(hour + T.GetHour() - 24);
                temp.SetMinute(minute + T.GetMinute());
            }
            return temp;
        }

        Time Subtract(Time T)
        {
            Time temp;
            if(minute - T.GetMinute() < 0)
            {
                if(hour - T.GetHour() - 1 < 0)
                    temp.SetHour(hour - T.GetHour() - 1 + 24);
                else
                    temp.SetHour(hour - T.GetHour() - 1);
                temp.SetMinute(minute - T.GetMinute() + 60);
            }
            else
            {
                if(hour - T.GetHour() < 0)
                    temp.SetHour(hour - T.GetHour() + 24);
                else
                    temp.SetHour(hour - T.GetHour());
                temp.SetMinute(minute - T.GetMinute());
            }
            return temp;
        }

        //Display
        void Display()
        {out << hour << ':' << minute;}
};

void header()
{out << "\t\t\tTime To Time Program\n\n";}

int main()
{
    header();

    int pos = 0,
    AddSubPOS = 0;
    Time T1, T2;
    string data,temp,
    num = "0123456789";
    char k;

    ifstream inf("Time.in");

    while(inf >> k)
         data += k;

    for(pos = 0, AddSubPOS = 0; data.find_first_of(":", pos) != string::npos;)
    {
        //Finds Hour on the left
        pos = data.find_first_of(num, pos);
        temp = data.substr(pos,data.find_first_of(":", pos) - pos);
        pos = data.find_first_of(":", pos);
        T1.SetHour(stoi(temp));

        //Finds Minute on the left
        pos = data.find_first_of(num, pos);
        temp = data.substr(pos,data.find_first_of(":", pos) - pos);
        pos = data.find_first_of(":", pos);
        T1.SetMinute(stoi(temp));

        //Determines symbol
        AddSubPOS = pos;

        //Finds Hour on the right
        pos = data.find_first_of(num, pos);
        temp = data.substr(pos,data.find_first_of(":", pos) - pos);
        pos = data.find_first_of(":", pos);
        T2.SetHour(stoi(temp));

        //Finds Minutes on the right
        pos = data.find_first_of(num, pos);
        temp = data.substr(pos,2);
        pos += 2;
        T2.SetMinute(stoi(temp));

        if(data.at(AddSubPOS) == '-')
        {
            out << "Current Time ";
            T1.Display();
            out << '-' << setfill(' ') << T2.GetHour() << "hr "
                << T2.GetMinute() << "min "
                << " = Future Time";
            T1.Subtract(T2).Display();
            if(data.find_first_of(num,pos) != string::npos)
                out << endl;
        }
        if(data.at(AddSubPOS) == '+')
        {
            out << "Current Time ";
            T1.Display();
            out << '+' << setfill(' ') << T2.GetHour() << "hr "
                << T2.GetMinute() << "min "
                << " = Future Time";
            T1.Add(T2).Display();
            if(data.find_first_of(num,pos) != string::npos)
                out << endl;
        }
    }
    out.close();
    inf.close();
    return 0;
}
At line 110, the find_first_of(":", pos) will find the ':' that follows the next hour.

Your parsing code is awkward. Consider creating a method that parses a time. Since you aren't doing any error checking now, I won't either:
1
2
3
4
5
6
    istream & read(istream &strm)
    {
        char colon;
        strm >> hour >> colon >> minute;
        return strm;
    }


Then the loop becomes simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   while (T1.read(inf)) {
        inf >> k;               // read the operation
        T2.read(inf);           // read the next time

        if(k == '-')
        {
            out << "Current Time ";
            T1.Display();
            out << '-' << setfill(' ') << T2.GetHour() << "hr "
                << T2.GetMinute() << "min "
                << " = Future Time";
            T1.Subtract(T2).Display();
        }
        if(k == '+')
        {
            out << "Current Time ";
            T1.Display();
            out << '+' << setfill(' ') << T2.GetHour() << "hr "
                << T2.GetMinute() << "min "
                << " = Future Time";
            T1.Add(T2).Display();
        }
        out << '\n';
    }

Topic archived. No new replies allowed.