简体   繁体   中英

How can I display the number minutes, hours, or days from the amount of seconds that I have in C++?

//courseID:CIS165-006HY
//name: Omar Barrera
//Prof. Wang
//Assignment#4
//Due by 03/07/2020

#include <iomanip>
#include <iostream>
using namespace std;

int main() {
int 86400 seconds = 1 day,
int 3600 seconds = 1 hour,
int 60 seconds = 1 minute,
int number;

cout << "Enter amount of Seconds" << endl;
cin >>number>>endl;



if (number >= 86400)
    cout<<number<<"day(s)"<<endl;
else if (86400 >= number >= 3600)
    cout<<number<<"hour(s)"<<endl;
else if (3600 >= number >= 60)
    cout<<number<<"minute(s)"<<endl;
else (number < 60)
    cout<<number<<"seconds"<<endl:

return 0;
}

The error that popped up in my compiler was

main.cpp: In function 'int main()': main.cpp:12:5: error: expected unqualified-id before numeric constant int 86,400 seconds = 1 day, ^~ main.cpp:18:7: error: 'number' was not declared in this scope cin >>number>>endl;

C++ doesn't work like maths

else if (86400 >= number >= 3600)

should be

else if (86400 >= number && number >= 3600)

I recommend you change your logic to use more math:

int main()
{
    unsigned int number = 0;

    cout << "Enter amount of Seconds" << endl;
    cin >>number>>endl;

    const double days = number / 86400.0;
    const double hours = number / 3600.0;
    const double minutes = number / 60.0;

    std::cout << "Days:    " << days    << "\n";
    std::cout << "Hours:   " << hours   << "\n";
    std::cout << "Minutes: " << minutes << "\n";

    return 0;
}

Note that I eliminated the invalid variable declarations that followed main .

I also moved the calculations to after the number is input from the User.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM