Hello, folks. I am posting this code in order to see if anyone can help me solve the time calculator problem. The book states
""" Time Calc
Write a program that asks the user to enter a number of seconds.So...
If, There are 60 seconds in a minute. If the number of seconds entered by the user is
greater than or equal to 60, the program should display the number of minutes in
that many seconds.
If, There are 3,600 seconds in an hour. If the number of seconds entered by the user
is greater than or equal to 3,600, the program should display the number of hours
in that many seconds.
If, There are 86,400 seconds in a day. If the number of seconds entered by the user is
greater than or equal to 86,400, the program should display the number of days
in that many seconds. """
Yeah this is quite irritating, since my logic skills suck. So if anyone is willing to help on this code it would be greatly appreciated.
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
|
#include <iostream>
using namespace std;
int main()
{
double seconds, minutes, hours, days;
cout<<"====================================================="<<endl;
cout<<"===Time Calculator v. 3.0,==="<<endl;
cout<<"=====================================================\n\n"<<endl;
cout<<"Note: The conditional settings.\n\n";
cout<<" If you want a minutes to seconds calculation you want to enter a number >=60\n\n";
cout<<" If you want a hours to seconds calculation you want to enter a number >=3,600\n\n";
cout<<" If you want a days to seconds calculation you want to enter a number >=86,400\n\n";
cin>>seconds;
if (seconds>=60 || seconds <3600 )
cout<< seconds * 60<<"\n\n";
else if (seconds >=3600 || seconds <86400)
cout<< seconds * 3600<<"\n\n";
else if (seconds>= 86400)
cout<< seconds * 86400<<"\n\n\n\n";
else
cout<<"You have entered an non-conversant number. ";
system("pause");
return 0;
}
|
I do get a correct answer when I enter a number >= 60. However if programmed this way the code short circuits and only makes the first statement run. however if I nest the If and make the else if's into if's in the first if like;:
1 2 3 4 5 6 7 8 9 10
|
if (seconds>=60 || seconds <3600 )
{
cout<< seconds * 60<<"\n\n";
if (seconds >=3600 || seconds <86400)
cout<< seconds * 3600<<"\n\n";
if (seconds>= 86400)
cout<< seconds * 86400<<"\n\n\n\n";
}
|
Then this works, but it then shows all three conditions, when I only want the one that is suppose to be met. I know this must be just a logical issue, but obviously I did not achieve the logical rigor this project needs. So if anyone could help this would be greatly appreciated. Thanx