0

First, let me get this out of the way. I know my program is a little rough right now, but I can't even get to fixing it if lines 42-52 (future time input) are being skipped. So, for context, I need to make a time machine that can go forward 24 hours, and I need to make sure that the input for the start and future time is in the HH:MM xm format. Then I need to have the total minute difference, which can convert to the hours and minutes until the future time. Sorry if what I've done gives you a headache still learning.

Here is my code:

#include <iostream>

// Total number of minutes in an hour and day
const int MINUTES_IN_HOUR = 60;
const int MINUTES_IN_DAY = 24 * MINUTES_IN_HOUR;

// Function that computes the difference between two times
int convertDifference(int startHoursPar, int startMinutsPar, bool startIsAMPar, int endHoursPar, int endMinutesPar, bool endIsAMPar);

int main()
{
    using namespace std;

    int startHours;
    int startMinutes;
    bool startIsAM;
    int endHours;
    int endMinutes;
    bool endIsAM;
    char amPmChar;
    char extra; //IMPORTANT!!
    char answer;
    int diff;


    do
    {
        cout << "Enter start time, in the format 'HH:MM xm', where xm is \n"  << "either 'am' or 'pm' for AM or PM: ";
        cin >> startHours >> extra >> startMinutes >> startIsAM;

        if (startIsAM == 'a' || startIsAM == 'A')
        {
             startIsAM = true;
        }
        else
        {
            startIsAM = false;
        }

        cout << "\nEnter future time, in the format 'HH:MM xm', where xm is\n" << "either 'am' or 'pm' for AM or PM: ";
        cin >> endHours >> extra >> endMinutes >> endIsAM;

        if (endIsAM == 'a' || endIsAM == 'A')
        {
            endIsAM = true;
        }
        else
        {
            endIsAM = false;
        }

        diff = convertDifference(startHours, startMinutes, startIsAM, endHours, endMinutes, endIsAM);

        /*if ((hours > 1) && (minutes > 1))
            {
                cout << "There are " << diff << " minuets " << "(" << hours << " hours and " << minutes << " minutes) between " << startHours << ":" << startMinutes << " and " << endHours << ":" << endMinutes << ".";
            }
            else
            {
                cout << "There are " << diff << " minuets " << "(" << hours << " hour and " << minutes << " minute) between " << startHours << ":" << startMinutes << " and " << endHours << ":" << endMinutes << ".";
            }
            */
        cout << endl << "\nRun again? (y / n): ";
        cin >> answer;

    } while (answer == 'y');
    
    
    
    
    return 0;
}

int convertDifference(int startHoursPar, int startMinutsPar, bool startIsAMPar, int endHoursPar, int endMinutesPar, bool endIsAMPar)
{
    int diff, hours, minutes, minutes2;

    if (startIsAMPar == 'p')
    {
        if (startHoursPar >= 1 && startHoursPar < 12)
        {
            startHoursPar += 12; 
        }
    }
    if (endIsAMPar == 'p')

    {
        if ((startHoursPar >= 1) && (endHoursPar < 12))
        {

            endHoursPar += 12;

        }
    }
    
    minutes = (startHoursPar * 60) + startMinutsPar;
    
    minutes2 = (endHoursPar * 60) + endMinutesPar;
    

    if ((startHoursPar >= endHoursPar) || ((startHoursPar == endHoursPar) && (startMinutsPar > endMinutesPar)))

    {
        endMinutesPar += MINUTES_IN_DAY;
    }

    diff = endMinutesPar - startMinutsPar;

    if (diff > MINUTES_IN_DAY)

    {
        diff -= MINUTES_IN_DAY;
    }
    
    return diff;
}
appl3bees
  • 1
  • 1
  • 1
    you are reading `am/pm` into the `bool` `startIsAM` which fails and puts `cin` into a failed state resulting in all further inputs to also fail until `cin.clear` is called – Alan Birtles Oct 24 '21 at 19:04
  • @AlanBirtles I hate to ask this, but how do I use cin.clear? Its probably straightforward, but I've never seen cin.clear, sorry. – appl3bees Oct 24 '21 at 21:20
  • https://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – Alan Birtles Oct 25 '21 at 06:09
  • You could add code below before the second cin: `cin.clear(); cin.ignore(10000, '\n');` – Minxin Yu - MSFT Oct 25 '21 at 09:16

0 Answers0