Showing posts with label article. Show all posts
Showing posts with label article. Show all posts

Saturday, 8 March 2014

Adding an icon to a Dev-C++ Program

If you go to the Dev C++ folder, you can find the .exe file of the program, but the icon will be the old default clumsy one. Just for an example let me create a small program and show you the exe file. Here ya go!


To change the icon, you need two things :
1. Dev-C++                    2. .ico file


.ico file basically is just an image file which is used as an icon. You can easily find an ico creator or an ico converter online.


  • In Dev-C++, click on Project -->  Project Options.

                

  • Click on the 'Browse' button in the icon section.


  • Click on the 'Browse' button in the icon section.
  • Navigate to the .ico file and click Ok.



  • Now compile the program again, and the desired program will have the icon you selected!


Happy Coding!
:)

Wednesday, 8 January 2014

Changing color of output window in Visual C++

Okay. Have you ever wondered how it would be to try something different than the same old black and white output window? Then this post is just for you.

For this, you will have to use the Visual C++ IDE [ :'( ]. Here are the steps:

1.You must add the header <windows.h>
2. The statement to change the color is :
system("color <color_code>");
 Color code is a couple of hexadecimal numbers, the first one corresponding to background color and the second one to font color.
Here is a simple example:

    #include<windows.h>
    void main()
    {
        system("color 95");
    }
This program renders the background color of output window as blue and font color as purple!


You can try different hexadecimal numbers instead of numbers to change the color according to your will. Here is the list of numbers and corresponding colors:

0 = Black                                    8 = Gray
1 = Blue                                      9 = Light Blue
2 = Green                                   A = Light Green
3 = Aqua                                    B = Light Aqua
4 = Red                                      C = Light Red
5 = Purple                                   D = Light Purple
6 =  Yellow                                  E = Light Yellow
7 = White                                    F = Bright White

e.g., If you want a Light Green background with Blue font in it, the statement should be
system("color A1");
You could play around with this command and have lots of fun with it!

NOTE : "color" is not to be confused with "colour" because former is American spelling and latter is British. Most of the programming and scripting languages use American spelling.

That's all for today folks.... Later!


Monday, 11 November 2013

How to repeat a program till the user wants to end it!

Here I am making a simple program-plug-in (as I call it) to execute the program more than once, if the user wants it. The task is to ask the user if he/she wants to execute the program again after one run. 

Suppose I have a program to find the factorial of a number. 
#include<iostream.h>
#include<conio.h>

long Factorial(long n)
{
     if(n==1){return 1;}
     else{return (n*Factorial(n-1));}
}

int main()
{
     long x;
     cout<<"Enter the number you want to find the factorial of : ";
     cin>>x;
     cout<<x<<"! = "<<Factorial(x)<<endl;
     return 0;
}

After one run, program automatically terminates and If I want to run it again, I will have to open it again. To solve this there are many methods. Im gonna show you two of them. 


  • Solution 1 - The viable one
                  We could use the old school 'GOTO' statement here. Just go ahead and include a label at the beginning of the program execution, and at the end of a run, ask if the user wants to run the program again, by a character choice,and if he says yes, just add a 'goto' statement there.
Syntax:                      label_name:
                                                        (program)
                                              
                                             goto label 


Here is the modified code after inclusion of goto:
#include<iostream.h>
#include<conio.h>

long Factorial(long n)
{
     if(n==1){return 1;}
     else{return (n*Factorial(n-1));}
}

int main()
{
     START:
     long x;
     char choice;
     cout<<"Enter the number you want to find the factorial of : ";
     cin>>x;
     cout<<x<<"! = "<<Factorial(x)<<endl;
     cout<<"Do you want to go again? [y/n] : ";
     cin>>choice;
     if (choice == 'y')
     {
         goto START;
     } 
 return 0;
}


This is a perfectly viable solution and no one cant say that it wont work. But, goto statement is considered harmful by many people since it breaks the beauty of structured programming. You can use it, of course, but there is also a generally preferred alternative for 'goto' statement - the iterators!

  • Solution 2 - The recommended one
                   The generally recommended alternative for using 'goto' is to use loops like do-while. In this case we are putting the whole program inside a giant do-while loop. We use it here because of its exit-controlled behavior. After each run, we keep asking the user if he/she wants to go again, and as long as the user says yes, the do-while loop keeps iterating again and again. 
Syntax:                   do{
                                                program_statements;

                                             }while(statement);

Here is the program code after the inclusion of do-while loop:
#include<iostream.h>
#include<conio.h>

long Factorial(long n)
{
    if(n==1){return 1;}
    else{return (n*Factorial(n-1));}
}

int main()
{
    char choice;
    long x;
    do
    {
       cout<<"Enter the number you want to find the factorial of : ";
       cin>>x;
       cout<<x<<"! = "<<Factorial(x)<<endl;
       cout<<"Do you want to go again? [y/n]"<<endl;
       cin>>choice; 
    }while ( choice!='n'&&choice=='y' && choice=='Y');
}
 

Keep in mind that the loop always comes inside the main() function whether or not the program has external classes or functions. Also remember that we may use any loop instead of do-while.


That's all for today, folks! Happy Coding!!!

You might also like