Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Diliphindustani opened this issue Oct 20, 2023 · 2 comments

Comments

@Diliphindustani
Copy link

No description provided.

@peeyushtripathi
Copy link

peeyushtripathi commented Oct 21, 2023

#include
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); 

};

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
---------------------------------------------------------------------------------*/

@Raja-Muhammad-Bilal-Arshad
Copy link

Raja-Muhammad-Bilal-Arshad commented Oct 19, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants