0
#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??

6
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by Nick Evan
Featured Replies
  • 1

    [QUOTE=Aamit;635739]so plz tell me what is the solution for it??[/QUOTE] Pick up the habbit of using e.g. the following compiler options -W -pedantic -Wall. When you compile, work through the error/warning list until your program compiles cleanly. Here is a guide [url]http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Warning-Options.html#Warning-Options[/url] Read More

0

This code is not even compiling. If you can post the correct-compiling code i can try to find the problem.

0

Your program is a confusing mixture between C and C++. It contains duplicate #include statements and you are using both cout and printf(). It looks like you took someone's C program and attempting to make it your own c++ by just adding cout and leaving all the rest of the C code.

As Angi said, you are going to have to post something that will compile cleanly before asking any questions about runtime errors.

0

Code is compiling.

i m also given convert.h .....
plz check out....

0

Code is compiling.

i m also given convert.h .....
plz check out....

What compiler are you using. Most c++ compilers issue error messages on lines like this: char buff[str.size()]; because the size of the character array must be a const value.

[edit]Oops! too late. :) [/edit]

0

and also when you have a funtion like

string send(string aaa)
{
       hr_str=aaa;
       //cout<<"\nin Fun"<<hr_str;
    
}

that defines a return type as 'string' and returns nothing.

0
#include <string>
#include <iostream>
#include <cctype>
using namespace std;

string getTime();
void printTime(const string& userTime);
bool checkMinSec(const string& userTime);
bool checkHour(const string& userTime);
int stringToInt(const string& tmpStr);

int main() {
	cout << "Enter the Time in 24 hr Format" << endl;
	cout << "E.g. 10:05:03 It shoud be in hh:mm:ss" << endl;
	string userTime;
	userTime = getTime();
	printTime(userTime);
}

string getTime() {
	bool valid = false;
	string userTime = "";
	while (!valid) {
		string tmpInpute;
		cout << "Hour -> ";
		getline(cin, tmpInpute);
		if (checkHour(tmpInpute)) {
			valid = true;
			userTime += tmpInpute + ':';
		} else {
			cout << "Not valid inpute" << endl << "Enter correct value" << endl;
		}
	}
	valid = false;
	while (!valid) {
		string tmpInpute;
		cout << "Minute -> ";
		getline(cin, tmpInpute);
		if (checkMinSec(tmpInpute)) {
			valid = true;
			userTime += tmpInpute + ':';
		} else {
			cout << "Not valid inpute" << endl << "Enter correct value" << endl;
		}
	}
	valid = false;
	while (!valid) {
		string tmpInpute;
		cout << "Second -> ";
		getline(cin, tmpInpute);
		if (checkMinSec(tmpInpute)) {
			valid = true;
			userTime += tmpInpute;
		} else {
			cout << "Not valid inpute" << endl << "Enter correct value" << endl;
		}
	}
	return userTime;
}

void printTime(const string& userTime) {
	cout << "Your Time->" << userTime << endl;
}

bool checkHour(const string& tmpStr) {
	if (tmpStr.size() > 2) {
		return false;
	}
	for (size_t i = 0; i < tmpStr.size(); ++i) {
		if (!isalnum(tmpStr[i])) {
			return false;
		}
	}
	if (stringToInt(tmpStr) > 23) {
		return false;
	}
	return true;
}

bool checkMinSec(const string& tmpStr) {
	if (tmpStr.size() > 2) {
		return false;
	}
	for (size_t i = 0; i < tmpStr.size(); ++i) {
		if (!isalnum(tmpStr[i])) {
			return false;
		}
	}
	if (stringToInt(tmpStr) > 59) {
		return false;
	}
	return true;
}

int stringToInt(const string& tmpStr) {
	int tmpInt = 0;
	for (size_t i = 0; i < tmpStr.size(); ++i) {
		tmpInt += (tmpStr[i] - '0');
		tmpInt *= 10;
	}
	return tmpInt/10;
}
0

First of all: giving away free code in violation with the Daniweb rules.

Second:

....

int stringToInt(const string& tmpStr) {
	int tmpInt = 0;
	for (size_t i = 0; i < tmpStr.size(); ++i) {
		tmpInt += (tmpStr[i] - '0');
		tmpInt *= 10;
	}
	return tmpInt/10;
}

I don't like that way of converting. It smells a bit 'C' to me.
How about:

#include <sstream>

[....]

 int stringToInt(const string& tmpStr) 
{
    std::istringstream strin(tmpStr);
    int i;
    strin >> i;
    return i;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.