0
#include<iostream>
using namespace std;

int computeDifference(int hours_par, int minutes_par, char isAM_par, int hours2_par, int minutes2_par, char isAM2_par);
//Precondition: Hours and minutes are input from the keyboard, hours must be in a 12- hour notation,
// between 1 and 12. Minutes should be between 0 and 59. isAM must be either AM or PM 
//Postcondition: The value of the difference in minutes between the initial time and
//the future time is returned.

int main()
{
	int hours, minutes, hours2, minutes2, difference;
	char ans, timePresent1, timePresent2, timeFuture1, timeFuture2;
	bool isAM, isAM2;

	do
	{
		cout << "Welcome to The Time Machine. We will help you go forward in time, up to 24 hours.\n";
		cout << "Please enter the initial number of hours, in a 12-hour notation: \n";
		cin >> hours;
		cout << "Now enter the initial number of minutes:\n";
		cin >> minutes;
		cout << "Now enter if the initial time given is either AM or PM:\n";
		cin >> timePresent1 >> timePresent2;
		cout << endl;
		cout << "Now enter the hours to which you want to go forward. Remember that its up to 24 hours.\n";
		cin >> hours2;
		cout << "Now enter the minutes to which you want to go forward:\n";
		cin >> minutes2;
		cout << "Now enter if the future time is either AM or PM:\n";
		cin >> timeFuture1 >> timeFuture2;
		cout << endl;

		if (hours > 12 || hours <= 0 || hours2 > 12 || hours2 <= 0)
		{
			cout << "You have entered an incorrect value in hour\n";
			break;
		}
		if (minutes > 59 || minutes < 0 || minutes2 > 59 || minutes2 < 0)
		{
			cout << "You hace entered an incorrect value in minutes\n";
			break;
		}
		if (timeFuture1 == 'a' || timeFuture1 == 'A')
		{
             isAM = true;
        }
        else if (timeFuture1 == 'p' || timeFuture1 == 'P')
        {
             isAM = false;
        }
        else
        {
            cout << "You have entered an invalid value in AM or PM\n";
        }
                         
		difference = computeDifference(hours, minutes, isAM, hours2, minutes2, isAM2);

		cout << "You were moved forward in time by " << difference << " minutes\n";
		cout << endl;
		cout << "Do you want to do it again? (y/n)\n";
		cin >> ans;
	} while (ans == 'Y' || ans == 'y');

	cout << "Have a nice day!\n";
	return 0;
}

int computeDifference(int hours_par, int minutes_par, char isAM_par, int hours2_par, int minutes2_par, char isAM2_par)
{
	int timeInMinutes = 0, timeInMinutes2 = 0, hoursInMinutes = 0, hoursInMinutes2 = 0, timeAdvanced = 0;

	if (isAM_par = true)
	{
        if (hours_par >= 1 && hours_par < 12)
        {
		hoursInMinutes = hours_par * 60;
	    }
	    else if (hours_par = 12)
	    {
             hoursInMinutes = 0;
        }
	else if (isAM_par = false)
	{
         if (hours_par > 12 && hours_par <= 11)
         {
		     hoursInMinutes = (hours_par * 60) + 720;
         }
         else if (hours_par = 12)
         {
             hoursInMinutes = hours_par * 60;
         }
    }
	timeInMinutes = hoursInMinutes + minutes_par;
	
	if (isAM2_par = true)
	{
        if (hours2_par >= 1 && hours2_par < 12)
        {
		hoursInMinutes2 = hours2_par * 60;
	    }
	    else if (hours2_par = 12)
	    {
             hoursInMinutes2 = 0;
        }
	else if (isAM2_par = false)
	{
         if (hours2_par > 12 && hours2_par <= 11)
         {
		     hoursInMinutes2 = (hours2_par * 60) + 720;
         }
         else if (hours2_par = 12)
         {
             hoursInMinutes2 = hours2_par * 60;
         }
    }
	timeInMinutes2 = hoursInMinutes2 + minutes2_par;

	if (timeInMinutes2 > timeInMinutes)
	{
		timeAdvanced = timeInMinutes2 - timeInMinutes;
	} else if (timeInMinutes > timeInMinutes2)
	{
		timeAdvanced = 1440 - (timeInMinutes - timeInMinutes2);
	} else if (timeInMinutes == timeInMinutes2)
	{
		timeAdvanced = 1440;
	}
	return (timeAdvanced);
    }
}}

The program's running fine... but when I use a present time of AM and a future time of PM and viceversa.... it outputs a wrong time... for example: present: 11:50am future: 12:10pm It says Ive moven 740 minutes into the future...

The same outputs when I enter 11:50pm and 12:10am... can anyone see where's the problem? Thanks a lot

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Lerner
0

Don't know about your logic, but this syntax is wrong:

if (isAM_par = true)

= is assignment and you want equality which is ==. This mistake is repeated many times throughout the program.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.