Skip to content

Instantly share code, notes, and snippets.

@cjaytango
Last active October 1, 2017 10:39
Show Gist options
  • Save cjaytango/b9a16d52d28392cb9494b9f8e275d7dd to your computer and use it in GitHub Desktop.
Save cjaytango/b9a16d52d28392cb9494b9f8e275d7dd to your computer and use it in GitHub Desktop.
c++ Alarm Clock program (Reddit DailyProgrammer #321)
#include <string>
#include <iostream>
using namespace std;
int tim,raw,one,ten,hou=0;
string amp;
string parsec() {
string voice;
switch (one) {
case 1: voice="eleven"; break;
case 2: voice="twelve"; break;
case 3: voice="thirteen"; break;
case 4: voice="fourteen"; break;
case 5: voice="fifteen"; break;
case 6: voice="sixteen"; break;
case 7: voice="seventeen"; break;
case 8: voice="eighteen"; break;
case 9: voice="nineteen"; break;
}
return voice;
}
string parseb(int x) {
string voice;
switch (x) {
case 0: voice="oh-"; break;
case 1: voice=parsec(); break;
case 2: voice="twenty-"; break;
case 3: voice="thirty-"; break;
case 4: voice="forty-"; break;
case 5: voice="fifty-"; break;
}
return voice;
}
string parsea(int x) {
string voice;
switch (x) {
case 0: voice="twelve "; break;
case 1: voice="one "; break;
case 2: voice="two "; break;
case 3: voice="three "; break;
case 4: voice="four "; break;
case 5: voice="five "; break;
case 6: voice="six "; break;
case 7: voice="seven "; break;
case 8: voice="eight "; break;
case 9: voice="nine "; break;
case 10: voice="ten "; break;
case 11: voice="eleven "; break;
case 12: voice="twelve "; break;
}
return voice;
}
int scrub(char par[6],int raw) {
if (raw<1000 && raw>99) {
one=par[3]-48;
ten=par[2]-48;
hou=par[1]-48;
amp="A.M.";
}
else {
int big=0;
hou=raw;
one=par[3]-48;
ten=par[2]-48;
big=ten*10;
hou=(hou-one-big)/100;
if (hou<12) amp="A.M.";
else {
amp="P.M.";
hou=hou-12;
}
}
return 0;
}
int gather() { //Takes input, parses out anything non-numeric
string temp;
int y=0;
int z=0;
char a[6],b[6];
for (int z=0;z<=6;z++) { //Nullifies holding arrays
a[z]='\n';
b[z]='\n';
}
getline (cin,temp);
strcpy(a,temp.c_str()); //Fills array a with input
for (int x=0; x<6; x++) {
if ((a[x]>47 && a[x]<58)) {
b[y]=a[x];
y++;
}
}
z=atoi(b);
cout<<b[0]<<endl<<b[1]<<endl<<b[2]<<endl<<b[3]<<endl;
cout<<z<<endl;
scrub(b,z);
return z;
}
int main() {
cout<<"What time is it?"<<endl;
tim=gather();
if (tim>2359||tim<0||one>9||ten>6) { cout<<"Invalid entry. Please try again."<<endl; return 0; }
else {
cout<<"It's "<<parsea(hou)<<parseb(ten);
if (ten!=1) cout<<parsea(one);
cout<<" "<<amp<<endl;
return 1;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment