Need help for one problem

Write a program in which you have the void ToMilTime function (int hour, int minutes, bool pm) to display the time in the military time format: the first two characters replicate the hour, the last two characters repress the minutes. ex 9:12 pm is 2112 in the military.
1. In the miltime.h fsier it is declared the void function ToMilTime (int hour, int minutes, bool pm)
2.In the multime.cpp file defines the void function ToMilTime (int hour, int minutes, bool pm).
-If pm is true and the time is not 12 it will increase by 12
-If pm is false and the time is 12 it becomes 0
-If the hour is less than 10, there will be a 0 in front of him
-If minutes is less than 10, there will be a 0 in front of him
What help do you need?
I don' t know how to do it.
I suggest you start with paper and pen, and scratching down a flow chart. If you need further help, post your flow chart here.
Last edited on
Use test-driven development. Come up with a bunch of examples, feed them to your function, and have it output something. Keep improving the function until the output looks correct.

(There are clear steps to your function improvement -- just follow the bullet statements)
This is not a homework site. We won't do your homework for you.

Show us what you have so far, and we will help you work through issues you are facing. Don't just post a homework problem and expect us to give you a solution.

But, let's see what your problem says:

1. In the miltime.h fsier it is declared the void function ToMilTime (int hour, int minutes, bool pm)


That's easy. This header file holds the function prototype.

miltime.h

1
2
3
4
5
6
7
// Hopefully you understand include guards.  You can also use #pragma once
#ifndef MILITIME_H
#define MILITIME_H

void ToMiltime(int hour, int minutes, bool pm);

#endif 


2.In the multime.cpp file defines the void function ToMilTime (int hour, int minutes, bool pm).


This is also pretty easy. This is the source file that contains the implementation of the function

miltime.cpp

1
2
3
4
5
6
7
8
9
10
#include  "miltime.h" // Include the header that declares the function we are defining.

void ToMilTime(int hour, int minutes, bool pm)
{
    // Put the code here that does the following:
    // -If pm is true and the time is not 12 it will increase by 12
    // -If pm is false and the time is 12 it becomes 0
    // -If the hour is less than 10, there will be a 0 in front of him
    // -If minutes is less than 10, there will be a 0 in front of him
}


Now, do your part and make an attempt to write the program.
they probably 'want' you to do a lot of string doctoring to get the answer, but you could do this in a small (24 entry) lookup table of strings. either one works fine, but I default to using tables when the data is static.
1
2
3
4
5
6
// filename: miltime.h
#pragma once

void ToMilTime(int hour, int minute, bool pm);

// end of file miltime.h 


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
// filename: miltime.cpp
#include "miltime.h"

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

void ToMilTime(int hour, int minutes, bool pm)
{
	// deal with bad arguments
	if (60 < minutes || minutes < 0 || hour < 0 || hour > 24)
	{
		cout << "Invalid time\n";
		return;
	}

	if (!pm && hour > 12) hour -= 12;
	if (pm && hour < 12) hour += 12;

	string military_time;
	if (hour < 10) military_time += '0';
	military_time += to_string(hour);
        if (minutes < 10) military_time += '0';
	military_time += to_string(minutes);

	cout << military_time << '\n';
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// filename: main.cpp
#include <iostream>
using namespace std;

#include "miltime.h"

int main()
{
    ToMilTime(14, 20, false); // prints 0220
    //.........

    cout << '\n';
    system("PAUSE"); // wait for the user to press any key
}
Last edited on
@fewdiefie, Don't just hand out full answers to homework like that.

#include <iostream> // needed to use cout in "miltime.cpp"

Don't include headers in a header file unless it's needed in the header file itself. Just include it directly in the cpp file that needs it. And never do a "using namespace std" at global scope in a header file. Imagine if the cpp file needed 5 different standard headers. Then those would all be included in main where they weren't needed at all. And "using namespace std" dumps all the identifiers into the global namespace! That's a (bad) decision that the mainline should be able to make itself, not be forced upon it because of a stupid header.
Last edited on
@tpd, first; you're right the include is in the wrong place #corrected;
second, you cannot report me for giving a complete answer to the novice since this is a c++ forum where you can ask anything about the language and expect an answer, not a school website.
Last edited on
fewdieie answers someones homework, then says:
(the website is) not a school website.


lol
lol
I think you need to learn "mount stupid".
You must learn "mount stupid 'from scratch'", You having 298 posts doesn't give you a third ball.
I have 25 posts but many years with c++ and I guess you have none and by the way, next time don't abuse posts, keep your mouth shut until you have an answer to the question instead of being rude to people helping novices...
Last edited on
fewdiefie wrote:
second, you cannot report me for giving a complete answer to the novice since this is a c++ forum where you can ask anything about the language and expect an answer, not a school website.

erm, yes they can. it is not a homework site, and the forum doesn't write full solutions for homework problems. If they ask a question, they can expect help certainly, but they shouldn't expect a complete answer.
It it less about whether or not it is reportable for posting complete answers, you could've just said you felt generous and gave an answer, but you forgot something.

keep your mouth shut until you have an answer

The answer was already made by Doug4. And you posted 2 hours later, so you should've seen it.

Also programming is not a skill you learn from answers on a cheat sheet. It is not a memorization game. There are things you learn that books don't cover because it would take too many pages, like comprehending C++ errors for example, you can't learn them if you don't do your homework.

I would much rather answer a question on how to fix an error than do all the homework questions for someone who isn't willing to give any effort (or in this case, the opportunity for that to happen is gone now because of someone).
Yeah i already got it, I just didn't like this:
I think you need to learn "mount stupid".
Topic archived. No new replies allowed.