Hi,
I can't find my program broblem plz help me!
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>
using namespace std;
class Time{
int S,M,H;
public:
Time (int h,int m,int s){S=s;M=m;H=h;}
Time (){S=0;M=0;H=0;}
void show(){
cout << H<<":"<<M<<":"<<S;
}
Time operator +(Time T){
Time temp;
temp.H= H + T.H;
temp.M= M + T.M;
temp.S= S + T.S;
if (temp.S >= 60) {
temp.S -= 60;
temp.M++;
}
if (temp.M >= 60) {
temp.M -= 60;
temp.H++;
return temp;
}
}
};
int main()
{
Time T1(2,20,33),T2(3,30,37),T3;
cout << "T1: ";
T1.show();
cout << "\nT2: ";
T2.show();
T3= T1 + T2;
cout << "\n\nT3: ";
T3.show();
}
|
OUTPUT:
T1: 2:20:33
T2: 3:30:37
T3:
33:0:51
//T3 is not true!
Last edited on
Your operator + won't return anything if temp.M < 60. Move that return out of that if statement.
How is that compiling anyway?
Woooow, tanx
i can't see that! :) :D
look this:
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
|
#include <iostream>
using namespace std;
class Time{
int H,S,M;
public:
Time(int h,int m,int s)
{
H=h;M=m;S=s;
}
Time()
{
H=0;S=0;M=0;
}
void showtime()
{
cout << H << ":"<< M << ":" << S;
}
Time operator+(Time T)
{
Time temp;
temp.H=H+T.H;
temp.M=M+T.M;
temp.S=S+T.S;
if (temp.S >= 60) {
temp.S -= 60; temp.M++;
}
if (temp.M >= 60) {
temp.M -= 60; temp.H++;
}
return temp;
}
};
int main()
{
Time T1(2,33,20),T2(3,41,32),T3;
T3 = T1 + T2;
T3.showtime();
}
|
worked!
http://cl.ly/81zG/Screen_shot_2011-06-29_at_9.44.07_PM.png
Last edited on