Broad Network


C++ Basic Input and Output

C++ Taking the Bull by the Horns – Part 16

Forward: In this part of the series, I show a simple way of inputting data and a simple way of outputting data with C++.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

This is part 16 of my series, C++ Taking the Bull by the Horns. Since the beginning of this series, we have been writing programs but we have not input data using the keyboard. We have been outputting data using the monochrome (black and white) screen or its simulation like the Command Prompt window. Using the keyboard for input and a monochrome screen for output, where input and output is text, is said to be using the console. In this part of the series, I show a simple way of inputting data and a simple way of outputting data with C++.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The main Function
This is what we have seen about the main function:

int main()
    {
        //statements
    
        return 0;
    }

The return value of the main function is a value of type, int. That is why the main function is preceded by the word, int, at its definition. The last statement of the main function is “return 0;” which returns zero. Always end your main function with this return statement. It is possible to return some integer other than zero, but I will not go into that.

Any code outside the main function that has to be executed, must be called directly or indirectly from the block of the main function. When the program starts, statements of the main function are executed from top to bottom ending at “return 0;”. Before it reaches this end (returning zero), calls must have been made to code segments (functions) outside the block of the main function.

Executing a Program
To execute a program, you normally type something like the following at the DOS prompt:

        filename.exe

and then you press Enter. This is a place where you can type input for the program. The program name is filename.exe, where filename is the name you give. After typing the program name, you can type a space and then type strings that will be input to the program. The strings are separated by spaces (not by commas). So each of the strings should not have a space, since spaces are used to separate strings. For this to work, you need to put the following parameters in the parentheses of the main function:

        int argc, char *argv[]

You have two parameters here: an int and an array of strings. The identifier, argc for the int parameter is the number of strings in the array. For simple input, you do not have to know the value of this number, so you do not have to know the value in the object identified by argc.

Consider the following:

        filename.exe one two three

This is what you will type at the DOS prompt to try the code below. filename.exe represents the name you will give your executable file. Give any name you want, but let it end with .exe. After this, there is a space and then three strings. The strings above are separated by spaces. Quotation makes are not used. Each string should not have a space. Read and try the following code using the above line at the DOS prompt.

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
    {
        cout << argv[0]; cout << "\n";
        cout << argv[1]; cout << "\n";
        cout << argv[2]; cout << "\n";
        cout << argv[3]; cout << "\n";
    
        return 0;
    }

You output should be,

filename.exe
one
two
three

The array element, argv[0] from the second parameter would give the filename of the program. The rest of the elements would give strings you typed, in the order you typed them.

Outputting Data
You can output data using the cout object. We have seen some examples of the use of this object in this and the previous parts of the series. I will give you a more formal explanation on how this object is used later in the series.

I did not explain the details of the parameters for the main function. However, for these basic tutorials just accept things the way I have given.

Well, let us stop here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message