Home > database >  Program seems to be skipping second set of inputs and going directly to the end calculations
Program seems to be skipping second set of inputs and going directly to the end calculations

Time:10-23

after the second output statement, the program doesn't take the second set of inputs. Any idea what I could even do to fix this? Is this just a quirk of C ? Any help at all is much appreciated. :)

#include <iostream>
using namespace std;

int main()
{
    int startHours, startMinutes;
    int endHours, endMinutes;
    bool startIsAM, endIsAM;
    char amPmChar, extra;

    
    
    int computeDifference(int startH, int startM, bool startTime, int endH, int endM, bool endTime);

    int diff, hours, minutes;

    //----------------------------------------------------------------------//

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

    startIsAM = (amPmChar == 'A') || (amPmChar == 'a');

    cout << "Enter future time in the format 'HH:MM xm' where 'xm' is\n either 'am' or 'pm': ";
    cin >> endHours;
    cin >> extra;
    cin >> endMinutes;
    cin >> amPmChar;
}

CodePudding user response:

You have to read one more char, because when you read amPmChar from the input - m is still in your stream. And after that - there is an endline, so your endHours tries to initialize with that value. That's your problem. You may read more about it in this answer, it's about cin validation.

  •  Tags:  
  • c
  • Related