Example of int main()
If you feel lost with the theory, let’s try out something practical to prove the point. The example has been simplified to illustrate the use of int main( ). We’ll write a program ‘prog1’ in which we’ll ask the user to enter an integer. Depending on the value entered, we’ll set the return value of main( ). We’ll use this return code in DOS to conditionally execute another program called ‘prog2’.
The programs are pretty simple and they would appear as:
//prog1.cpp – This program asks the user for an input and returns a value of 0 or //5.
#include<iostream.h>
int
main( )
{
int choice;
cout<<"\nWelcome to prog1";
cout<<"\nEnter 1 to skip 2nd program:";
cin>>choice;
if(choice==1)
{
return 5;
}
else
{
return 0;
}
}
Our second program just displays a statement to indicate that we are executing the second program.
//prog2.cpp – This program simply displays a statement on the screen
#include<iostream.h>
int
main( )
{
cout<<"\nCongrags.You are in the second program!";
return 0;
}
Build the two executable files: prog1.exe and prog2.exe.
Note: If you are using VC++, the exe files will be created in a folder called “Debug” within your project. Turbo C++ will create the exe files in the same folder itself.
So, now we have 2 programs but how do we conditionally execute the 2nd one depending on the return code of the first?
The answer lies in batch programming (if you are using DOS) or in shell programming (if you are using Unix/Linux). We’ll deal with batch programming. This is basically the creation of *.bat files (so far we’ve been creating *.exe files). I won’t go into this topic deeply but I’ll cover a bit of it so that you can appreciate return codes from programs. Before starting to write a *.bat file, you can copy the 2 exe files we created (prog1.exe and prog2.exe) into the same folder (I’ve copied them into my C:\).
I assume that you have used a bit of MS-DOS (at least you should be familiar with the command prompt, changing directories etc.).
Command prompt: To get to this from Windows go to START->RUN and type “command” in the pop-up box. You’ll be taken to the DOS prompt (this is the place from where you can give commands to DOS). The prompt on my system is:
C:\MYWIN\Desktop>
Now type cd\ to go to C:\
C:\>
Let’s create a batch file named combo.bat. To do this simply type:
C:\>edit combo.bat
You’ll be taken to an MS-DOS text editor (similar to Notepad in Windows). Type the following in the file, save it and return back to DOS.
@ECHO OFF
ECHO **** WELCOME TO BATCH PROGRAMMING ****
ECHO Executing the prog1
prog1
IF errorlevel 5 GOTO end
prog2
:end
ECHO End of batch file
Perhaps everything seems weird?
Remember: MS-DOS command.com is not case-sensitive (i.e. typing DIR or dir is the same).
The first 3 lines of our batch file are used for the purpose of displaying something on the screen (equivalent to cout<< in C++). On the 4th line we say:
prog1
This is equivalent to executing the program ‘prog1.exe’ on the command prompt. Batch files are used basically to execute a set of instructions/programs in a particular sequence. If every time you log into the system, you want to perform 10 commands, then each time you’ll have to keep typing the 10 commands on your prompt. You can save time (and also needn’t worry about remembering the sequence of commands) if you write those 10 command in a batch file. Now, each time you log into the system, you just need to type the name of the batch file and voila! (all your 10 commands will be executed faithfully).
Coming back to our program, prog1 in the batch file will execute our first program. You’ll see the display:
Welcome to prog1
Enter 1 to skip 2nd program: 1
Let’s assume the user types 1. According to our program:
if(choice==1)
{
return 5;|
}
Now prog1 returns a value of 5 to the caller. The caller is our batch program combo.bat. The next line of the batch program checks for this return code.
IF errorlevel 5 GOTO end
The IF condition becomes true if the previous step had a return value of 5 or greater. In our case the previous step was the execution of ‘prog1’ and this returned a value of 5. Thus the condition is true and the combo.bat will go to the section labeled ‘end’. Here we just display a statement saying that it’s the end of the batch program (echo is used to display on the screen).
If the user had entered some other value then the batch file would have executed prog2 since the return value would have been less than 5 (in our case it was 0) and so the IF condition would be false.
The output if you entered 1 would be:
C:\>combo
**** WELCOME TO BATCH PROGRAMMING ****
Executing the prog1
Welcome to prog1
Enter 1 to skip 2nd program:1
End of batch file
C:\>
The output if you entered some other number would be:
C:\>combo
**** WELCOME TO BATCH PROGRAMMING ****
Executing the prog1
Welcome to prog1
Enter 1 to skip 2nd program:3
Congrags.You are in the second program!End of batch file
C:\>
Just follow the above steps, create the batch file and try it out on your system. To execute batch files you don’t need to compile the file (i.e. combo.bat can be directly executed). This is because the command prompt is an interpreter (it executes commands one at a time and does not require compilation as we do for C++ programs).
In Unix/Linux, this is called shell programming (and instead of batch files we call them shell scripts). In large applications, you would need to execute a series of programs everyday and these are written in shell scripts. The execution of programs would depend on the return code of the previous program (if the previous program failed because of some error then you might not want to continue execution of the remaining programs). By now you should have understood about the difference between void main( ) and int main( ).
Note: If you are using Unix/Linux then refer to the Appendix for the above section.
Copyright © 2004 Sethu Subramanian All rights reserved.