.

Thursday 22 January 2015

change text color of c/c++ console applications

Changing background or text of console in c++ is not a very major task. One can do it easily by doing some really good efforts . This tutorial is all about changing the colors in c++ which helps to look application similar like a desktop.

Step 1 -

Make header file for color as - Color.h and write the following code inside it.


#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define BRIGHT "\x1b[1m"
#define RESET "\x1b[0m"

step 2 -

Calling these colors with cout statement to change the text as per wish.




include your header file in the program as

#include "Color.h"

..
..

you can use red color of text by writing cout statement as ..

cout<<RED<<"enter the string"<<RESET;

blue as

cout<<BLUE<<"enter the string"<<RESET;

yellow as

cout<<YELLOW<<"enter the string"<<RESET;

now it will always print the text in a desired form.
Read more ...

How to validate a date in c++ ?

Validation of date in c++ involves three steps -

1) Declare the function inside the body of a class.

2) Give definition to it outside the class using scope resolution.

3) Call it using do while loop by passing string type of dte in it.

Function Declaration -

class Validate
{
...

bool validateDate(string &);

...
}

Function Definition(Outside the class) -


bool Validate::validateDate(string& str_date)
{
  int d;
char mon1[3];
char mon[4],h1,h2;
int y;
char temp[10];
int m;  
   
   
if (str_date.length() == 9)
{

strcpy(mon1,str_date.substr(3,3).c_str());

d = atoi(str_date.substr(0,2).c_str());
strcpy(mon,str_date.substr(3,3).c_str());

        mon [sizeof(mon ) - 1] = 0;

  y = atoi(str_date.substr(7,2).c_str());

h1 = temp[2];
h2 = temp[6];
mon[0]=tolower(mon[0]);
mon[1]=tolower(mon[1]);
mon[2]=tolower(mon[2]);

if (!strcmp(mon,"jan")){m = 1;}
else if (!strcmp(mon,"feb")) {m=2;}
else if (!strcmp(mon,"mar")) {m=1;}
else if (!strcmp(mon,"apr")) {m=3;}
else if (!strcmp(mon,"may")) {m=1;}
else if (!strcmp(mon,"jun")) {m=3;}
else if (!strcmp(mon,"jul")) {m=1;}
else if (!strcmp(mon,"aug")) {m=1;}
else if (!strcmp(mon,"sep")) {m=3;}
else if (!strcmp(mon,"oct")) {m=1;}
else if (!strcmp(mon,"nov")) {m=3;}
else if (!strcmp(mon,"dec")) {m=1;}
else
{
cout<<endl<<"Invalid Month"<<endl;
return 0;
}

if ( (d>=1 && d<=31)&&(m==1) &&(y>=15 && y<=20))
{
return true;
}
else if((m == 3) && (d>=1 && d<=30) && (y>=15 && y<=20))
{

return true;
}
else if((m == 2) && (d>=1 && d<=28) && (y>=15 && y<=20))
{

return true;
}
else if((m == 2) && (d>=1 && d<=29) && (y==16 || y==20))
{

return true;
}
else
{
cout<<"Invalid Day or Invalid Year"<<endl;
return false;
}
}
else
{
cout<<endl<<"Invalid date format... \n Please Enter in";cout<<" DD-MMM-YY (22-MAR-15)"<<RESET<<endl;
return false;
break;
}
}

Function Calling-

do
{
cout << endl <<CYAN<< "Enter the discount period:"<<RESET;
        getline(cin,d_period);
}while(Validate::validateDate(d_period)!=1);
Read more ...

How to Validate String in c++ ?

Validation of string in c++ involves three steps -

1) Declare the function inside the body of a class.

2) Give definition to it outside the class using scope resolution.

3) Call it using do while loop by passing string  in it.

Function Declaration-

class Validate{
......

bool validateString(string&);

.....
}

Function Definition (outside class) -

bool Validate::validateString(string& str_string)
{
if(str_string.length()<1||str_string==" ")
                {
                cout<<"Field Cannot be Blank"<<endl;
                return 0;
                }
 
int i;
do
{
                                if(str_string[0]==' ')
                                {
                                cout<<"Input cannot begin with a blank"<<endl;
                                return false;
                                }

for(i=0;i<str_string.length();i++)
      {
if(!(isalnum(str_string[i])||str_string[i]=='.'||isspace(str_string[i])||str_string[i]=='-'))
{
cout<<"Enter valid string:\n"<<endl;
    return 0;
}
    }

      }while(i!=str_string.length());
return 1;
}



Function Call-
string name;
do
{
cout <<"Enter the Name:"<<endl;
getline(cin,name);
}while(Validate::validateString(name)!=1);
}
Read more ...

How to Validate a Number/Integer in c++ ?

Validation of integer in c++ involves three steps -

1) Declare the function inside the body of a class.

2) Give definition to it outside the class using scope resolution.

3) Call it using do while loop by passing string type of number in it.


Function declaration -

class Validate
{
......
bool validateLongNumber(string );
...

}

Function Definition (outside the class) -


bool Validate::validateLongNumber(string &str_string)
{
if(str_string.length()<1||str_string==" ")
{
cout<<"Field Cannot be Blank"<<endl;
return 0;
}

int i;
do
{
for(i=0;i<str_string.length();i++)
{
                                if(str_string[0]==' ')
                                {
                                cout<<"Input cannot begin with a blank"<<endl;
                                return false;
                                }

if(atol(str_string.c_str()))
{
if(!isdigit(str_string[i]))
{
cout<<"Enter valid number\n"<<endl;
return 0;
}
else continue;
}
if(str_string[0]=='0')            
{
                      cout<<"Please Don't Enter '0'"<<endl;
       return false;
                        }

cout<<"Enter valid number\n"<<endl;
return 0;

}
return 1;

}while(i!=str_string.length());
}


Function calling -
string number;

do
{
cout << "Enter the Number"<<endl;
        getline(cin,number);
}while(Validate::validateLongNumber(number)!=1);
Read more ...