This is a code which i have written to take the time entry from user, validate it and then store it in a file
#include <iostream>
#include <map>
#include<string>
#include<sstream>
#include<cstdlib>
#include<fstream>
#include<ctime>
using namespace std;
class SET_TIME
{
public:
void set_time();
SET_TIME(){}
bool isValidTime(int hr, int min, int sec)
{
return (((hr >= 0) && (hr < 24)) &&
((min >= 0) && (min < 60)) &&
((sec >= 0) && (sec< 60)));
}
void sleep(long d)
{
clock_t start=clock();
while(clock() - start < d); ///loop until time's up
}
};
void SET_TIME :: set_time()
{
string in;
const char delim = ':';
char *argv[10];
stringstream ss;
char c1, c2;
int hr, min, sec;
int hrs,mins,secs,count;
if ((argv[1]=="SET_TIME"))
//if(1)
{ char *in = argv[2];
ss.str(in);
if ((ss >> hr >> c1 >> min >> c2 >> sec) &&(c1 == delim) && (c1 == c2) &&
isValidTime(hr, min, sec))
{
cout << "ok" << endl;
}
else
{
cout << "invalid time" << endl;
}
}
ofstream temp("temp.txt");
temp<<hr<<":"<<min<<":"<<sec;
remove("time.txt");
rename("temp.txt","time.txt");
while(1)
{
hrs=hr;
mins=min;
secs=sec;
while(secs<=59||mins<=59||hrs<=23)
{
secs++;
void sleep(long);
if(secs==60)
{
secs=0;
mins++;
}
if(mins==60)
{
mins=0;
hrs++;
}
if(hrs==24)
{
secs--;
break;
}
}
hrs=0;
mins=0;
}
count++;
temp.close();
}
int main(int argc, char* argv[])
{
SET_TIME st;
st.set_time();
}
but it is showing runtime error
please help me debug it.....its urgent
please use code tags. and we cant help if you dont give us the error, because typically people don't want to scour code for bugs
Perhaps has to do with line 61:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <map>
#include<string>
#include<sstream>
#include<cstdlib>
#include<fstream>
#include<ctime>
using namespace std;
class SET_TIME {
public:
void set_time();
SET_TIME() {}
bool isValidTime(int hr, int min, int sec) {
return (((hr >= 0) && (hr < 24)) &&
((min >= 0) && (min < 60)) &&
((sec >= 0) && (sec < 60)));
}
void sleep(long d) {
clock_t start = clock();
while (clock() - start < d); ///loop until time's up
}
};
void SET_TIME::set_time() {
string in;
const char delim = ':';
char *argv[10];
stringstream ss;
char c1, c2;
int hr, min, sec;
int hrs, mins, secs, count;
if ((argv[1] == "SET_TIME"))
//if(1)
{
char *in = argv[2];
ss.str(in);
if ((ss >> hr >> c1 >> min >> c2 >> sec) && (c1 == delim) && (c1 == c2) &&
isValidTime(hr, min, sec)) {
cout << "ok" << endl;
} else {
cout << "invalid time" << endl;
}
}
ofstream temp("temp.txt");
temp << hr << ":" << min << ":" << sec;
remove("time.txt");
rename("temp.txt", "time.txt");
while (1) {
hrs = hr;
mins = min;
secs = sec;
while (secs <= 59 || mins <= 59 || hrs <= 23) {
secs++;
void sleep(long);
if (secs == 60) {
secs = 0;
mins++;
}
if (mins == 60) {
mins = 0;
hrs++;
}
if (hrs == 24) {
secs--;
break;
}
}
hrs = 0;
mins = 0;
}
count++;
temp.close();
}
int main(int argc, char* argv[]) {
SET_TIME st;
st.set_time();
}
|
Line 36 should be in the form:
34 35 36
|
#include <cstring>
...
if ( strcmp(argv[1], "SET_TIME") == 0 )
|
Last edited on