returning a value with char
I need some help with this program that I am writing.First of all in status, how do i return a number value with the char, and why does my program crash after a while...plus on build it gives me a warning abt all paths not returning a value in get status when it does...why???
output:-
/*
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
*/
the .h file
Code:
#include<time.h>
class Microwave
{
public:
Microwave();
~Microwave();
bool start(double);
void stop(void);
char* status(void);
private:
double stat;
double seconds;
time_t starts;
time_t end;
double diff;
};
.cpp file
Code:
#include "Microwave.h"
#include<time.h>
Microwave::Microwave()
{
stat=0;
seconds=0;
}
bool Microwave::start(double set)
{
if(stat!=NULL)return false;
else
{
stat=set;
time(&starts);
return true;
}
}
void Microwave::stop(void)
{
stat=0;
diff=0;
}
char* Microwave::status(void)
{
time(&end);
if(stat==0)
{
return "Microwave not in use";
}
diff=difftime(end,starts);
if(diff<stat)
{
return "Microwave in use. <<m.stat-m.diff<< seconds left";
}
}
Microwave::~Microwave()
{
stat=0;
diff=0;
}
the main.cpp file
Code:
// Start takes the number of seconds to run.
// Start will return a true if successful, false otherwise.
// You can't start if it is already running.
// Calling stop will stop the Microwave. Calling stop when it is not running has no effect.
#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;
}