how to add military time into an integer number

Hi guys,

below is part of my code that I'm having issues with. I prompt the user to
enter a time between 600 and 2200 in military time and then i need to find out how many hours he entered any partial hour is considered as a full hour. For example, if the user enters start time 600 and end time 2130 how do I calculate the total hours ? Thanks in advanced guys.

cout << "Enter Enter a strting time " << endl;
cin >> startingtime;
cout << "Enter a end time" << endl;

while (startingtime < 600 || startingtime > 2200 )
{

cout << "Enter a valid time between 600AM and 2200PM Military time " << endl;
cin >> startingtime;

}

while (endtime < 600 || endtime > 2200)
{
cout << "Enter a valid end time 600 & 2200" << endl;
cin >> endtime;
}
// example total hours= 5;
I would think of it this way:

1
2
3
4
5
6
 // get the starting time
// get the end time
// do your awesome checking :D (make sure, of course, you aren't going to divide by zero)
// divide the end time by starting time
// output the answer
// *fin* 


In general, it's best practice to psuedocode it and then code it! :D

Have fun,

~ Hirokachi
Hirokachi (97) ,

can you please give me an example ?
Last edited on
I hope you two Alex can be friends.
@Alex A
@Hirokachi

:)
I most definitely can.

So here's an example for odd or even numbers:
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
 #include <iostream>

using namespace std;

int main () {

int oddOrEven;

// ask for number
std::cout << "Please type a number to find out if it's odd or even : ";

// get number
cin >> oddOrEven;

// if number divided by 2 remainder is 0 then print even otherwise odd
if (oddOrEven % 2 == 0) {
   cout << "The number is even.\n";
}
else
{
   cout << "The number is odd.\n";
}

return 0;
}


That is a similar problem that might give you a better feel for what I am talking about.

~Hirokachi

Edit: I think I fixed your suggestions.
Edit 2: I forgot the () after int main. XD
Last edited on
There are a few problems with your code, Hirokachi.
1.
#include "iostream"

No, iostream should not be wrapped in double-quotation signs "". We always write :
#include <iostream>

2.
std::cout << "Please type a number to find out if it's odd or even : \n";
Most likely, when you are about to input a certain something, it is more advisable that you consider removing the redundant newline character at the end.
std::cout << "Please type a number to find out if it's odd or even : ";
So you have a program with better output.

3.
1
2
std::cout << "the number is even.\n";
std::cout << "the number is odd.\n";

Maybe your code is sort of poor quality. You didn't start your sentences with a capital letter.

You can change it to :
1
2
std::cout << "The number is even.\n";
std::cout << "The number is odd.\n";


4.
It is good practice to write code without using namespace std; but for C++ beginners, repeatedly write std:: many times can be considered confusing and time consuming for them. I strongly advise that you just use using namespace std; without much thoughts to help people, and choose not to use using namespace std; in your own code if you believe that your code may gain more benefits from it.
Last edited on
I'm sorry.

In my defense, I just wrote that code on the fly and haven't wrote code in like a good 4 months.

Thanks for all those great critics, and that's how I learn and grow as a coder!

Edit: In that last post, I believe I fixed those errors.

~ Hirokachi
Last edited on
Hirokachi,

how can I convert the answer from the % so it gives me a decimal answer. For example user enters start time 605 end time 810 I get 205 but I would like to get 2.05. I try diving by 100 but that did not work.

cout << "Enter Enter a strting time " << endl;
cin >> startingtime; //605


while (startingtime < 600 || startingtime > 2200)
{

cout << "Enter a valid time between 600AM and 2200PM Military time " << endl;
cin >> startingtime;

}
cout << "Enter a end time" << endl;
cin >> endtime; //810

while (endtime < 600 || endtime > 2200)
{
cout << "Enter a valid end time 600 & 2200" << endl;
cin >> endtime;
}

int hours = (endtime % startingtime);
cout << hours << endl; //205
Well,

You would have to do something like:
10 / 3 = 3
10 % 3 = 3
so you want to show that the answer for:
10 / 3 = 3.33333

I would store:
10 / 3 into a floating point number
(10 % 3) / 10 = .3 into a floating point number
Then add 3 + .3 and get 3.3 which is an approximation of the exact answer which is 3.3333 repeating.

Please let me know if that isn't making sense.

~ Hirokachi
Last edited on
@Alex A

Ideally, you should make sure that endtime is greater than startingtime. One way would be...
1
2
3
4
5
while (endtime <= startingtime || endtime > 2200)
{
cout << "Enter a valid end time between " << startingtime <<< " & 2200" << endl;
cin >> endtime;
}


And to help out figuring the total hours..
Take 1200 and subtract startingtime IF its less than 1200. Divide by 100.
So if user types 900 as start. 1200-900, that leaves 300. 300 divided by 100 is 3. So, 3 hours.
Now take endtime and subtract 1200 from it. Again divide the result by 100.
If user types 1800 as endtime, the 1800 minus 1200 is 600. Divided by 100 is 6. Take the 6 and add it to the 3, and that gives you 9 hours total.

Of course, you'll still need to increase the time if either starting or ending times are greater than the even hour on the hour.
Last edited on
Topic archived. No new replies allowed.