some more tweaking i got it working, except for the last few lines, why does it spit out negative numbers at the end, and not look like the output requested.
main.cpp, the first few lines are the output, my output is at the end.
Code:
/*
Started Microwave, response is success
Microwave in use. 90 seconds left
Microwave not in use
Started Microwave, response is success
Microwave in use. 20 seconds left
Microwave not in use
Started Microwave, response is success
Microwave in use. 40 seconds left
Started Microwave, response is failure
*/
#include <iostream>
#include <windows.h>
#include "Microwave.h"
using namespace std;
void printStatus(Microwave& m)
{
cout << m.status() << endl;
}
inline const char* printBool(bool result)
{
return result ? "success" : "failure";
}
void main()
{
Microwave aMicrowave;
bool result;
result = aMicrowave.start(90);
cout << "Started Microwave, response is " << printBool(result) << endl;
printStatus(aMicrowave);
Sleep(10000);
aMicrowave.stop();
printStatus(aMicrowave);
result = aMicrowave.start(30);
cout << "Started Microwave, response is " << printBool(result) << endl;
Sleep(10000);
printStatus(aMicrowave);
Sleep(21000);
printStatus(aMicrowave);
result = aMicrowave.start(40);
cout << "Started Microwave, response is " << printBool(result) << endl;
printStatus(aMicrowave);
Sleep(10000);
result = aMicrowave.start(10);
cout << "Started Microwave, response is " << printBool(result) << endl;
}
.h file
Code:
#include<time.h>
class Microwave
{
public:
Microwave();
~Microwave();
bool start(double);
void stop(void);
const char* status(void);
private:
double seconds;
time_t begin;
time_t end;
double diff;
};
.cpp file
Code:
#include "Microwave.h"
#include<time.h>
#include<stdio.h>
#include<string.h>
Microwave::Microwave()
{
seconds=0;
diff=0;
}
Microwave::~Microwave()
{
seconds=0;
diff=0;
}
bool Microwave::start(double times)
{
if(seconds!=0)
return false;
seconds=0;
seconds=times;
time(&begin);
return true;
}
void Microwave::stop(void)
{
seconds=0;
diff=0;
}
const char* Microwave::status(void)
{
time(&end);
if(seconds==0)
return "Microwave is not in use";
if(seconds==difftime(end,begin))
{
seconds=0;
return "Microwave is not in use";
}
static char temp[80];
diff=difftime(end,begin);
sprintf(temp,"Microwave in use. %.0f seconds left",seconds-diff);
return temp;
}
my output:-
Started Microwave, response is success
Microwave in use. 90 seconds left
Microwave is not in use
Started Microwave, response is success
Microwave in use. 20 seconds left //correct uptil here.
Microwave in use. -1 seconds left
Started Microwave, response is failure
Microwave in use. -1 seconds left
Started Microwave, response is failure