I am having trouble displaying "next day" in the correctly in my program. The assignment states, ".The user is prompted to enter the hour, min, second of the current processing time - currTime. Your program will compute the next processing time which is 30 mins after the current processing time. Your program will display the current processing timetime currTime and the next processing time 1 nextTime1 following processing time 2 nextTime2. If the next processing time goes to the next day then
you display “Next Day” next to the next processing time (see sample output).
The sample output shows:
Current Proc Time = 23:50:10 | Next Proc Time = 0:20:10 (Next Day) | Next Proc Time 2 = 0:50:10
My problem occurs during the bold line "Next Proc Time 2 = 0:50:10," in my code it displays (Next Day) in which it should not, only for the previous line, "Next Proc Time (Next Day" should it be displayed. So my question is what should I do to alter my code under the Void TimeType::Display() function to have it display "Next Day" when the day is equal to one correctly. Also, I apologize if my question is not too specific, I am just at a loss and do not know what to do to fix this issue.
#ifndef TIME_H
#define TIME_H
class TimeType { //class definition
private:
int sec;
int min;
int hr;
int day; // variable stores the information about the day, you need to track the transition to the next day
public:
TimeType();
void SetTime(int s, int m, int h, int d);
TimeType NextProcTime(void);
void Display();
};
#endif
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "timetype.h"
using namespace std;
TimeType::TimeType()
{
sec = 0;
min = 0;
hr = 0;
}
void TimeType::SetTime(int s, int m, int h, int d)
{
sec = s;
min = m;
hr = h;
day = d;
}
TimeType TimeType::NextProcTime(void)
{
long buf = 0;
int h, m, s, d;
buf = (day * 86400 + hr * 3600 + min * 60 + sec) + (30 * 60); //calculation time in seconds needed for the allocation of time to reflect additional minutes
d = buf / 86400; // recalculation time for days hours minutes seconds
h = (buf - 86400 * d) / 3600;
m = (buf - 86400 * d - 3600 * h) / 60;
s = (buf - 86400 * d - 3600 * h - 60 * m);
TimeType T; //creating a buffer TimeType object
T.SetTime(s, m, h, d); //full a buffer TimeType object
return T; //return buffer TimeType object
}
void TimeType::Display()
{
if (day == 1)
{
printf(" %02i:%02i:%02i (next day)\t", hr, min, sec);
}
else {
printf(" %02i:%02i:%02i \t", hr, min, sec);
}
};
int main(int argc, char *argv[])
{
int hr = 0, min = 0, sec = 0;
TimeType currTime, nextTime1, nextTime2;
char t;
do
{
system("cls");
while (1) {
cout << "Please enter the current processing hour." << endl;
cin >> hr;
if (hr >= 0 && hr < 24)
break;
cout << "Invalid Input, try again." << endl << endl;
}
cout << endl;
while (1)
{
cout << "Please enter the current processing minute." << endl;
cin >> min;
if (min >= 0 && min < 60)
break;
cout << "Invalid Input, try again." << endl << endl;
}
cout << endl;
while (1)
{
cout << "Please enter the current processing second." << endl;
cin >> sec;
if (sec >= 0 && sec < 60)
break;
cout << "Invalid Input, try again." << endl << endl;
}
cout << endl;
currTime.SetTime(sec, min, hr, 0);
nextTime1 = currTime.NextProcTime();
nextTime2 = nextTime1.NextProcTime();
cout << "Current Proc Time = ";
currTime.Display();
cout << endl;
cout << "Next Proc Time 1 = ";
nextTime1.Display();
cout << endl;
cout << "Next Proc Time 2 = ";
nextTime2.Display();
cout << endl;
cout << endl << "Do you have more data to enter? (y/n)" << endl;
cin >> t;
cout << endl;
} while (t == 'y'); // cycle for program multiple using
return 0;
}
Related
I trying to develop a program that displays a 12 hour and 24 hour clock at the same time. But whenever I compile, I get a build error saying 'GetAM_PM': identifier not found. I get this error on line 26 in spite of using the same variable from my function parameter. What could be the root of this problem? Here is my code:
#include <iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
//converting it into 12 hour format
int TwelveHourFormat(int twelve_hours) {
return (twelve_hours == 0 || twelve_hours == 12) ? 12 :
twelve_hours % 12;
}
//printing the 12 hour format
void Display_12_HourFormat(int seconds, int minutes, int
twelve_hours) {
cout.fill('0');
cout << TwelveHourFormat(twelve_hours) << ":" << minutes << ":"
<< seconds << " " << GetAM_PM(twelve_hours);
}
//printing the 24 hour format
void Display_24_HourFormat(int seconds, int minutes, int
twenty_four_hours) {
cout.fill('0');
cout << twenty_four_hours << ":" << minutes << ":" << seconds;
}
void AddHour(int hour) {
hour = (hour + 1) % 24;
}
void AddMinute(int hour, int min) {
if (min == 59) {
AddHour(hour);
}
min = (min + 1) % 60;
}
void AddSecond(int min, int sec) {
if (sec == 59) {
AddMinute(min, sec);
}
sec = (sec + 1) % 60;
}
// function return AM/PM respect to hour of time
string GetAM_PM(int twelve_hours) {
return twelve_hours >= 12 ? "PM" : "AM";
}
// This method prints the menu options
void DisplayMenu() {
cout << "Chada Tech Clocks Menu" << endl;
cout << "[1] Add one hour" << endl;
cout << "[2] Add one minute" << endl;
cout << "[3] Add one second" << endl;
cout << "[4] Exit program" << endl;
}
int main()
{
int seconds, minutes, hours;
//obtains current time in seconds
time_t total_seconds = time(0);
//getting values of seconds, minutes and hours
struct tm ct;
localtime_s(&ct, &total_seconds);
seconds = ct.tm_sec;
minutes = ct.tm_min;
hours = ct.tm_hour;
// Variable declared
int option;
do
{
// DisplayMenu function is called
DisplayMenu();
cin >> option;
// If user input is 1, Clock function is called
if (option == 1) {
TwelveHourFormat(hours);
AddHour(hours);
GetAM_PM(hours);
Display_12_HourFormat(seconds, minutes, hours);
Display_24_HourFormat(seconds, minutes, hours);
}
// If the option is 2, the Clock function is called
else if (option == 2) {
AddMinute(minutes, seconds);
GetAM_PM(hours);
}
// If the option is 3, the Clock function is called
else if (option == 3) {
AddSecond(minutes, seconds);
GetAM_PM(hours);
}
// If the option is 4, exit message prints and application
stops running
else if (option == 4) {
cout << "You have exited the application.";
break;
}
else {
cout << "You have entered an invalid input." << endl;
}
} while (option != 4);
}
You need to declare GetAM_PM before you define Display_12_HourFormat. Otherwise it'll not be known by the compiler at the time it parses the Display_12_HourFormat function in which GetAM_PM is called.
Just move these lines to the beginning, before any other function:
// function return AM/PM respect to hour of time
string GetAM_PM(int twelve_hours) {
return twelve_hours >= 12 ? "PM" : "AM";
}
It is not this case, but if you end up with circular dependency, try to declare the methods in a .h or forward declare the methods in the code.
The program is supposed to repeat after the user enters incorrect data for hours, minutes, seconds.
I can't get programs to loop on Xcode.
Example:
user enters invalid data for hours, minutes, and seconds.
Program displays "invalid data", and then asks the user if they would like to repeat the program, if the user enters "y or Y", the program asks the user to enter the time again.
#include <iostream>
#include <iomanip>
using namespace std;
struct Time
{
int hours ;
int seconds;
int minutes;
};
void getTime(Time &time);
bool isTimeValid(Time &time);
void addOneSecond(Time &time);
void displayTime(Time &time);
const int MAX_HOURS = 23;
const int MAX_MINS = 59;
const int MAX_SECS = 59;
int main()
{
Time time;
getTime(time);
isTimeValid(time);
addOneSecond(time);
displayTime(time);
return 0;
}
void getTime(Time &time)
{
cout << "Enter the time in \"military time\", (24-hour format), in"
<< " the following order:\nHH:MM:SS, (Hours, Minutes, Seconds).\n\n";
cout << "Hours: ";
cin >> time.hours;
cout << "Minutes: ";
cin >> time.minutes;
cout << "Seconds: ";
cin >> time.seconds;
cout << endl << endl;
}
bool isTimeValid(Time &time)
{
bool answer = ' ';
if (((time.hours >= 0) && (time.hours <= MAX_HOURS)) &&
((time.minutes >= 0) && (time.minutes <= MAX_MINS)) &&
((time.seconds >= 0) && (time.seconds <= MAX_SECS)))
{
return true;
}
else
{
cout << "Invalid Time.\n\n";
return false;
}
cout << "Do it again? (Y/N)";
cin >> answer;
do
{
getTime(time);
isTimeValid(time);
addOneSecond(time);
displayTime(time);
} while(toupper(answer) == 'T' );
}
void addOneSecond(Time &time)
{
time.seconds++;
if (time.seconds > MAX_SECS)
{
time.seconds = 0;
time.minutes++;
}
}
void displayTime(Time &time)
{
cout.fill('0');
cout << "After adding one second, the time is " << setw(2) << time.hours
<< ":" << setw(2) << time.minutes << ":" << setw(2)
<< time.seconds << ".\n\n";
}
So I have a project that requires me to basically make a clock/watch. The default format of the code is a 24 hour format but option 5 in the menu, "toggle 24 hour format", it is required that hour formats change to a 12 hour format. It is not required to keep the previous input of the 24 hours, example: the user inputs 23:45, then presses option 5, then option 4, it shouldn't display 11:45pm. The user should go back to option 1 to input a new time and input an 'a' to choose am or 'p' to choose pm. My question is, how to write a boolean statement so when the user inputs '5' in the switch, the void functions know the format is now the 12 hour format and do a variation of the equations so they deal with 12 hour times.
The problem I see happening is the subtraction or addition functions in a 12 hour format. How would I write the 12 hour addition and subtraction statements so if the time is 12:30 and 45 minutes is added, the new time gets outputted as 1:15 and not 13:15.
I have some idea of how to do it (I'm terrible with boolean logic and functions):
bool mode
if (mode==true)
{
\\12 hour format
}
else (mode==false)
{
\\24 hour format
}
But I have no idea how to format that and make it so the option 5 changes the mode. I kind of understand how to do it in the void functions:
if mode==true
then add hours and minutes as a 12 hour format
But again, confused on how to do this. I looked at a similar problem posted about 6 days ago, but the answer didn't satisfy me, and only dealt with a single input and made it so the user has to input 24 or 12 to denote the time instead of making it so option 5 toggles to mode. It did use:
mode= !mode
which looked promising so that if option 5 is toggled, the user can input option 5 again and make the format toggle again, but again: how do I make it so option 5 toggles the mode?
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew);
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew);
void getTime(int &h, int &m, bool mode);
void rollTimeForward(int& h, int& m);
void rollTimeBack(int& h, int& m);
void printTime(int h, int m, bool mode);
void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew)
{
cout << "Enter a new time:" << endl << "Hours: ";
cin >> deltaH;
cout << "Minutes: ";
cin >> deltaM;
hNew = h + deltaH;
mNew = m + deltaM;
rollTimeForward(hNew, mNew);
cout << "The new time is: " << hNew << ":" << mNew << endl;
return;
}
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew)
{
cout << "Enter a new time:" << endl << "Hours: ";
cin >> deltaH;
cout << "Minutes: ";
cin >> deltaM;
hNew = h - deltaH;
mNew = m - deltaM;
rollTimeBack(hNew, mNew);
cout << "The new time is: " << hNew << ":" << mNew << endl;
return;
}
void getTime(int &h, int &m, bool mode)
{
cout << "Enter a time (hours, minutes): ";
cin >> h;
cin >> m;
if (h>23 || m>59 || h<0 || m<0)
{
cout << "Invalid input! Please input a value for hours below 24 and a value of minutes below 60." << endl;
}
return;
}
void rollTimeForward(int& h, int& m)
{
h = h + m / 60;
m = m % 60;
h = h % 24;
return;
}
void rollTimeBack(int& h, int& m)
{
h = h - m / 60;
m = m % 60;
return;
}
void printTime(int h, int m, bool mode)
{
cout << "The current time is: ";
cout << h << ":" << m << endl;
}
int h, m, deltaH, deltaM, hNew, mNew;
bool mode;
char time;
int main()
{
int choice;
do
{
cout << "-----MENU-----" << endl << endl << "1-Enter a time" << endl << "2-Add delta to time" << endl << "3-Subtract delta from time" << endl << "4-Display current time" << endl << "5-Toggle 24 hour mode" << endl << "6-exit" << endl;
cin >> choice;
switch (choice)
{
case 1:
getTime(h, m, mode);
continue;
case 2:
calcDeltaFutr(h, m, deltaH, deltaM, hNew, mNew);
continue;
case 3:
calcDeltaPast(h, m, deltaH, deltaM, hNew, mNew);
continue;
case 4:
printTime(h, m, mode);
continue;
case 5:
cout << "12 hour mode turned on" << endl;
continue;
case 6:
cout << "Exiting...";
break;
}
if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6)
{
cout << "Invalid input! Please try again!" << endl << endl;
}
else
{
break;
}
} while (choice != 6);
return 0;
}
Your print time function doesn't do anything with mode.
void printTime (int h, int m, bool mode)
{
if (mode) // assuming that when mode is true it means 24 hour time
{
//print time in 24 hour format
}
else
{
//print time in 12 hour format
}
}
Also your get time needs to do something similar because as of now, you can only set it by using 24 hour format.
In your switch statement for case 5, you want to switch your mode value. mode = !mode will work as long as mode already has a boolean value assigned to it.
Side note:
You only need the function prototypes if the function is called before it is defined.
To solve this problem I would use make use of your
void getTime (int &h, int &m, bool mode)
bool mode is not being used, perhaps you could say
if(bool)
{
//run 12 hour code
}
else
{
// run 24 hour code
}
and just in case 5 call this method by passing the bool you made "mode"
I hope this helps
I'm trying to figure out how to get both military and standard time to display.
A User inputs the time, then its shown in its standard form.
It's mostly there, but the standard time portion is giving me a few issues.
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time(int = 0, int = 0, int = 0);
~Time();
int hour; // valid values are 0 to 23
int minute; // valid values are 0 to 59
int second; // valid values are 0 to 59
void setTime(int, int, int); // function that checks if inputs are valid
void printUniversal(); // prints in HH:MM:SS format
void printStandard(); // prints in HH:MM:SS AM/PM format
static int count; //counter
};
#endif
#include "stdafx.h"
#include "Time.h" //header file that contains the Time class file
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int Time::count = 12;
Time::Time(int hr, int min, int sec)
{
hour = hr; minute = min; second = sec;
count++;
}
Time::~Time()
{
count--;
}
void Time::setTime(int hr, int min, int sec)
{
hour = (hr >= 0 && hr < 24) ? hr : 0; // checks if hour input is valid
minute = (min >= 0 && min < 60) ? min : 0; // checks if minute input is valid
second = (sec >= 0 && sec < 60) ? sec : 0; // checks if seconds input is valid
}
void Time :: printUniversal()
{
cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;
}
void Time::printStandard()
{
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << setfill('0') << setw(2) << minute << ":" << setw(2) << second << (hour < 12 ? " AM" : " PM");
}
//(Where I implement the functions - main.cpp)
#include "Time.h"
#include <iostream>
using namespace std;
int main()
{
int hour, minute, second;
Time t; //t is the time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
//test is also a time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
//Time *tp = new Time;
//Time *tarray = new Time[5];
cout << "Enter hour in military time ";
cin >> hour;
cout << "Enter minute ";
cin >> minute;
cout << "Enter second ";
cin >> second;
cout << "\nThe standard time is ";
t.printStandard(); //(PROBLEM!!!- I have the number 12 appearing right after AM and i can't get rid of it. )
cout << "\nThe universal time is ";
t.printUniversal();
cout << endl;
return 0;
} // end main
Update: The errors are gone for the moment, thanks to the suggestions posted.
However now when I run it, I get this....well since I can't post images yet...
I'll input 13, 45, 05 and almost like a old VCR, I can't 12:00:00 for standard or 00:00:00 for universal
No matter what I input into its all the same output.
Time t(); // This declares a function named t which takes
// no arguments and returns a Time object
Time test(); // This declares a function named test which
// takes no arguments and returns a Time object
Time t; // this is a default constructed Time object named t
Time t( 12, 7, 5 ); // this is a Time object with all
// parameters passed to the constructor
In printStandard, you have
<< sizeof(Time)
This is where the 12 is coming from. Objects of your Time class type take 12 bytes in memory.
I strongly suggest you get a book on C++, as all of this would be explained in there.
I am writing a simple program to simulate a watch. Class Date, Class Time, Class Watch. Watch is derived from Date and Time. Trying to call a date member function from Time member function. It says I cant do it without an object. I am learning and I am sure most of this is crap haha. Any help would be appriciated. Basically at the end of Time::Tick, I am trying to call Class Date member function inDay. Calling one member function from another class member function. I dont know how to do a friend function from another class. I tried to make my functions static but it leads me down a long road of errors. Any advice?
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;
class Date
{
public:
Date();
void inDay();;// increments day, tests if month increments, if true, increment
void inMonth(); // increments month, tests if year increments, if true, increment
void inYear(); // increment year
void displayWatch(); // calls daysInMonthSwith, calls inDay, Display date
int daysInMonthSwitch(); // determines the number of days in the month based on which month using a switch
//void testPrint(); //Testing purposes only
void setDay();
void setMonth();
void setYear();
private:
int day;
int month;
int year;
int daysInMonth;
};
class Time{
public:
Time();
void setTime(int, int, int ); // set hours, minutes, seconds
void printStandard(); // print time in standard time format 12h
void setHour();
void setMin();
void setSec();
void tick();
private:
int hour;
int minute;
int second;
};
class Watch: public Time, public Date {
public:
Watch();
int getDate();
void setTime(int, int, int);
int getTime();
private:
};
//class Time::
Date::Date() : // Class Constructor
day(27),
month(2),
year(2013),
daysInMonth(31)
{
} // End Constructor
void Date::setDay()
{
int tempDay;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
while (tempDay < 1 || tempDay > daysInMonth)
{
cout << "You entered a day outside the range." << endl;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
}
day = tempDay;
}
void Date::setMonth()
{
int tempMonth;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
while (tempMonth < 1 || tempMonth > 12)
{
cout << "You entered a month outside the range." << endl;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
}
month = tempMonth;
}
void Date::setYear()
{
int tempYear;
cout << "Please enter a year in the full number(Correct: 2005, Incorrect: 05): ";
cin >> tempYear;
year = tempYear;
}
void Date::displayWatch()
{
daysInMonthSwitch();
cout << "Date: " << month << "/" << day << "/" << year << endl;
//testPrint(); // Prints number of days in current month for testing only
//cout << "Month: " << month << endl; // Prints month for testing purposes
//inDay();
}
////////////// inDay function increments day and if conditions are met, increments month and year
void Date::inDay() //increments month and if day is more greater than number of days in month, call inMonth function
{
day++;
if (day > daysInMonth)
{
inMonth();
day = 1;
}
}// end function
void Date::inMonth() // increment month and if month is more greater than or equal to 12, call inYear function
{
month++;
if (month >= 12)
{
Date::inYear();
}
} // end function
void Date::inYear() // increment year
{
year++;
month = 1;
} // end function
//void Date::testPrint()
//{
// cout << "Days in month: " << daysInMonth << endl;
//} // for testing purposes only
int Date::daysInMonthSwitch() // Function contains switch that determines the number of days in the current month
{
//int month = m;
int result;
switch(month)
{
case 1:
result = 31;
break;
case 2:
result = 28;
break;
case 3:
result = 31;
break;
case 4:
result = 30;
break;
case 5:
result = 31;
break;
case 6:
result = 30;
break;
case 7:
result = 31;
break;
case 8:
result = 31;
break;
case 9:
result = 30;
break;
case 10:
result = 31;
break;
case 11:
result = 30;
break;
case 12:
result = 31;
break;
default :
cout << "Invalid Month" << endl;
}
daysInMonth = result;
return daysInMonth;
} // end daysInMonthSwitch function
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Time::Time()
{
const time_t currentTime = time(0);
const tm *localTime = localtime( ¤tTime );
setTime(localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
}
void Time::setHour()
{
int h;
cout << "Please enter the hour: ";
cin >> h;
hour = ( h >= 0 && h <= 24 ) ? h : 0;
}
void Time::setMin()
{
int m;
cout << "Please enter the minute: ";
cin >> m;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
}
void Time::setSec()
{
int s;
cout << "Please enter the second: ";
cin >> s;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
void Time::setTime( int h, int m, int s )
{ // validating time time, if incorrent, set to 0
hour = ( h >= 0 && h <= 24 ) ? h : 0;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
///void Time::tick();
void Time::printStandard()
{
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" <<
setfill('0') << setw(2) << minute << ":" << setw(2) << second
<< ( hour < 12 ? " AM" : " PM");
}
void Time::tick()
{
while (second >=0 && second <=60)
{
if (second < 59)
{
Sleep(1000);
second++;
}
else
{
Sleep(1000);
minute++;
second = 0;
if (minute >= 60)
{
hour++;
minute = 0;
if (hour >= 24)
hour = 0;
inDay();
}
}
}
};
///////////////////////////////////////////////////////////////////
Watch::Watch()
{}
////////////////////////////////////////////////////////////////// End Member Functions
int main()
{
Watch watch1;
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.setDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.inDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
return 0;
} // end main
Your class Time does not descend from Date. It can't call methods on classes that might also happen to be parents of child classes. Imagine if you instantiated a Time alone. What Date would it operate on?
A method that increments Time until it rolls over and then increments Date would need to be in Watch where it can see both.