1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
/*************
Program: Asg_13.cpp
Author: Alan P. Matie
Date: 29 Apr 2009
Description:
Savitch, Pg 297, #1 -- Time Conversion
Write a program that converts from 24-hour notation to 12-hour notation.
For example, it should convert 14:25 to 2:25 PM. The input is given as two intergers.
There should be at least three functions, one for input, one to do the conversion,
and one for output.
Record the AM/PM information as a value of type char, 'A' for AM and 'P' for PM.
Thus, the function for doing the conversions will have a call-by-reference formal
parameter of type char to record whether it is AM of PM. (The function will have
other parameters as well.) Include a loop that lets the user repeat this
computation for new input values again and again until the user says he or she
wants to end the program.
New Concepts:
Challenges:
Add a loop, and it contains only functions!
Functions must be of proper type and pass by value, or reference as appropriate.
There must be functions for all tasks (input, processes, and output) or the program
will earn a grade of zero.
Last Modified: 01 May 2009
**************/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
// Declare constants
// Function Prototypes
void Welcome();
void in_time(int& hours, int& minutes);
void repeat(char& ans);
int time_convert(int& hours, int& minutes);
void printTime(int hours, int minutes, char display);
int main()
{
// Declare variables below here
int hours = 0, minutes, _24hr_time;
char ans, display;
// Initialization Section for real number output. DO NOT MOVE!
cout <<setiosflags(ios::fixed | ios::showpoint);
cout <<setprecision(2);
// Begin your "main processing" below here
Welcome();
do
{
in_time(_24hr_time, minutes);
time_convert(_24hr_time, minutes);
printTime(_24hr_time, minutes, display);
repeat(ans);
}while (toupper(ans) == 'Y');
return 0;
}
// function definitions below here
void Welcome()
{
cout << "Welcome to your 24 hour to 12 hour time conversion program!"<<endl<<endl;
}
void in_time(int& hours, int& minutes)
{
cout<< "Please enter the hour portion of the time to be converted ==> ";
cin >> hours;
cout<< "Please enter the minutes portion of the time to be converted ==> ";
cin >> minutes;
}
int time_convert (int& hours, int& minutes)
{
if (hours > 12)
{
hours = hours - 12;
display = 'P';
}
else
{
display = 'A';
}
return 0;
}
void printTime(int hours, int minutes,char display)
{
cout<< "The time converted to a 12 hour format is: "<<hours<<":"<<minutes<<""<<display<<"M"<<endl;
}
void repeat(char& ans)
{
cout << "Would you like to run another conversion? Y or N ?"<<endl;
cin >> ans;
}
| |