Ok, for my program, we have to do separate compilation of 3 files: main.cpp, time.h, and time.cpp. We can only modify the time.cpp file. The part (well, actually the first part so far) that i'm having trouble with is when the user inputs the time.
This is the format that the user inputs the time in: [hour][colon][minute][optional whitespace][indicator only the first letter 'a' or 'p' is important] I've tried a few ways to get this to input, but i can figure it out. I think if someone can help me out with that, I can pretty much do the rest of the program very easily. I've posted everything, but I don't think that it is neccessary..just the last file. thanks
time.h
Code:
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
/// !!!!!!!!!!!!!!!!!!!! *DON'T* MODIFY THIS FILE !!!!!!!!!!!!!!!!!!!! ///
/// !!!!!!!!!!!!!!!!!!!! DO *NOT* TURN IN THIS FILE !!!!!!!!!!!!!!!!!!!! ///
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
#ifndef TIME_H_GUARD
#define TIME_H_GUARD
//////////////////////////////////////////////////////////////////////////////
/// DEFINITION OF THE Time STRUCTURE ///
//////////////////////////////////////////////////////////////////////////////
struct Time {
int hour; // in 0..23 \ military clock; days begin at midnight = 00:00;
int minute; // in 0..59 / 6:53 AM = 06:53, noon = 12:00, 11:02 PM = 23:02
};
//////////////////////////////////////////////////////////////////////////////
/// PROTOTYPES OF FUNCTIONS FOR USE BY CLIENTS OF THE Time STRUCTURE ///
//////////////////////////////////////////////////////////////////////////////
Time timeMake(int h, int m, bool isPM);
// pre: h in 1..12, m in 0..59, isPM true iff PM or noon
// post: returns a valid Time structure set to the specified time
Time timeDifference(Time t1, Time t2);
// pre: t1 is a valid Time, t2 is a valid Time
// post: returns a valid Time set to as many minutes after midnight as
// there are minutes between t1 and t2 when they are considered
// as belonging to the same day; the difference in minutes is taken
// absolutely and is always considered >= 0 regardless of whether
// t1 is earlier in the day than t2 or not
void timePrint(Time t);
// pre: t is a valid Time
// post: the given time is printed to cout in the style of "7:03 AM"
// (without quotes); the final indicator is taken from "midnight",
// "AM", "noon", and "PM" (without quotes) as appropriate
bool timeRead(Time *t);
// pre: t is a valid pointer to a not-necessarily-valid Time
// post: an attempt is made to read a time from cin; if successful,
// the Time pointed to by t reflects what was read and true
// is returned; on failure, false is returned and t has been
// set to midnight; the valid input format for a time is exactly
//
// [HOUR][COLON][MINUTE][OPTIONAL-WHITESPACE][INDICATOR]
//
// where [HOUR] is either exactly one or exactly two decimal digits
// giving an integer in 1..12, [COLON] is an explicit ':' (without
// the single quotes), [MINUTE] is exactly two decimal digits giving
// an integer in 0..59, [OPTIONAL-WHITESPACE] is any sequence of zero
// or more whitespace characters, and [INDICATOR] is any non-empty
// string without whitespace of which only the first character matters:
// 'A' or 'a' indicates AM (or midnight for 12:00), 'P' or 'p' indicates
// PM (or noon in the case of 12:00), 'N' or 'n' means noon (but this
// is only allowed for 12:00), and 'M' or 'm' means midnight (only
// allowed for 12:00); whitespace separates a time from any item
// immediately before and/or after it; remark: times output by
// timePrint() are suitable for reading back in with timeRead()
//
// Valid examples: 3:00a 1:03pm 09:59ante
// 09:05P 12:00AM 12:08AAy,ix$bq\kh
// 5:09 p 4:07 Am 07:10 post_meridiem
// 02:08 A 12:43 pM 12:00 nOOnT1M3
// 12:00 midnight 12:00N 12:00 moontime
#endif
main.cpp
Code:
#include "Time.h"
// for structure Time
// and function timeMake()
// and function timeRead()
// and function timeDifference()
// and function timePrint()
#include <cstdlib>
// for macro EXIT_SUCCESS
// and macro EXIT_FAILURE
#include <iostream>
using std::cout;
using std::cerr;
int main(void)
{
Time t1, t2;
cout << "1st time? ";
if(!timeRead(&t1)) {
cerr << "Failed to read first time!\n";
return(EXIT_FAILURE);
}
cout << "2nd time? ";
if(!timeRead(&t2)) {
cerr << "Failed to read second time!\n";
return(EXIT_FAILURE);
}
Time delta = timeDifference(t1, t2);
cout << "Noon: \""; timePrint(timeMake(12, 0, true));
cout << "\", midnight: \""; timePrint(timeMake(12, 0, false));
cout << "\", L2 ends: \""; timePrint(timeMake(10, 50, false));
cout << "\",\nL3 ends: \""; timePrint(timeMake( 1, 50, true));
cout << "\"; difference of "; timePrint(t1);
cout << " and "; timePrint(t2);
cout << " is "; timePrint(delta);
cout << ".\n";
return(EXIT_SUCCESS);
}
time.cpp --> the modifiable file--it should go in the timeRead func
Code:
#include "Time.h"
#include <iostream>
#include <string>
using namespace std;
//////////////////////////////////////////////////////////////////////////////
/// PROTOTYPES OF ANY AND ALL FUNCTIONS PRIVATE TO THIS FILE ///
//////////////////////////////////////////////////////////////////////////////
// --delete this comment and replace it with the prototypes of any and all--
// -- helper functions you write; such functions are private to this file --
//////////////////////////////////////////////////////////////////////////////
/// DEFINITIONS OF FUNCTIONS PROTOTYPED IN Time.h ///
/// AND ANY & ALL PRIVATE FUNCTIONS PROTOTYPED ABOVE ///
//////////////////////////////////////////////////////////////////////////////
string time1;
Time timeMake(int h, int m, bool isPM)
{
// --delete this comment and the stub code below, then add your own code--
Time t;
t.hour = 0;
t.minute = 0;
return(t);
}
Time timeDifference(Time t1, Time t2)
{
// --delete this comment and the stub code below, then add your own code--
Time t;
t.hour = 0;
t.minute = 0;
return(t);
}
void timePrint(Time t)
{
// --delete this comment and the stub code below, then add your own code--
cout << "FIX-ME";
}
bool timeRead(Time *t)
{
return(true);
}