RE: beepclock code attempt 2

  • From: "Ken Perry" <whistler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sat, 7 Jun 2008 22:14:35 -0700

 
 
Ok Alex what are you going to run this program on?  I am thinking your not
writing this for the PC and its important I know what platform your running
it on so I can best direct your efforts.  I have ran your program and what
is happening is you have the getch commented out and it gets into your
second loop and that is where it stays spinning away happily to the next
minute. this is fine if you don't want input but judging by the description
of the program you should be able to turn on and off the beeps. and be able
to type q to quit.  There are two main ways to accomplish this and that is
one by threads and two by select.  Now depending on what platform your
writing this program for I can give you some advice.
 
If your writing this for the PC I would scrap everything your doing and
download the boost libraries where you can get the boost threads and the
boost time date functions which will make your life a dream.
 
If you don't want to get the boost classes then you will need to learn how
to use select or download a good thread library for the mingw compiler which
is what you are using under dev c++.
 
If your not writing it for the PC then let me know because that will change
what I suggest.
 
Ken
 
  _____  

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Friday, June 06, 2008 8:46 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: beepclock code attempt 2


Here is a second attempt, from a pc this time, to send the code for
beepclock.cpp. See my previous message for an explanation of the loop at the
end of the file.
 
Code:
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main () {
int end=0;
int min=0;//stores current system minutes
int min2=0;//stores minutes as well, used to make infinite loop so beeps do
not happen
//multiple times for the same minute
int hour=0;//stores current hour
bool beeps=true;//'true' is beeps enabled, 'false' is silence
string cmd;
cout<<"This program beeps once on the quarter hour, twice on the half, and
three times at quarter of the hour, all at 488 mhz. Each hour it beeps as
many times as the hour is at 244mhz. To silence beeps, type \"s\", and to
enable beeps again type \"b\". To exit, press either \"e\" or \"q\".\n";
while(end==0){//loop not supposed to ever end so monitoring of sys clock and
//beep alerts continue until prog killed
time_t now=time(NULL);//get current time in secs from 01-01-70
tm* t =localtime(&now);//convert to tm struct for getting minutes later
min=t->tm_min;//get minutes from said tm struct
min2=min;//used for loop at the end of this main loop
hour=t->tm_hour;//now get hours
if(hour>12) hour-=12;
if(hour==0)hour=12;
if(min==0&&beeps){
for(int i=0;i<hour;i++){
Beep(244, 500);
Sleep(500);
}
}
else{
if(min==15&&beeps){//15 past the hour, beep once
Beep(488, 500);
}
else if(min==30&&beeps){//half past, beep twice
Beep(488, 500);
Sleep(500);
Beep(488, 500);
}
else if(min==45&&beeps){//quarter of, beep 3 times
Beep(488, 500);
Sleep(500);
Beep(488, 500);
Sleep(500);
Beep(488, 500);
}
}
while(min==min2){
time_t now=time(NULL);//get current time in secs from 01-01-70
tm* t =localtime(&now);//convert to tm struct for getting minutes later
min2=t->tm_min;//get minutes again to see if this loop should end
//cmd=getch();//this loop is good for input
//because it is almost always running, getch gets char from kbd w/o <enter>
if(cmd=="s"){
beeps=false;
system("cls");
cout<<"Beeps disabled, type \"b\" to enable.\n";
}
else if(cmd=="b"){
beeps=true;
system("cls");
cout<<"Beeps enabled, type \"s\" to disable.\n";
}
else if(cmd=="e"||cmd=="q"){
exit(0);
}
}
//cout<<"test";//alerts exiting of subloop, for testing
}
cin.get();
return false;
}

Have a great day,
Alex

Other related posts: