I've been given a basic form of programming to do. And I simply cannot figure it out. I know this topic has been covered several times before. But each of them going around the one crucial step that I need to complete my programming. My instructor insists that the 24 hour clock is given in HH:MM time and not as two separate factors. If you want me to post the bits of coding I have in here I can, but it's not much and probably will not contribute much. I'm thankful for any bits of help I can get. If you need anymore information just ask I'm not quite sure what all you need to be able to help me with this.

Member Avatar for Mouche

Yes, please post your code (and use CODE tags) to show us what you've done. Then ask a specific question about it. If you are getting a compiler error, say what it is.

#include <iostream>
using namespace std;

const int threshold = 12;

void input(int& milit_hours, int& milit_min);
//Gets the time from the user

int calc(int& change_hours);
//changes from 24hour to standard by subtraction

void output(int out_hours, int out_min);
//Return the new value in standard time including AM or PM

int main ( )
{
    int prim_hour, prim_min;
    
    input(prim_hour, prim_min);
    calc(prim_hour);
    output(prim_hour, prim_min);
    system ("pause");
    return 0;
    
}

void input(int& milit_hours, int& milit_min)
{
     using namespace std;
     cout << "Enter the time in HH:MM ";
     cin >> milit_hours;
     cin >> milit_min;
}

int calc(int& change_hours)
{
    using namespace std;
    if (change_hours <= 12)
       return (change_hours);
    else
        return (change_hours - 12);
}

void output(int out_hours, int out_min)
{ 
     using namespace std;
     cout << "New time\n";
     cout << out_hours;
     cout << ":";
     cout << out_min;
}

Please note this code is most likely terrible. It's very rough. And before anyone insults me I know I probably seem like an idiot and in programming I pretty much am.
for whatever reason I can't get it to subtract like I want it to. Or really do what I want it to at all. I've tried for a good 19 hours straight now, and I'm tired, and desperate. Thank-you again to anyone willing to help me.

Please note this code is most likely terrible. It's very rough. And before anyone insults me I know I probably seem like an idiot and in programming I pretty much am.

The only one insulting you is you. You code is nicely modular. Each function is defined to do one job. It's formatted. All in all, good code.

The only problems are:
1) using namespace std; only needs to be at the top of the program
2) using system("pause"); -- there are better ways
3) Not accepting the return value crom calc() -- you returned a value but never put it anywhere (this is the fix)

Oh, yeah, and CODE Tags...

The system pause was really part of my rough coding, I've redone the program so many times by the time I finally asked for help that the proper ways were just not what I wanted to go through at the time.

I fixed most everything I am still having one strange problem the minutes are coming up as only 2 no matter what value I put in for it.

Please post the code you have now.

#include <iostream>
using namespace std;

const int threshold = 12;

void input(int& milit_hours, int& milit_min);
//Gets the time from the user

int calc(int& change_hours);
//changes from 24hour to standard by subtraction

void output(int out_hours, int out_min, int milit_hour);
//Return the new value in standard time including AM or PM

int main ( )
{
    char ans;
do


 {
    int new_value;
    
    int prim_hour, prim_min;   
    input(prim_hour, prim_min);
    new_value= calc(prim_hour);
    output(new_value, prim_min, prim_hour);
    cout << "\n";
    cout << "repeat?\n ";
    cin >> ans;
    
    }
while (ans == 'y' || ans == 'Y');   
system ("pause");
return 0;
} 

void input(int& milit_hours, int& milit_min)
{
     cout << "This program does not error check input from the user.\n";
     cout << "  Assumes user will input an hour from 0 to 23 and a minute";
     cout << " from 0 to 59. \n";
     cout << "Enter 24 hour time in the format of HH:MM \n";
     cin >> milit_hours;
     cin >> milit_min;
}

int calc(int& change_hours)
{   
    if (change_hours <= 12)
       return (change_hours);
    else
        return (change_hours - 12);
}

void output(int out_hours, int out_min,int milit_hours)
{ 
     char apres;
     cout << "Time in 12 hour format: \n ";
     cout << out_hours;
     cout << ":";
     cout << out_min;

}

One more thing I'd like to add is that I'm not quite sure where to put my repeat function. I've tried it in several locations and it doesn't seem to work anywhere.

What happened to your formatting? You broke it...

The system pause was really part of my rough coding, I've redone the program so many times by the time I finally asked for help that the proper ways were just not what I wanted to go through at the time.

Excuses, excuses... :icon_wink:

I fixed most everything I am still having one strange problem the minutes are coming up as only 2 no matter what value I put in for it.

Not so strange. What variable gives you the 2? And where did you load a value into it?

One more thing I'd like to add is that I'm not quite sure where to put my repeat function. I've tried it in several locations and it doesn't seem to work anywhere.

What repeat function?

I ended up having the input for the hours and minutes be seperate, not really what was required but it was the only thing that would fix the minutes error.

I need a repeat function so anyone who uses the program can repeat it as many times as they want. But I can't seem to put it somewhere so it works...It's not there because I haven't found an appropriate solution yet.

and I messed up my formating? :( I didn't even notice..

I ended up having the input for the hours and minutes be seperate, not really what was required but it was the only thing that would fix the minutes error.

So I take it you found out why you got a 2 and fixed it. I'm assuming so because you didn't respond directly to my questions.

I need a repeat function so anyone who uses the program can repeat it as many times as they want. But I can't seem to put it somewhere so it works...It's not there because I haven't found an appropriate solution yet.

Well, it looks like you tried, not with a function but with a do-while, which is more appropriate than a function....

and I messed up my formating? :( I didn't even notice..

.... and the do-while is where you messed up your formatting.

AH...see...the Do while does not actually work...I should remove it because the function never asks for me to input y to continue. It's currently the only issue I'm having now and yes I fixed the minute problem by having them be input separate rather than HH:MM other wise it wouldn't work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.