Reputation: 1
I am trying to make an adjustable clock (where you have to set the time manually). I need to add a function that if the user has not pressed a button after 1 second, then the seconds variable updates and the clock prints. Then I need the screen to clear and start all over. I am using Eclipse on macOS so kbhit() and system(“CLS”) do not work. Any help would be greatly appreciated. Here is what I have so far:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <limits>
void MenuDisplay();
void Clock12(int hrs, int mins, int secs);
void Clock24(int hrs, int mins, int secs);
void Clocks(int hrs, int mins, int secs);
void Wait();//FIXME
int main() {
//Declare variables
int hours = 23;
int minutes = 58;
int seconds = 50;
int userOption = 0;
string quit;
bool counting = true;
cout << string(100, '\n');
//Loop to gather and use input from user
while (userOption != 4) {
userOption = 0;
//waits for user to press enter
//Wait();
//function to display the menu options
MenuDisplay();
cout << " Please pick an option (1-4): ";
cin >> userOption;
switch (userOption) {
case 1:
++hours;
if (hours > 23) {//resets hours in case > 24
hours = 0;
}
counting = false;
break;
case 2:
++minutes;
break;
case 3:
++seconds;
counting = false;
break;
case 4:
quit = "The clocks are set. Goodbye.\n";//For readability
counting = false;
break;
default:
cout << "Please enter a valid option (1-4)." <<endl;
counting = false;
break;
}
if (seconds > 59) {
seconds = 0;
++minutes;
}
if (minutes > 59) {
minutes = 0;
++hours;
}
//FIXME: This is the best I could do.
//I am not sure why, but all the
//other functions for clearing a screen were not working.
//I tried at least 6 other methods. None worked.
cout << string (100, '\n');
//Starts clock
counting = true;
cout << quit;
Sleep(1);
cout << string(100,'\n');
ClockRunning(hours, minutes, seconds, counting);
void Clocks(int hrs, int mins, int secs) {// Prints Clocks
//set border size
int hours = hrs;
int minutes = mins;
int seconds = secs;
cout << "************************** **************************"
<< endl;
cout << "* 12-Hour Clock *"
<< " * 24-Hour Clock *" << endl;
Clock12(hours, minutes, seconds);
Clock24(hours, minutes, seconds);
cout << "**************************
**************************" << endl;
}
void MenuDisplay() {// Menu Options
cout << " " << "**************************" << endl;
cout << " " << "* 1-Add One Hour *" << endl;
cout << " " << "* 2-Add One Minute *" << endl;
cout << " " << "* 3-Add One Second *" << endl;
cout << " " << "* 4-Exit *" << endl;
cout << " " << "**************************" << endl;
}
void Clock12(int hrs, int mins, int secs) {// 12-hour clock
int seconds = secs;
int minutes = mins;
int hours = hrs;
bool isAM;
string PM;
if (hours < 12) {
isAM = true;
}
if (hours > 12) {
hours -= 12;
isAM = false;
}
if (hours > 12) {
hours = 0;
}
if (hours == 0) {
hours += 12;
}
if (isAM) {
PM = "AM";
}
else if (!isAM) {
PM = "PM";
}
cout << "* " << setw(2) << setfill('0') << hours << ":"
<< setw(2) << setfill('0') << minutes << ":"
<< setw(2) << setfill('0') << seconds << " " // <<endl;
<< PM << " *";
}
void Clock24(int hrs, int mins, int secs) {// 24-Hour Clock
int hours = hrs;
int minutes = mins;
int seconds = secs;
if (seconds > 59) {
seconds = 0;
++minutes;
}
if (hours > 23) {
hours = 0;
}
cout << " * " <<
setw(2) << setfill('0') << hours << ":"
<< setw(2) << setfill('0') << minutes << ":"
<< setw(2) << setfill('0') << seconds << " *" << endl;
}
void Wait() {// Waits for user to press enter
std::cout << "Press ENTER to display the menu.\n";
std::cin.ignore( std::numeric_limits
<std::streamsize> ::max(), '\n' );
}
int Sleep(float seconds = 1) {// Sleeps for 1 second
clock_t startClock = clock();
float secondsAhead = seconds * CLOCKS_PER_SEC;
while (clock() < (startClock + secondsAhead) ){}
return 1;
}
void ClockRunning(int hrs, int mins, int secs, int stop) {
int hours = hrs;
int minutes = mins;
int seconds = secs;
bool counting = true;
int stopped = stop;
while (counting) {//FIXME:Escape the loop
++seconds;
if (seconds > 59) {
seconds = 0;
++minutes;
}
if (minutes > 59) {
minutes = 0;
++hours;
}
Clocks(hours, minutes, seconds);
Sleep(1);
cout << string (100, '\n');
if (stopped == 0) {
counting = false;
}
}
}
Flowchart of the Code:
Upvotes: 0
Views: 199
Reputation: 7156
On Mac and Linux systems you can clear the console screen using:
system("clear");
"CLS"
is just for Windows.
Upvotes: 1