What's wrong with the code...... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's wrong with the code......

I'm trying to add two times and display them. https://code.sololearn.com/cPGFU1P0f9V5

20th Sep 2019, 2:48 AM
Md Ibrahim (West Bengal, India)
Md Ibrahim (West Bengal, India) - avatar
4 Answers
+ 2
Md Ibrahim (West Bengal, India) , compare it with your code and if you don't understand anything, ask for clarification.. few points I am mentioning: after cout and << ; you cannot call void function. void function doesn't return any thing and cout << requires something to print... so moving function call to new line without cout. in sum function, you were returning t which is time type...so changed it's return type from int to time in definition... within class declaration of this function, changed type to time instead of void. in function of sum, time t,r.hour=0; is incorrect... r.hour is of time other than time. int a,b means a and b both are of int type. so, time t and r.hour=0 should be seperated by semicolon
20th Sep 2019, 3:24 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Md Ibrahim (West Bengal, India) below is working code: #include<iostream> using namespace std; class Time { int hours,minutes; public: void input(int h, int m) { hours=h; minutes=m; } void display() { cout<<hours<<" hours and "<<minutes<<"minutes"<<endl; } Time sum(Time);//declarations with object arguments }; Time Time::sum(Time t1) { Time r; r.minutes=0,r.hours=0; r.minutes=minutes+t1.minutes; r.hours=r.minutes/60; r.minutes=r.minutes%60; r.hours=hours+t1.hours+r.hours; return r; } //main function int main() { Time a1; Time a2; Time a3; a1.input(2,45); a2.input(3,30); a3=a1.sum(a2);//T3=T1+T2; cout<<"T1= "; a1.display(); cout<<"T2= "; a2.display(); cout<<"T3= "; a3.display(); }
20th Sep 2019, 3:20 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Ketan Lalcheta Thank you so much for your valuable time...It's working.
21st Sep 2019, 1:27 PM
Md Ibrahim (West Bengal, India)
Md Ibrahim (West Bengal, India) - avatar
20th Sep 2019, 2:59 AM
Md Ibrahim (West Bengal, India)
Md Ibrahim (West Bengal, India) - avatar