Friday, December 21, 2012

Creating simple C++ programs in Linux

OK, I hope you are ready to begin working with the new platform here. If you have troubles with the basic installation of fedora-14, please go back to the previous post for furthest clarification.

First and foremost, we'll begin with the programming tradition. Yes, it is a simple Hello World program. Actually I'm doing this to make you aware how to handle things here in linux platform.

Hello World program

  1. open a new terminal in linux and type vi hello.cpp and press Enter.this will open a blank text editor. Note that hello is the name that we are giving for the program and .cpp extension says it is going to be a C ++ program. vi command is to open the text editor
  2. as soon as you open the text editor, you will see that you can't type anything yet. to do so, first press key i from your key board (i says insert mode is on)
  3. type the below code 
#include<iostream>
using namespace std;
int main()
{
        cout<<"Hello World!"<<endl;
        return 0;

}

#include will include the header files that are required to the program, here we need only the iostream library. if we need any more library files, we have to mention them seperately

second line says that we are using the standard namespace

next line begins the main method of the program. here the return type of the main method is int so, we have to return an integer before exiting the program

cout << command is used to print something on the screen

endl mentions the end of line (to move the cursor to the next line

then we return an integer. since we have no any special value to return, we can reurn 0 (of course you can return any number)

4. to save the program, press Esc key from the keyboard and : , then type wq
this will take you back to the terminal exiting the text editor.



5. now we have to compile our program before running. to do so, type the below shown command

g++ -o hello hello.cpp

6. if you don't have any compile time errors, then the system won't show you any errors.

7.then, to run the program, type the below command

./hello

you will see the Hello World! statement printed on the screen. (refer the below image)

Great! even the program we made was so simple, it could give you a good basic knowledge if you are a beginner.

A simple program to convert temperature units

you will learn some more commands via this little program. just open a new text editor named tempConvertor.cpp and type the below shown code

//This program will convert 
//temperatures between Celsius and Fahrenheit.

#include<iostream>
using namespace std;
int main()
{

float celsius,fahrenheit;                          //declare two variables of type float

fahrenheit=95.0;                                     //varaible fahrenheit is hard coded with the value 95.0
celsius=(fahrenheit-32)*(5.0/9.0);         //calculation to convert the fahrenheit value to celsius

        //print the output
cout<<fahrenheit<<" degrees  in fahrenheit is "<<celsius<<" degrees in celsius "<<endl; 

return 0;
}

the program code is commented to understand each line easily
if you compile and run the program, you should get the below output.











A simple program to get the light years from the user and print the distance in inches

#include<iostream>
using namespace std;
int main()

{
long double light_years,distance_1_year,distance_inches;

cout<<"Enter Light Years:"<<endl;    //print this message to get the user input
cin>> light_years;                                 //get the user input and assign it to light_years variable

distance_1_year=light_years*300000*60.0L*60*24*365;
distance_inches=distance_1_year*1000000/(25.4L);

cout<<light_years<<" Light years are equal to "<<distance_inches<<" distance in Inches"<<endl;

return 0;
}