You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Getting compilation error in this c++ program
#include <iostream>
using namespace std;
class Time
{
int h, m, s;
public:
Time()
{
h = 0, m = 0, s = 0;
}
void setTime();
void show()
{
cout << h << ":" << m << ":" << s;
}
Time operator+(Time);
};
Time Time::operator+(Time t1)
{
Time t;
int a, b;
a = s + t1.s;
t.s = a % 60;
b = (a / 60) + m + t1.m;
t.m = b % 60;
t.h = (b / 60) + h + t1.h;
t.h = t.h % 12;
return t1;
}
int main()
{
Time t1, t2, t3;
cout << "enter the first time";
t1.setTime();
cout << "\n Enter the second time";
t2.setTime();
t3 = t1 + t2;
cout << "\n first time";
t1.show();
cout << "\nsecond time";
t2.show();
cout << "\n sum of times";
t3.show();
return 0;
}
#420
class Time {
int h, m, s;
public:
Time()
{ h = 0, m = 0, s = 0;
}
void setTime();
void show() {
cout << h << ":" << m << ":" << s;
}
Time operator+(Time);
};
void Time::setTime()
{
char tim[9];
std::cin>>tim;
int hh;
int mm;
int ss;
hh = atoi(&tim[0]);
mm = atoi(&tim[3]);
ss = atoi(&tim[6]);
this->h=hh;
this->m=mm;
this->s=ss;
}
Time Time::operator+(Time t1) {
Time t;
int a, b;
a = this->s + t1.s;
t.s = a % 60;
b = (a / 60) + this->m + t1.m;
t.m = b % 60;
t.h = (b / 60) + this->h + t1.h;
t.h = t.h % 12;
return t; }
int main() {
Time t1, t2, t3;
cout << "enter the first time hh:mm:ss";
t1.setTime();
cout << "\n Enter the second time";
t2.setTime();
t3 = t1 + t2;
cout << "\n first time";
t1.show();
cout << "\nsecond time";
t2.show();
cout << "\n sum of times";
t3.show();
return 0;
}
/*----------------------------------------------------------------------------------------------------------------
──(shiv㉿kali)-[~/GIT_REPOS/online_c++_questions]
└─$ ./a.out
enter the first time hh:mm:ss10:13:45
Enter the second time13:45:56
first time10:13:45
second time13:45:56
sum of times11:59:41
┌──(shiv㉿kali)-[~/GIT_REPOS/online_c++_questions]
└─$ ./a.out
enter the first time hh:mm:ss01:12:23
Enter the second time03:04:05
first time1:12:23
second time3:4:5
sum of times4:16:28
---------------------------------------------------------------------------------*/
Missing Implementation of setTime(): The setTime() function is declared but not defined. You need to implement this function to allow the user to input time values.
Incorrect Return Statement in operator+: In the operator+ function, you are returning t1 instead of the newly created t. This means the result of the addition is not being returned correctly.
Time Format Handling: The handling of hours should consider a 24-hour format or a 12-hour format correctly. The current implementation uses % 12, which may not be appropriate for all cases.
With these changes, your program should compile and run correctly, allowing the user to input two times and then output their sum. The time is handled in a normalized manner, ensuring that the output is valid.
No description provided.
The text was updated successfully, but these errors were encountered: