time calculator

im haveing trouble getting this thing to work. the program is to work like this
if there are 60 seconds in a minute it will show the time in number of mintues in that many seconds and 3,600 in an hour the program should display the number of hours in minutes and 86,400 seconds in a day and show the same for days but i keep getting if statement errors in the program.



#include<iostream>
using namespace std;
int main ()

{


double minute = 60.0,
hour = 3600.0,
day = 86400.0,
value,
value1;

cout << "please enter a number of minutes:";
cin >> value;

if (hour >= value)
value1 = value * hour;
cout <<"the number of seconds in minute is:" << value1 <<endl;



else
if (day >= value)
value1 = value * day;
cout <<"the number of seconds in hour is:" << value1 <<endl;


else value1 = value * minute;
cout <<"the number of seconds in day is:" << value1 <<endl;

return 0;

}
If you want to connect more than one statement to an if or an else you must put them in curly braces {}.
1
2
3
4
5
6
7
8
9
10
if (shazam)
  {
  hp += 10;
  cout << "Right on, dude!\n";
  }
else
  {
  hp--;
  cout << "Fooey.\n"
  }

When you write your code, make sure to use proper indentation. Most good editors today have "smart tabs". Turn them on. They're invaluable.

Hope this helps.
its still not working and whats a smart tab?




#include<iostream>
using namespace std;
int main ()

{


double minute = 60.0,
hour = 3600.0,
day = 86400.0,
value,
value1;

cout << "please enter a number of minutes:";
cin >> value;

if (hour >= value)
{
value1 = value * hour;
cout <<"the number of seconds in minute is:" << value1 <<endl;
}

else (day >= value)
{
value1 = value * day;
cout <<"the number of seconds in hour is:" << value1 <<endl;
}

else (minute >= value)
{
value1 = value * minute;
cout <<"the number of seconds in day is:" << value1 <<endl;
}

return 0;

}
ok.. let's look at the logic of what you have there..

First you're declaring the variables and setting the minutes, hours, and day variables..
your values are correct. so that looks good.

then you prompt the user and get the input. That looks good too.
I notice, you're using cin to input into a double. As far as I know that should work, but not being thoroughly read up on cin, i can't say that for sure.

Now, you check if hour is greater than or equal to value.
if it is, you set value1 to value times hour.
Then you count the number of seconds in minute is, and the value.
That doesn't look right.

if you want to change the minutes you requested to minutes.. wait.. huh?

Ok, perhaps when you prompted the user you meant to ask for the number of seconds. not minutes. Let's go with that assumption.
Then, you want to convert it to minutes. so you should check if minutes is greater than or equal to value
Then to convert it to minutes you should devide, not multiply.

Then you check, with an else (that should be else if)
check hour, and devide, then use another else if to check day and again devide.

However, if hour is greater than or equal to value then it'll always trigger and the others won't. so the others will be useless code.
To fix this, you should probably reverse it and check days first, hours next, and minutes last.

All of these, since they are doubles, will return decimal points so like 1 day and 6 hours (108000 seconds) will be 1.25 days

So if you would like to split that up into days, hours:minutes:seconds then you'd need another approach.

You would want to first check if day is greater than or equal to value, and if so
intDays (notice it's an integer) is value devided by day and value is value modulo day. (this puts the remainder into value thus subtracting what you put into intDays.

Then you would check (with another If, not an else if) if hour is greater than or equal to value, and if so
intHours is value devided by hour, and value is value modulo hour

Then same goes for minutes and this leaves value to only contain left over seconds.

When you declare the ints for intDays, intHours, and intMinutes, make sure you initialize them to 0 (zero) so that if one of the above checks fails, it remains zero.

Then cout << intDays << " days, " << intHours << " hours, " << intMinutes << " minutes, and " << value << " seconds.";

Does that make sence?
I purposefully did not write the code. I want to see if you can do it on your own. Post what you get and either I or someone will help fix any problems.

Edit: Oh yea, and a smart tab, If i'm correct, he's talking about most editors will auto-indent for you. Mine doesn't indent correctly, or at least not to my liking so I turned it off and manually indent instead.
Last edited on


: error C2146: syntax error : missing ';' before identifier 'value'

#include<iostream>
using namespace std;
int main ()

{


int minute = 60,
hour = 3600,
day = 86400,
intday,
inthour,
intminute,
value;

cout << "please enter a number of seconds:";
cin >> value;


if (day >= value)
{
(intday = value / day) value %= day;
}


if (hour >= value)
{
(inthour = value / hour) value %= hour;
}

if (minute >= value)
{
(intminute = value / hour) value %= minute;
}

cout << intday << " days, " << inthour << " hours, " << intminute << " minutes, and " << value << endl;
Last edited on
: error C2146: syntax error : missing ';' before identifier 'value'. near the if statements
Last edited on
(intday = value / day) value %= day;
Those are two commands in one line. You should use ';' between them:
(intday = value / day); value %= day;

The same for the other if's also.

Also you have wrong checks. You want to check if the value is greater or equal to day, hour, minute. You are doing the opposite.
Last edited on
bah.. t hat's my fault.. posting at 1 in the morning. yea.. should check if the value is greater than or equal to day, then hour, then minute..

You can either change (day >= value) to (day <= value) and do the same with the rest. (that's the easiest way.)

or change (day >= value) to (value >= day) and do the same with the rest.

But also, yea, two commands so use:
1
2
3
4
{
   intday = value / day;
   value %= day;
}


Oh yea, and don't forget, when you first declare the intminute, intday and inthour, initialize them to 0.

That way if the check doesn't pass, you don't get garbage printed to screen. e.g.
If given the number of second equal to 1 day, 30 minutes and 10 seconds, the hour will not pass and thus nothing get's assigned. If initialized to zero to begin with it'll print out as "1 days, 0 hours, 30 minutes, and 10 seconds" If not initialized the 0 hours might be anything.
Last edited on
thanks you guys it works!!
You did all the hard work. We just pointed you in the right direction.
I also liked that you thought to use the %= instead of value = value % etc..

For extra effort, if you wish, there are 31536000 seconds per year (that's 86400 * 365) and 2628000 per month (31536000 / 12) Obviously that month is average, since some months have 31 days and others have 30, and even one has only 28. But it's close enough to work for our purposes.

I'm tempted to try to work on one that works the other way, by taking a string like "#y, #M, #d, #h, #m, #s" and convert it all to seconds. Would require parsing the string, and use of atoi, but that isn't difficult.
Topic archived. No new replies allowed.