This is my first question post so please be gentle. 🙂
I’m taking a C++ course with an instructor who insists on many, many restrictions (he has code that tests the code on his end so mostly only one way to do things).
Using Xcode on a Mac.
I need to accept user input in military time format “hh:mm:ss” and store hours, minutes, and seconds directly into a struct that contains those separate values.
I tested the scanf format used below earlier today (before writing more code) and now the scanf doesn’t allow entry of any characters at all. Previously it allowed entry and correctly stored in the struct. I have no idea how to accept the military time input format with std::cin. (And no I am not allowed to use any other functions built into the standard library.)
Any idea why I can’t input anything?? Thanks in advance. I must have changed something that broke this. (I have read other similar posts and flushing the input/output buffer does not help.)
#include <iostream>
using std::cout;
using std::cin;
#include "C1A7E1_MyTime.h"
//using namespace std;
MyTime *DetermineElapsedTime(const MyTime *start, const MyTime *stop);
int main()
{
// declare start/stop/result structs
MyTime start, stop, *result;
// prompt user for start and stop times in military format
cout << "Enter start time in military format: n";
scanf("%2d%*c%2d%*c%2dn", &start.hours, &start.minutes, &start.seconds);
cout << "n";
cout << "start hours is " << start.hours;
cout << "start minutes is " << start.minutes;
cout << "start seconds is " << start.seconds;
//cout << "Enter stop time in military format: ";
//scanf("%2d%*c%2d%*c%2d", &stop.hours, &stop.minutes, &stop.seconds);
//cout << "stop hours is " << stop.hours;
//cout << "stop minutes is " << stop.minutes;
//cout << "stop seconds is " << stop.seconds;
//result = DetermineElapsedTime(&start, &stop);
//printf("%2d:%2d:%2dn", result->hours, result->minutes, result->seconds);
}
C1A7E1_MyTime.h contains:
struct MyTime {int hours, minutes, seconds;};
DOSHEA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.