#include<stdio.h>
#include<conio.h>
#include<string>
#include<stdlib.h>
#include <iostream>
#include <cstdlib>
#include "convert.h"
using namespace std;
int check(string);
int checkNo(string);
int checkMinSec(string);
string send(string);
int getTime(int);
string hr_str,final_time;
int main()
{
int ch,result=2;
int i=1,x,y,z,p,q,n;
char tt[2],dd[5],ll[9],mm[5],nn[3];
char xa[3];
char choice;
printf("\nEnter the Time in 24 hr Format");
printf("\nE.g. 10:05:03 It shoud be in hh:mm:ss");
getTime(1);
}
int getTime(int a)
{
int result=2;
string t_time,t_time1,t_time2,colon=":";
char h[5],m[5],s[5];
while(result !=1)
{
printf("\nHour -> ");
scanf("%s",&h);
result= check(h);
}
// cout<<"\nMain Fun"<<hr_str;
t_time = hr_str;
// cout<<"\nIn t_time"<<t_time;
result=2;
while(result !=1)
{
printf("\nMinute -> ");
scanf("%s",&m);
result= checkMinSec(m);
}
t_time1 = hr_str;
//cout<<"\nIn t_time"<<t_time1;
result=2;
while(result !=1)
{
printf("\nSecond -> ");
scanf("%s",&s);
result= checkMinSec(s);
}
t_time2 = hr_str;
//cout<<"\nIn t_time"<<t_time2;
string asd=t_time+colon+t_time1+colon+t_time2;
cout<<"\nYour Time->"<<asd<<"\n";
send(asd);
}
int check(string str)
{
char *end_ptr;
long long_var;
int int_var,i=0;
// char buff[2]=" ";
char buff[str.size()];
str.copy(buff,str.size(),0);
long_var = strtol(buff, &end_ptr, 0);
if (ERANGE == errno)
{
puts("number out of range\n");
}
else if (long_var > INT_MAX)
{
printf("%ld too large!\n", long_var);
}
else if (long_var < INT_MIN)
{
printf("%ld too small!\n", long_var);
}
else if (end_ptr == buff)
{
printf("not valid numeric input\n");
return 2;
}
#if 0
else if ('\0' != *end_ptr)
{
printf("extra characters on input line\n");
}
#endif
else
{
int_var = (int)long_var;
// printf("The number %d is OK!\n", int_var);
if( int_var>=0 && int_var<=9)
{
std::string str = "0" + stringify( int_var);
// printf("\n1 digit");
cout<<"\n"<<str;
send(str);
}
else if( int_var>9 && int_var<24)
{
// printf("\n2 digit");
std::string str = stringify( int_var);
// cout<<"\n"<<str;
send(str);
}
else if(int_var>23)
{
printf("\nEnter correct value");
return 2;
}
return 1;
}
}
string send(string aaa)
{
hr_str=aaa;
//cout<<"\nin Fun"<<hr_str;
}
int checkMinSec(string str)
{
char *end_ptr;
long long_var;
int int_var,i=0;
// char buff[2]=" ";
char buff[str.size()];
str.copy(buff,str.size(),0);
long_var = strtol(buff, &end_ptr, 0);
if (ERANGE == errno)
{
puts("number out of range\n");
}
else if (long_var > INT_MAX)
{
printf("%ld too large!\n", long_var);
}
else if (long_var < INT_MIN)
{
printf("%ld too small!\n", long_var);
}
else if (end_ptr == buff)
{
printf("not valid numeric input\n");
return 2;
}
#if 0
else if ('\0' != *end_ptr)
{
printf("extra characters on input line\n");
}
#endif
else
{
int_var = (int)long_var;
// printf("The number %d is OK!\n", int_var);
if( int_var>=0 && int_var<=9)
{
std::string str = "0" + stringify( int_var);
// printf("\n1 digit");
// cout<<"\n"<<str;
send(str);
}
else if( int_var>9 && int_var<60)
{
// printf("\n2 digit");
std::string str = stringify( int_var);
// cout<<"\n"<<str;
send(str);
}
else if(int_var>23)
{
printf("\nEnter correct value");
return 2;
}
return 1;
}
}
convert.h
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
class BadConversion : public std::runtime_error {
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};
inline std::string stringify(double x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}
i m creating this for checking input validation
i.e. input valid or not?
When i m given input:--
Enter the Time in 24 hr Format
E.g. 10:05:03 It shoud be in hh:mm:ss
Hour -> 22
Minute -> 1
Second -> 1
It gives Error
exe has encountered a problem and needs to close.
We are sorry for the inconvenience.
It gives Don't send problem??
How to resolve it??