diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0cba2e6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/Project1.sln b/Project1.sln new file mode 100644 index 0000000..7a8d745 --- /dev/null +++ b/Project1.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.33328.57 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Project1", "Project1\Project1.vcxproj", "{E794ABF6-346A-4345-9FA7-C70972942EC2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Debug|x64.ActiveCfg = Debug|x64 + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Debug|x64.Build.0 = Debug|x64 + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Debug|x86.ActiveCfg = Debug|Win32 + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Debug|x86.Build.0 = Debug|Win32 + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Release|x64.ActiveCfg = Release|x64 + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Release|x64.Build.0 = Release|x64 + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Release|x86.ActiveCfg = Release|Win32 + {E794ABF6-346A-4345-9FA7-C70972942EC2}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D81BAE95-5B37-482B-B96F-23F0A27AED08} + EndGlobalSection +EndGlobal diff --git a/Project1.zip b/Project1.zip new file mode 100644 index 0000000..a3a77e2 Binary files /dev/null and b/Project1.zip differ diff --git a/Project1/Clock.h b/Project1/Clock.h new file mode 100644 index 0000000..cd611b2 --- /dev/null +++ b/Project1/Clock.h @@ -0,0 +1,215 @@ +/* + * Clock app for Project One + * Clock.h header file + * + * Date: 2022/03/16 + * Author: Cody Cook + */ + +#pragma once // only read this file once +#include "Main.h" // grab clearScreen function +#include // use of cin, cout +#include // use of string +#include // use of string functions + +using namespace std; + +unsigned int hour; // the clock's hour +unsigned int minute; // the clock's minute +unsigned int second; // the clock's second + +string twoDigitString(unsigned int n) +{ + /// Convert an unsigned integer to a two character string equivalent. + + // the result of the conversion from int to string + string value; + + // when n is less than 10, add a leading zero + if (n < 10 && n >= 0) + { + value = "0" + to_string(n); + } + else if (n >= 10 && n < 60) + { + value = to_string(n); + } + else + { + value = "00"; + } + + // return the new string + return value; +} + +string nCharString(unsigned int n, char c) +{ + /// return the c character n times. + return string(n, c); +} + +string formatTime24(unsigned int h, unsigned int m, unsigned int s) +{ + /// return the concatenation + return twoDigitString(h) + ":" + twoDigitString(m) + ":" + twoDigitString(s); +} + +string formatTime12(unsigned int h, unsigned int m, unsigned int s) +{ + /// set A M to default and change to P M when it is appropriate + string ampm = "A M"; + if (h >= 12) + { + h -= 12; + ampm = "P M"; + } + if (h == 0) + { + h = 12; + } + + /// return the concat of thes values + return twoDigitString(h) + ":" + twoDigitString(m) + ":" + twoDigitString(s) + " " + ampm; +} + +void displayClocks(unsigned int h, unsigned int m, unsigned int s) +{ + /// display the clocks in a table + + // clear the screen + clearScreen(); + + // output the table, 12 hour on left, 24 hour on right. + cout << nCharString(27, '*') << nCharString(3, ' ') << nCharString(27, '*') << endl; + cout << "*" << nCharString(6, ' ') << "12-Hour Clock" << nCharString(6, ' ') << "*" << nCharString(3, ' '); + cout << "*" << nCharString(6, ' ') << "24-Hour Clock" << nCharString(6, ' ') << "*" << endl; + cout << endl; + cout << "*" << nCharString(6, ' ') << formatTime12(h, m, s) << nCharString(7, ' ') << "*" << nCharString(3, ' '); + cout << "*" << nCharString(8, ' ') << formatTime24(h, m, s) << nCharString(9, ' ') << "*" << endl; + cout << nCharString(27, '*') << nCharString(3, ' ') << nCharString(27, '*') << endl; +} + +unsigned int getHour() +{ + /// return the hour value + return hour; +} +void setHour(unsigned int h) +{ + /// set the hour value + hour = h; + return; +} +void addOneHour() +{ + /// add one hour to the clock; if the hour is 23, set it to 0 + if (getHour() >= 0 && getHour() <= 22) + { + setHour(getHour() + 1); + } + else if (getHour() == 23) + { + setHour(0); + } + return; +} +unsigned int getMinute() +{ + /// return the minute value + return minute; +} +void setMinute(unsigned int m) +{ + /// set the minute value + minute = m; + return; +} +void addOneMinute() +{ + /// add one minute to the clock; if the minute is 59, add one hour + if (getMinute() >= 0 && getMinute() <= 58) + { + setMinute(getMinute() + 1); + } + else if (getMinute() == 59) + { + setMinute(0); + addOneHour(); + } + return; +} + +unsigned int getSecond() +{ + /// return the second value + return second; +} + +void setSecond(unsigned int s) +{ + /// set the second value + second = s; + return; +} + +void addOneSecond() +{ + /// add one second to the clock; if the second is 59, add one minute + if (getSecond() >= 0 && getSecond() <= 58) + { + setSecond(getSecond() + 1); + } + else if (getSecond() == 59) + { + setSecond(0); + addOneMinute(); + } + return; +} + +void setTime() +{ + /// Allow the user to set the time. Required 24-hour time format. + + int userHour, userMinute, userSecond = -1; // use regular integers to handle exception + bool validTime = false; // flag to determine if the time is valid + + while (!validTime) + { + // ask user for the current time; then parse and configure. + cout << "Please enter the current time in 24-hour format (HH:MM:SS): "; + + // set the hour and ignore the : + cin >> userHour; + cin.ignore(); + + // set the minutes and ignore the : + cin >> userMinute; + cin.ignore(); + + // set the seconds + cin >> userSecond; + cin.ignore(); + + // validate the input + if ((userHour >= 0 && userHour <= 23) && (userMinute >= 0 && userMinute <= 59) && (userSecond >= 0 && userSecond <= 59)) + { + validTime = true; + } + else + { + clearCin(); + userHour, userMinute, userSecond = -1; + cout << "Invalid time entered. Please try again." << endl; + } + } + + setHour(userHour); + setMinute(userMinute); + setSecond(userSecond); + + // now that time is set, clear the screen and show the clocks + clearScreen(); + displayClocks(hour, minute, second); +} \ No newline at end of file diff --git a/Project1/Main.h b/Project1/Main.h new file mode 100644 index 0000000..117b5dc --- /dev/null +++ b/Project1/Main.h @@ -0,0 +1,31 @@ +/* + * Clock app for Project One + * Main.h header file + * + * Date: 2022/03/16 + * Author: Cody Cook + */ + +#pragma once + +#include + +using namespace std; + +void clearScreen() +{ + // Clear the screen + system("CLS"); +} + +void clearCin() +{ + /* When the cin potentially contains bad data; + * like alphabeticals when looking for unsigned integers. + * Doubles as a pause function, surprisingly, if necessary. + */ + + // clear the cin and also ignore the rest of the line + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); +} \ No newline at end of file diff --git a/Project1/Menu.h b/Project1/Menu.h new file mode 100644 index 0000000..39ac2dc --- /dev/null +++ b/Project1/Menu.h @@ -0,0 +1,100 @@ +/* + * Clock app for Project One + * Menu.h header file + * + * Date: 2022/03/16 + * Author: Cody Cook + */ + +#pragma once // only read this file once +#include "Main.h" // get clearScreen function +#include "Clock.h" // use of clock functions +#include // use of cin, cout +#include // use of string +#include // use of string functions + +string menuCommand = "Don't Exit"; +; + +const char *menuItems[] = {"Add One Hour", "Add One Minute", "Add One Second", "Exit Program"}; + +void printMenu(const char *strings[], unsigned int numStrings, unsigned char width) +{ + + // print width of *, followed by a new line; top of table + cout << nCharString(width, '*') << endl; + + // for each of the strings + for (unsigned int i = 0; i < numStrings; i++) + { + + // set this string to s + string s = strings[i]; + + // create a menu option + cout << "* " << i + 1 << " - " << s << nCharString(width - s.length() - 7, ' ') << "*" << endl; + + // after every line, except the last line, add a * one each side of a line of spaces + if (i < numStrings - 1) + { + cout << endl; + } + } + // print width of *, followed by a new line; bottom of table + cout << nCharString(width, '*') << endl; +} + +unsigned int getMenuChoice(unsigned int maxChoice) +{ + // variable for user's selection in menu + unsigned int choice; + + // prompt user for option + cin >> choice; + + // if the user puts a bad value, make them enter it again + while (choice < 1 || choice > maxChoice) + { + cout << "Invalid choice, please try again: "; + clearCin(); + cin >> choice; + } + + // return the valid choice option + return choice; +} + +void mainMenu() +{ + // variable for user's selection in menu + unsigned int menuChoice = getMenuChoice(4); + + // options 1-3 control the clock + // option 4 exits the program + switch (menuChoice) + { + case 1: + // add one hour to the clock, then show the clock + addOneHour(); + displayClocks(hour, minute, second); + break; + case 2: + // add one minute to the clock, then show the clock + addOneMinute(); + displayClocks(hour, minute, second); + break; + case 3: + // add one second to the clock, then show the clock + addOneSecond(); + displayClocks(hour, minute, second); + break; + case 4: + // exit the program + menuCommand = "exit"; + break; + default: + // if the user puts a bad value, make them enter it again + cout << "Invalid choice, please try again." << endl; + break; + } +} diff --git a/Project1/Project1.rc b/Project1/Project1.rc new file mode 100644 index 0000000..4974f48 --- /dev/null +++ b/Project1/Project1.rc @@ -0,0 +1,100 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "winres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (United States) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""winres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,0,0,1 + PRODUCTVERSION 1,0,0,1 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x40004L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "CompanyName", "Cody Cook @ SNHU" + VALUE "FileDescription", "My Project1 Assignment: Clock simulation" + VALUE "FileVersion", "1.0.0.1" + VALUE "InternalName", "Project1.exe" + VALUE "LegalCopyright", "Copyright (C) 2023 Cody Cook" + VALUE "OriginalFilename", "Project1.exe" + VALUE "ProductName", "Project 1: Clock Simulation" + VALUE "ProductVersion", "1.0.0.1" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + +#endif // English (United States) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/Project1/Project1.vcxproj b/Project1/Project1.vcxproj new file mode 100644 index 0000000..4c0d976 --- /dev/null +++ b/Project1/Project1.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {e794abf6-346a-4345-9fa7-c70972942ec2} + Project1 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + true + + + + + + \ No newline at end of file diff --git a/Project1/Project1.vcxproj.filters b/Project1/Project1.vcxproj.filters new file mode 100644 index 0000000..82c7636 --- /dev/null +++ b/Project1/Project1.vcxproj.filters @@ -0,0 +1,41 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/Project1/main.cpp b/Project1/main.cpp new file mode 100644 index 0000000..abc3f67 --- /dev/null +++ b/Project1/main.cpp @@ -0,0 +1,23 @@ +/* + * Clock app for Project One + * main.cpp + * + * Date: 2022/03/16 + * Author: Cody Cook + */ + +#include "Clock.h" +#include "Menu.h" + +void main() +{ + // call for initial setting of time + setTime(); + + // while the user hasn't exited, show the menu and give menu options + while (menuCommand != "exit") + { + printMenu(menuItems, 4, 26); + mainMenu(); + } +} \ No newline at end of file diff --git a/Project1/resource.h b/Project1/resource.h new file mode 100644 index 0000000..4394566 --- /dev/null +++ b/Project1/resource.h @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by Project1.rc + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif