### AM PM Military time

Ive been working on this for hours now, have around a million errors, was hoping for some pointers. The problem is "Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM)."

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` `````` P#include #include using namespace std; int military(int hour,int minute); int regular(int hour, int minute); int main() { string AmPm; char choice;//looks like variable "choice" needed to be outside of the do loop in order to work (kept getting an error message) do{ cout<<" Please enter an R to convert military time to regular time, or M to convert regular time to military time. "<> choice; while ((choice= 'R') || (choice = 'r') ) { int regular(int hour, int minute); } while ((choice = 'M') || (choice = 'm')) { int military(hour,minute, AmOrPm); } cout<<"Would you like to convert another time? R for regular, M for military. N to quit."<>choice; if ((choice != 'N') || (choice != 'n')||(choice !='r')||(choice !='R')||(choice !='M')||(choice !='m')) cout << "Error"; } while ((choice != 'N')||(choice != 'n')); return 0; } int military() { int minute; // I tried to declare these in the main function, and it did not work. int hour; string AmOrPm; cout<<"This converts regular time to military time "<>hour; cout<< " Now enter the number of minutes "<> minute; cout << "Now enter either am or pm"<> AmOrPm; if ((AmOrPm = "AM")||(AmOrPm = "am")) {AmOrPm = "AM"} if ((AmOrPm = "PM")||(AmOrPm = "pm")) { AmOrPm = "PM" } if (AmOrPm = "PM") { hour = hour + 12 } return hour, minute, AmOrPm; } int regular() { int minute; int hour; string AmOrPm cout<<"This converts military time to regular time"<> hour>>minute; if (hour > 12) { hour = hour -12 } cout<<"The time is "<
Hi,

You do have more bugs than an Australian road train :+)

Some of them are to do with scope, a variable must be declared in the current scope before it can be used.

= is for assignment ; == is for comparison

line 25 is a function declaration, not a function call.

missing semi colons

a function can only return one value. One can pass variables to a function by reference, that way the value will change in the outer scope.

if a function is supposed to return a value, then it must do so. Can make the function void, or return a value.

Good Luck !!
The if statements were wrong because, in a fog of overthinking all of this, I had to look up the setup of if statements and came across this if (testExpression)
{
// statements
} from a website, and it convinced me semicolons werent needed (I dont know why I did those). Here's the code somewhat fixed.

#include <iostream>
#include<string>

using namespace std;
int military(int hour,int minute);
int regular(int hour, int minute);

int main()
{

string AmPm;
char choice;//looks like variable "choice" needed to be outside of the do loop in order to work (kept getting an error message)
do{
cout<<" Please enter an R to convert military time to regular time, or M to convert regular time to military time. "<<endl;
cin >> choice;

while ((choice= 'R') || (choice = 'r') )
{
int regular(int hour, int minute);
}

while ((choice = 'M') || (choice = 'm'))
{

int military(hour,minute, AmOrPm);

}

cout<<"Would you like to convert another time? R for regular, M for military. N to quit."<<endl;

cin>>choice;
if ((choice != 'N') || (choice != 'n')||(choice !='r')||(choice !='R')||(choice !='M')||(choice !='m'))
{cout << "Error";}
} while ((choice != 'N')||(choice != 'n'));
return 0;

}

int military()
{
int minute; // I tried to declare these in the main function, and it did not work.
int hour;
string AmOrPm;
cout<<"This converts regular time to military time "<<endl;
cout << "Please enter the hour first..."<<endl;
cin >>hour;
cout<< " Now enter the number of minutes "<<endl;
cin >> minute;

cout << "Now enter either am or pm"<<endl;
cin>> AmOrPm;
if ((AmOrPm == "AM")||(AmOrPm == "am"))
{AmOrPm = "AM";}
if ((AmOrPm = "PM")||(AmOrPm = "pm"))
{
AmOrPm = "PM";
}
if (AmOrPm == "PM")
{
hour = hour + 12;
}

return hour, minute, AmOrPm;
}

int regular()
{
int minute;
int hour;
string AmOrPm
cout<<"This converts military time to regular time"<<endl;
cout<<"Enter the first two numbers, followed by the 2nd two numbers (example: 23 45)" <<endl;
cin >> hour>>minute;
if (hour > 12)
{
hour = hour -12;

}

cout<<"The time is "<<hour<< minute<<AmOrPm<<endl;
return hour, minute AmOrPm;
}

Last edited on
If I made the value a string in the military and regular functions, would I still be able to compare it , as far as "If hour > 12" for example? That has to be an integer or some other number value, not a string, right?
Last edited on

 ``12`` ``````int minute; // I tried to declare these in the main function, and it did not work. int hour;``````

Why not? You have to, in order to call the function.

 That has to be an integer or some other number value, not a string, right?

yes.

There is a lot of extra logic going on because you are attempting to account for different versions of case, as in PM or pm. Investigate the tolower or toupper functions to convert these strings to one case or the other which makes the comparisons easier.

`return hour, minute AmOrPm;`

As I said earlier, you can't do this.

`if ((choice != 'N') || (choice != 'n')||(choice !='r')||(choice !='R')||(choice !='M')||(choice !='m'))`

I have a pathological hatred for constructs like these : investigate the `switch` statement.

With your indenting do it in a consistent fashion that shows what is nested inside what. Don't put the closing brace at the end of the line. Put it in the same column as the opening brace. Your IDE should do this automatically.

Read through and implement the suggestions I made earlier, you are still missing some of those.
Thanks, dude
I urge you to re-read the assignment. In particular, this part:
 your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM).

Your current military() and regular() functions read the input, do the conversion and write the output, all in one function.

Before writing the code, you should think about these functions. Write down what each one will be called, what it's parameters and return values are, and exactly what each one will do. Then go through them and try to convince yourself that the program will work with these functions. Here's a hint: it probably won't the first time through. So things until you're pretty sure that it will work.

You can and should do all of this without writing a single line of code. At this point you're designing the code.

Now it's time to write your code. I'd start with the code that displays the time. Write that function (functions) and then create a main program that tests them out. Get this code working.

Now add code to convert between the formats. Modify your main program to call each of these with some different values. Test it until it works.

Finally, write the code that displays the menu and gets the input.

By writing the code in small steps like this, you can concentrate on one part of the code at a time. If you try to write the whole thing at once, then you'll just get lost, as you've discovered.
I'm starting to understand the true value of pseudocode. Thanks, dhayden