I have to create a program that accepts 3 user inputs (hours, minutes, seconds) and then asks the user whether they want it to show in standard or military time. The main program should make 2 calls to the function DisplayTime, whether its true if they want standard time or it will be false if they want military. I dont know what Im doing wrong or what I need to do to fix it.

Im not sure how to put the extra 0 in if the minutes are <10.
Also when I run the program it never makes it to military time even if I type in M.

Thanks for the help.

#include <iostream>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;

void GetTime (int & hours, int & minutes, int & seconds);
void DisplayTime (int hours, int minutes, int seconds, bool standardTime = true);

void main(void)
{
	char ans('S');
	int hours(0);
    int minutes(0);
    int seconds(0);

	GetTime (hours, minutes, seconds);

	cout << "\nDo you want this in standard or military time? S / M" << endl;
	cin >> ans;

	if (toupper(ans) == 'S')
		DisplayTime(hours, minutes, seconds);
	else
		DisplayTime(hours, minutes, seconds, false);

	system ("Pause");
}

void GetTime (int & hours, int & minutes, int & seconds)
{
	cout << "Enter the hour: ";
	cin >> hours;
	cout << "Enter the minute: ";
	cin >> minutes;
	cout << "Enter the second: ";
	cin >> seconds;
}

void DisplayTime(int hours, int minutes, int seconds, bool standardTime)
{
	char ans('S');
	while (ans == 'S')
		//display standard time
		cout << "The time is " << hours << ":" << minutes << ":" << seconds << endl;
	while(ans == 'M')
		//display military time
	{
		char type('A');
		cout << "Do you want AM or PM? A / P" << endl;
		cin >> type;
		if (type == 'A')//AM military time
			cout << "The time is " << hours << ":" << minutes << ":" << seconds << endl;
		else //PM military time
		{
			hours += 12;
			cout << "The time is " << hours << ":" << minutes << ":" << seconds << endl;
		}//else
	}//else
}//DisplayTime

Also when I run the program it never makes it to military time even if I type in M

void DisplayTime(int hours, int minutes, int seconds, bool standardTime)
{
char ans('S');
while (ans == 'S')
//display standard time
cout << "The time is " << hours << ":" << minutes << ":" << seconds << endl;
while(ans == 'M')
//display military time
{
char type('A');
cout << "Do you want AM or PM? A / P" << endl;
cin >> type;
if (type == 'A')//AM military time
cout << "The time is " << hours << ":" << minutes << ":" << seconds << endl;
else //PM military time
{
hours += 12;
cout << "The time is " << hours << ":" << minutes << ":" << seconds << endl;
}//else
}//else
}//DisplayTime

That's because you are declaring the variable 'S' after the user inputs the time standard. Therefore it will always run 'S'.

*You must also include the variable, 'ans', in the function.

Edit:

To solve the problem in adding a '0' before a number that is less than 10, just use a simple if statement.
ex:

if(number < 10)
    cout << "0" << number;

Edited 7 Years Ago by restrictment: n/a

This article has been dead for over six months. Start a new discussion instead.